我是编程世界的新手。好吧,我正在尝试使用 apache-poi 库读取一个 excel 文件(5 行,5 列)。我实际上有两个相同问题的实现。在第一个代码片段中,我只是读取了我的 excel 文件并将它们打印到控制台中。
但是现在我试图将读取的 excel 数据保存到一个数组中。所以我想在动态获取excel行和列大小后设置数组大小。但令我惊讶的是,当我执行我的第二个代码片段时,即使我的输入 excel 文件中只有 5 行、5 列,“while(cellIterator.hasNext()”似乎也在不断迭代。请指导我哪里出错了.
感谢 Hussain 代码片段 1(按预期工作)
public static void main(String args[]) {
readFile(ConfigReader.readConfigValues("XLS-path"),
ConfigReader.readConfigValues("SheetName"));
}
public static void readFile(String filePath, String sheetName) {
try {
FileInputStream file = new FileInputStream(new File(filePath));
// Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(file);
// Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheet(sheetName);
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
}
}
System.out.println("");
}
file.close();
FileOutputStream out = new FileOutputStream(new java.io.File(
"G:\\test1.xls"));
workbook.write(out);
out.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
代码片段 2(未按预期工作)
public static void main(String args[]) {
readFile(ConfigReader.readConfigValues("XLS-path"),
ConfigReader.readConfigValues("SheetName"));
}
public static void readFile(String filePath, String sheetName) {
try {
FileInputStream file = new FileInputStream(new File(filePath));
// Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(file);
// Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheet(sheetName);
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
String[][] excelArray = null;
excelArray = new String[getRowCount(rowIterator, excelArray)][];
file.close();
FileOutputStream out = new FileOutputStream(new java.io.File(
"G:\\test1.xls"));
workbook.write(out);
out.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static int getRowCount(Iterator<Row> rowIterator,
String[][] excelArray) {
int sizeArrayRow = 0;
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
excelArray[sizeArrayRow] = new String[getColCount(row)];
sizeArrayRow++;
}
return sizeArrayRow;
}
public static int getColCount(Row row) {
int sizeArrayCol = 0;
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
sizeArrayCol++;
}
return sizeArrayCol;
}