如何使用java eclipse阅读excel页面的特定表格

时间:2015-06-21 13:59:32

标签: java eclipse apache-poi

我想阅读Excel电子表格的第3页,并比较包含日期的D和F列。如何读取这些列并在Eclipse中进行比较?

我想使用Apache POI

提前致谢。

1 个答案:

答案 0 :(得分:2)

FileInputStream file = new FileInputStream(new File("C:\\test.xls"));

//Get the workbook instance for XLS file 
HSSFWorkbook workbook = new HSSFWorkbook(file);

//Get third(numbering starts from 0) sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(2);

//Get iterator to all the rows in current sheet
Iterator<Row> rowIterator = sheet.iterator();

// Iterate through rows
while (rowIterator.hasNext()) {
    Row row = rowIterator.next();

    // Index of column D is 3 (A->0, B->1, etc)
    Cell cellD = row.getCell(3);
    Cell cellF = row.getCell(5);

    Date dateD = HSSFDateUtil.getJavaDate(cellD.getNumberValue()); 
    Date dateF = HSSFDateUtil.getJavaDate(cellF.getNumberValue()); 

    // Your business logic continues....
}