Question

I need to work with both xls and xlsx. I got an outofmemory error when using xssf so I changed to sxssf and while that doesn't work I would like to change my code to use eventusermodel instead of ss usermodel. Unfortunately I do not understand very well how to use event api so if someone could provide some example code to go from File file or inputstream to a workbook.

Était-ce utile?

La solution

You should use the Event API, meaning that you need to combine SAX for reading and SXSSFWorkbook for writing.

This example is an Excel to CSV convertor. You should do something likewise in the endElement() method. You should create a new Row if not created and a new cell every time there is a value (name=="v"). Set the type of the cell and the new value:

if ("v".equals(name)) {
            if (row == null)
                row = sheet.createRow(0);
            cell = row.createCell(thisColumn);

            switch (nextDataType) {
            case BOOL:
                cell.setCellType(Cell.CELL_TYPE_BOOLEAN);
                char first = value.charAt(0);
                thisStr = first == '0' ? "FALSE" : "TRUE";
                if (thisStr == "FALSE")
                    cell.setCellValue(false);
                else
                    cell.setCellValue(false);

            case OTHER_CELL_TYPE:
                 //.... 

            default:
                cell.setCellType(Cell.CELL_TYPE_BLANK);
                break;
            }
             //......More proccessing
}

Here you have the apache poi SXSSF example for a better understanding of how to save it.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top