如何使用Apache POI读取MS excel文件?

时间:2017-05-02 05:40:24

标签: java excel apache swing

我有一个MS-Excel文件,我想通过Apache POI阅读。我以前没有这方面的工作,所以我不知道怎么做。任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:0)

public List<Record> readBooksFromExcelFile(String excelFilePath)
        throws IOException {

    List<Record> listBooks = new ArrayList<Record>();
    FileInputStream inputStream = new FileInputStream(new File(
            excelFilePath));

    Workbook workbook = new XSSFWorkbook(inputStream);
    Sheet firstSheet = workbook.getSheetAt(0);
    Iterator<Row> iterator = firstSheet.iterator();

    FormulaEvaluator objFormulaEvaluator = new XSSFFormulaEvaluator(
            (XSSFWorkbook) workbook);
    DataFormatter objDefaultFormat = new DataFormatter();
    String cellValueStr;

    int headingRow = 0;

    System.out
            .println("================== Start Reading Records From Excel ==================");
    LogFileWriter
            .appendToLogfile(
                    "================== Start Reading Records From Excel ==================",
                    true);

    while (iterator.hasNext()) {

        Row nextRow = iterator.next();
        // to ignore the first row.

        if (headingRow == 0) {
            headingRow++;
            continue;
        }
        Iterator<Cell> cellIterator = nextRow.cellIterator();
        Record aRecord = new Record();

        while (cellIterator.hasNext()) {
            Cell nextCell = cellIterator.next();
            nextCell.setCellType(Cell.CELL_TYPE_STRING);
            int columnIndex = nextCell.getColumnIndex();

            // to ignore the heading row in file.

            switch (columnIndex) {
            case 0:
                // objFormulaEvaluator.evaluate(nextCell);
                // cellValueStr =
                // objDefaultFormat.formatCellValue(nextCell,objFormulaEvaluator);

                cellValueStr = (String) getCellValue(nextCell);
                cellValueStr = cellValueStr.replace(" ", "");
                aRecord.setIccid(cellValueStr);

                // aRecord.setIccid((String) getCellValue(nextCell));
                break;
            case 1:
                // objFormulaEvaluator.evaluate(nextCell);
                // cellValueStr =
                // objDefaultFormat.formatCellValue(nextCell,objFormulaEvaluator);
                // cellValueStr=cellValueStr.replace(" ", "");

                cellValueStr = (String) getCellValue(nextCell);
                cellValueStr = cellValueStr.replace(" ", "");
                aRecord.setApn(cellValueStr);
                break;
            case 2:
                // objFormulaEvaluator.evaluate(nextCell);
                // cellValueStr =
                // objDefaultFormat.formatCellValue(nextCell,objFormulaEvaluator);
                // cellValueStr=cellValueStr.replace(" ", "");

                cellValueStr = (String) getCellValue(nextCell);
                cellValueStr = cellValueStr.replace(" ", "");
                aRecord.setIpAddress(cellValueStr);
                break;
            case 3:
                // objFormulaEvaluator.evaluate(nextCell);
                // cellValueStr =
                // objDefaultFormat.formatCellValue(nextCell,objFormulaEvaluator);
                // cellValueStr=cellValueStr.replace(" ", "");

                cellValueStr = (String) getCellValue(nextCell);
                cellValueStr = cellValueStr.replace(" ", "");
                aRecord.setPdp(cellValueStr);
                break;
            }
        }

        System.out.println("READ: " + aRecord.toString());
        LogFileWriter.appendToLogfile("READ: " + aRecord.toString(), true);
        listBooks.add(aRecord);
    }
    // workbook.close();
    System.out.println("================== Finished Reading Records From Excel ==================");
    LogFileWriter.appendToLogfile("================== Finished Reading Records From Excel ==================",true);
    return listBooks;
}

private Object getCellValue(Cell cell) {

    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        return cell.getStringCellValue();

    case Cell.CELL_TYPE_BOOLEAN:
        return cell.getBooleanCellValue();

    case Cell.CELL_TYPE_NUMERIC:
        return cell.getNumericCellValue();
    }

    return null;
}

这是Record.java

public class Record {

private String iccid;
private String apn;
private String ipAddress;
private String pdp;
public String getIccid() {
    return iccid;
}
public void setIccid(String iccid) {
    this.iccid = iccid;
}
public String getApn() {
    return apn;
}
public void setApn(String apn) {
    this.apn = apn;
}
public String getIpAddress() {
    return ipAddress;
}
public void setIpAddress(String ipAddress) {
    this.ipAddress = ipAddress;
}
public String getPdp() {
    return pdp;
}
public void setPdp(String pdp) {
    this.pdp = pdp;
}
public String toString() {
    return "Record [iccid=" + iccid + ", apn=" + apn + ", ipAddress="
            + ipAddress + ", pdp=" + pdp + "]";
}

}