简体   繁体   中英

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

I am trying to read a large excel file. I have looked into the POI library but i am not able to understand the codes present in POI event Library

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.

I have tried sjxlsx.jar also. But while implementing, i found that the jar doesnot contain all the class methods.

 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?

Out of memory error is solved by adding -Xmx500m (for instance). The JVM has a default max memory, which is actually relatively low.

java -Xmx500m com.yourpackage.Application

This example sets it to 500 meg.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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