-1

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?

rahul
  • 137
  • 3
  • 14
  • 2
    Can you please paste the code you have tried? and what error message you are getting while reading? – Abhijeet Dhumal Jun 17 '15 at 12:30
  • I have not tried any code using apache POI yet. The one that i tried gave me out of memory error. for sjxlsx the below is the code – rahul Jun 17 '15 at 12:46

1 Answers1

-1

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.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • I have tried this using higher memory levels, still i am getting the same error.java.lang.OutOfMemoryError: GC overhead limit exceeded – rahul Jun 19 '15 at 08:28
  • Note that the "GC overhead limit exceeded" message is not directly related to the max heapsize. This answer might help: http://stackoverflow.com/questions/1393486/error-java-lang-outofmemoryerror-gc-overhead-limit-exceeded – llogiq Jun 19 '15 at 08:40