I'm new to the world of programming. well, im trying to read a excel file (5 rows, 5 cols)using apache-poi library. I have actually two implementation of the same problem. In the first code snippet, i just read my excel files and print them into console.
However now im trying to save the read excel data into an array. So i want to set the array size after getting the excel row and column size dynamically. But to my surprise, when i execute my second code snippet, it seems that "while(cellIterator.hasNext()" iterates continuously even though there are only 5 rows, 5 cols in my input excel file. Please guide me where im going wrong.
Thanks Hussain Code snippet 1 (Working as expected)
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();
}
}
Code snippet 2 (Not working as expected)
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;
}