阅读具体的excel列到java程序中

我需要阅读Excel工作表的特定列,然后在java中声明variables。 我所做的程序读取了excel表格的全部内容。 但是我需要读一个像C这样的固定列

这是我所做的:

import java.io.File; import java.io.IOException; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; public class JavaApplication4 { private String inputFile; String[][] data = null; public void setInputFile(String inputFile) { this.inputFile = inputFile; } public String[][] read() throws IOException { File inputWorkbook = new File(inputFile); Workbook w; try { w = Workbook.getWorkbook(inputWorkbook); // Get the first sheet Sheet sheet = w.getSheet(0); data = new String[sheet.getColumns()][sheet.getRows()]; // Loop over first 10 column and lines // System.out.println(sheet.getColumns() + " " +sheet.getRows()); for (int j = 0; j <sheet.getColumns(); j++) { for (int i = 0; i < sheet.getRows(); i++) { Cell cell = sheet.getCell(j, i); data[j][i] = cell.getContents(); // System.out.println(cell.getContents()); } } for (int j = 0; j < data.length; j++) { for (int i = 0; i <data[j].length; i++) { System.out.println(data[j][i]); } } } catch (BiffException e) { e.printStackTrace(); } return data; } public static void main(String[] args) throws IOException { JavaApplication4 test = new JavaApplication4(); test.setInputFile("C://users/admin/Desktop/Content.xls"); test.read(); } } 

这是我的Excel表单,

从编号为/@v1@/ /@v2@/的碗中,随机绘制一个单一的小块。 找出抠出的概率是一个数字,是/@v3@//@ v4@/的倍数。

我需要读取这些数据,并通过匹配模式/@v1@1 ,我需要声明variables。 我怎样才能做到这一点?

你可以做什么,你应该首先使用sheet.getColumns()获取表单中的所有列,并将所有列存储在列表中。 那么你可以匹配得到所有的值基于列。 或者你可以使用下面的代码只获得列“C”.try。 让我知道这个是否奏效。

 int masterSheetColumnIndex = sheet.getColumns(); List<String> ExpectedColumns = new ArrayList<String>(); for (int x = 0; x < masterSheetColumnIndex; x++) { Cell celll = sheet.getCell(x, 0); String d = celll.getContents(); ExpectedColumns.add(d); } LinkedHashMap<String, List<String>> columnDataValues = new LinkedHashMap<String, List<String>>(); List<String> column1 = new ArrayList<String>(); // read values from driver sheet for each column for (int j = 0; j < masterSheetColumnIndex; j++) { column1 = new ArrayList<String>(); for (int i = 1; i < sheet.getRows(); i++) { Cell cell = sheet.getCell(j, i); column1.add(cell.getContents()); } columnDataValues.put(ExpectedColumns.get(j), column1); } 

这是非常简单和高效的代码,并按预期工作

 import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class TestExcelFile { public static void main(String[] args) { String envFilePath = System.getenv("AZURE_FILE_PATH"); // upload list of files/directory to blob storage File folder = new File(envFilePath); File[] listOfFiles = folder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { System.out.println("File " + listOfFiles[i].getName()); Workbook workbook; //int masterSheetColumnIndex = 0; try { workbook = WorkbookFactory.create(new FileInputStream(envFilePath + "\\"+ listOfFiles[i].getName())); // Get the first sheet. Sheet sheet = workbook.getSheetAt(0); //we will search for column index containing string "Your Column Name" in the row 0 (which is first row of a worksheet String columnWanted = "Column_Name"; Integer columnNo = null; //output all not null values to the list List<Cell> cells = new ArrayList<Cell>(); // Get the first cell. Row row = sheet.getRow(0); //Cell cell = row.getCell(0); for (Cell cell : row) { // Column header names. //System.out.println(cell.toString()); if (cell.getStringCellValue().equals(columnWanted)){ columnNo = cell.getColumnIndex(); } } if (columnNo != null){ for (Row row1 : sheet) { Cell c = row1.getCell(columnNo); if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) { // Nothing in the cell in this row, skip it } else { cells.add(c); //System.out.println(c); } } }else{ System.out.println("could not find column " + columnWanted + " in first row of " + listOfFiles[i].getName()); } } catch (InvalidFormatException | IOException e) { e.printStackTrace(); } } } } }