You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by suter_daniel <su...@hotmail.com> on 2012/04/18 15:53:26 UTC

Corrupt excels using SXSSF

Hi,
I tried to use SXSSF for the generation of large excel files. It works
perfectly file, if I don't use an existing template. As soon as I use a
template the file will be corrupt. Excel can only restore the original
template without any data I added.

This works (only relevant parts):

*final SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(200);*
Sheet sheet = sxssfWorkbook.createSheet();
...
        int currentRowIndex = firstDataRowIndex;
        final List<ReportRow> rows = reportData.getRows();
        for (final ReportRow reportRow : rows) {
            final Row row = sheet.createRow(currentRowIndex);
            final ColumnDescription[] columnNames =
reportRow.getColumnNames();
            for (final ColumnDescription columnName : columnNames) {
                if (!columnName.isMISOnly()) {
                    final Object columnValue =
reportRow.getColumnValue(columnName);
                    ExcelUtils.createCell(row,
columnNameToColumnMap.get(columnName), columnValue);
                }
            }

            currentRowIndex++;
        }
...
sxssfWorkbook.write(outputStream);


This does not work:

*final Workbook template =
reportData.getDescription().getTemplate(languageCode);
final SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook((XSSFWorkbook)
workbook, 200);*
Sheet sheet = sxssfWorkbook.getSheetAt(0);
...
sxssfWorkbook.write(outputStream);


--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Corrupt-excels-using-SXSSF-tp5649133p5649133.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Corrupt excels using SXSSF

Posted by suter_daniel <su...@hotmail.com>.
You're right. After lots of debugging I figured it out as well. The problem
with the template I use is, that it contains empty cells. So you can't see
any content in excel itself. So I unpacked the xlsx-file and looked at the
content which contains description for empty cells.
Would it make sense for the sxssf library to throw some kind of a
RuntimeException in case something gets overriden? 

--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Corrupt-excels-using-SXSSF-tp5649133p5650977.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Corrupt excels using SXSSF

Posted by "Qing.Liao" <ql...@live.cn>.
can we use SXSSFWorkbook to read a template ? 

ClassLoader clzloader = ReadExsitExcel.class.getClassLoader();
InputStream stream = clzloader.getResourceAsStream("workbook.xlsx");
XSSFWorkbook xsbook = new XSSFWorkbook(stream);
Sheet sheet0 = xsbook.getSheetAt(0);
int xsnum0 = sheet0.getLastRowNum();
SXSSFWorkbook sxsbook = new SXSSFWorkbook(xsbook);
Sheet sheet = sxsbook.getSheetAt(0);
int xsnum = sheet.getLastRowNum();
System.out.println(xsnum0);
System.out.println("=============");
System.out.println(xsnum);

--------------------

29
=============
0

the value of xsnum0 is correct. but the value of xsnum always be 0.
SXSSFWorkbook  can't read template?
		

--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Corrupt-excels-using-SXSSF-tp5649133p5709857.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Corrupt excels using SXSSF

Posted by Yegor Kozlov <ye...@dinom.ru>.
Make sure your code appends new rows in ascending order. If the
template contains data and your code overwrites it then the output
will be bad.
Remember that SXSSF is write-only API. It can append rows but cannot
overwrite or insert rows in random order. This is the price you pay
for low-memory footprint.


Yegor

On Wed, Apr 18, 2012 at 5:53 PM, suter_daniel <su...@hotmail.com> wrote:
> Hi,
> I tried to use SXSSF for the generation of large excel files. It works
> perfectly file, if I don't use an existing template. As soon as I use a
> template the file will be corrupt. Excel can only restore the original
> template without any data I added.
>
> This works (only relevant parts):
>
> *final SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(200);*
> Sheet sheet = sxssfWorkbook.createSheet();
> ...
>        int currentRowIndex = firstDataRowIndex;
>        final List<ReportRow> rows = reportData.getRows();
>        for (final ReportRow reportRow : rows) {
>            final Row row = sheet.createRow(currentRowIndex);
>            final ColumnDescription[] columnNames =
> reportRow.getColumnNames();
>            for (final ColumnDescription columnName : columnNames) {
>                if (!columnName.isMISOnly()) {
>                    final Object columnValue =
> reportRow.getColumnValue(columnName);
>                    ExcelUtils.createCell(row,
> columnNameToColumnMap.get(columnName), columnValue);
>                }
>            }
>
>            currentRowIndex++;
>        }
> ...
> sxssfWorkbook.write(outputStream);
>
>
> This does not work:
>
> *final Workbook template =
> reportData.getDescription().getTemplate(languageCode);
> final SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook((XSSFWorkbook)
> workbook, 200);*
> Sheet sheet = sxssfWorkbook.getSheetAt(0);
> ...
> sxssfWorkbook.write(outputStream);
>
>
> --
> View this message in context: http://apache-poi.1045710.n5.nabble.com/Corrupt-excels-using-SXSSF-tp5649133p5649133.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org