我是新來的編程世界。好吧,即時通訊嘗試使用apache-poi庫閱讀excel文件(5行,5列)。我實際上有兩個相同問題的實現。 在第一個代碼片段中,我只是讀取我的excel文件並將它們打印到控制檯中。使用apache poi讀取Excel文件並將它們存儲到數組中
但是現在我試圖將讀取excel數據保存到數組中。所以我想在動態獲取excel行和列大小後設置數組大小。但令我驚訝的是,當我執行我的第二個代碼片段時,似乎「while(cellIterator.hasNext()」連續迭代,即使在我的輸入excel文件中只有5行,5列。 。
感謝 侯賽因 代碼片段(按預期工作)
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;
}
大流士,對不起,我忘了提及它。其實我使用「cellIterator.next()」遍歷實際工作的Excel單元格。但是我面臨的問題是,即使在我的Excel表中有5列,它也會不斷迭代:) – Hussain 2013-02-19 01:38:36
您的意思是代碼的第二部分與您使用的代碼不同嗎?因爲,雖然第一個片段包含「cellIterator.next()」,但第二個片段卻沒有。 – 2013-02-19 04:05:37
嗨大流士,即使第二個片段包含相同的代碼,但它已被放入一個方法。 – Hussain 2013-02-19 13:01:49