简体   繁体   English

如何使用 POI 库读取大型 excel 文件 (.xlsx)?

[英]How to use POI library to read large excel file(.xlsx)?

I am trying to read a large excel file.我正在尝试读取一个大的 excel 文件。 I have looked into the POI library but i am not able to understand the codes present in POI event Library我查看了 POI 库,但我无法理解POI 事件库中的代码

I just want to read the excel file and write each element to a new file after changing the datatypes and then save it in the database.我只想在更改数据类型后读取excel文件并将每个元素写入一个新文件,然后将其保存在数据库中。

I have tried sjxlsx.jar also.我也试过 sjxlsx.jar。 But while implementing, i found that the jar doesnot contain all the class methods.但是在实现时,我发现 jar 并不包含所有的类方法。

 SimpleXLSXWorkbook workbook = new SimpleXLSXWorkbook(new File("C:/test.xlsx"));

        HSSFWorkbook hsfWorkbook = new HSSFWorkbook();

        org.apache.poi.ss.usermodel.Sheet hsfSheet = hsfWorkbook.createSheet();

        Sheet sheetToRead = workbook.getSheet(0, false);

        SheetRowReader reader = sheetToRead.newReader();

        Cell[] row;

        int rowPos = 0;

        while ((row = reader.readRow()) != null) {     
            org.apache.poi.ss.usermodel.Row hfsRow = hsfSheet.createRow(rowPos);

            int cellPos = 0;

            for (Cell cell : row) {

            if(cell != null){

                org.apache.poi.ss.usermodel.Cell hfsCell = hfsRow.createCell(cellPos);

                hfsCell.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING);
                hfsCell.setCellValue(cell.getValue());
            }
        cellPos++;
    }
    rowPos++;
}
return hsfSheet;

Can anyone help me how to use XSSF and SAX (Event API) to read large excel files?谁能帮助我如何使用 XSSF 和 SAX(事件 API)来读取大型 excel 文件?

Out of memory error is solved by adding -Xmx500m (for instance).通过添加 -Xmx500m(例如)解决内存不足错误。 The JVM has a default max memory, which is actually relatively low. JVM 有一个默认的 max memory,实际上是比较低的。

java -Xmx500m com.yourpackage.Application java -Xmx500m com.yourpackage.Application

This example sets it to 500 meg.本示例将其设置为 500 兆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM