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.
Asked
Active
Viewed 652 times
0
-
Did you try following the [examples posted on the POI website](http://poi.apache.org/spreadsheet/how-to.html#xssf_sax_api)? – Gagravarr Jun 07 '12 at 21:37
-
Yes but I need to convert to a workbook so Im not sure how to start that..even after looking at the example on the website – Marisa Jun 08 '12 at 14:15
-
You can't. If you want a full workbook, buy some more memory! If you are very resource constrained, you have to read at a low level... – Gagravarr Jun 08 '12 at 14:24
1 Answers
0
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.