We can use Jakarta COMMON POI to do it:
E.g. of Itertate over a file:
// load the excel
FileInputStream fin = new FileInputStream("simple1.xls");
POIFSFileSystem fs = new POIFSFileSystem(fin);
//create a Workbook
HSSFWorkbook wb = new HSSFWorkbook(fs);
// get a reference to the worksheet
int numberPage = wb.getNumberOfSheets();
// TODO:debug information
out.printf("Number Of Page %s%n", numberPage);
//read each sheet
for (int i = 0; i < numberPage; ++i) {
HSSFSheet sheet = wb.getSheetAt(i);
// TODO:debug information
int firstRow = sheet.getFirstRowNum();
int lastRow = sheet.getLastRowNum();
out.printf("FirstRow: %s, LastRow: %s%n", firstRow, lastRow);
out.printf("Physical Row: %s%n", sheet.getPhysicalNumberOfRows());
//read each Row
for (int j = firstRow; j <= lastRow; ++j) {
HSSFRow row = sheet.getRow(j);
//read each Cell
for (short k = row.getFirstCellNum(); k < row.getLastCellNum(); ++k) {
HSSFCell cell = (HSSFCell) row.getCell(k);
switch (cell.getCellType()) {
case (HSSFCell.CELL_TYPE_STRING): {
out.print(cell.getStringCellValue() + "\t");
break;
}
case (HSSFCell.CELL_TYPE_NUMERIC): {
out.print(cell.getNumericCellValue() + "\t");
break;
}
case (HSSFCell.CELL_TYPE_BOOLEAN): {
out.print(cell.getBooleanCellValue() + "\t");
break;
}
}
}
out.println();
}
}
fin.close();
No comments:
Post a Comment