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

祝福

我有一个 MS-Excel 文件,我想通过 Apache POI 读取它。我以前没有做过这方面的工作,所以我不知道该怎么做。谁可以帮我这个事 ?

祖拜尔
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 + "]";
}

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法使用Apache POI从Excel文件读取

使用Apache POI确定MS Excel文件类型

使用Apache POI 4.1.1从Excel文件读取值时出错

使用Apache POI API从Excel文件中读取值

尝试使用POI Apache库读取Excel文件

如何用Apache POI读取.xlsx文件?

使用Apache POI读取.docx文件

Apache POI将txt文件读取为Excel文件

如何插入MS表格Excel中使用的Apache POI的Java

使用Apache POI如何读取特定的Excel列

如何使用Apache POI读取和搜索excel数据

通过apache poi读取excel文件(在classpath中)

通过Apache POI读取大型Excel文件(xlsx)时出错

Apache POI-读取会修改Excel文件

如何在apache POI中读取Excel文件的确切单元格内容

使用Java Apache POI 3.9 Eclipse从Excel文件.xlsx中读取

使用Apache POI从Java中的xls和xlsx excel文件读取和写入

使用Apache POI库从Excel(xlsx)文件读取会引发NullPointerException

使用Apache POI读取数值表格Excel文件时出现问题

为什么在使用Apache POI读取Excel文件之前需要创建工作簿?

使用Apache POI在Java中读取/写入Excel文件时出现问题

NumberFormatException:对于输入字符串:尝试使用 apache poi 读取 excel 文件时的“null”

从 Apache poi java 读取 Excel

使用Apache POI更新Excel文件

如何使用POI读取Excel文件的空白单元格

使用Apache POI从Excel读取下拉列表内容

使用Java / Apache POI读取Excel并计算总和

使用Apache POI从Excel读取单元格

无法使用apache poi更改ms字文件(XWPF)的方向