https://issues.apache.org/bugzilla/show_bug.cgi?id=56170

            Bug ID: 56170
           Summary: Exception when adding content to tables that have no
                    headers
           Product: POI
           Version: 3.10
          Hardware: PC
            Status: NEW
          Severity: normal
          Priority: P2
         Component: XSSF
          Assignee: [email protected]
          Reporter: [email protected]

Created attachment 31338
  --> https://issues.apache.org/bugzilla/attachment.cgi?id=31338&action=edit
Template file to reproduce the issue

I'm trying to add rows to an Excel 2010 table that has no visible header rows.
However, POI-3.10 throws an exception on safe (see below).

Given:
======
an Excel 2010 file that contains a table with no visible header rows

Description:
============
New rows are added to the table and the table is expanded before safe. POI
tries to update the table headers in
org.apache.poi.xssf.usermodel.XSSFTable#updateHeaders but fails to take into
account that there are no visible table headers. 

If I use the debugger to set the value of row in XSSFTable line 295 to null,
the exception can be avoided and POI produces a file that opens in Excel
without errors. I guess adding a condition that checks for number of visible
header rows should help. 

Please note:
This exception does not occur if the table has headers. It also does not occur
if there is no other content in the file (thus the x in column C).

Demo program to reproduce (please adjust paths before running):
===============================================================
public class Demo
{
    public static void main(String[] args) throws IOException,
InvalidFormatException
    {
        final Workbook workbook = WorkbookFactory.create(new
File("Tabelle.xlsx"));
        final XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(0);

        // add some contents to table so that the table will need expansion
        Row row = sheet.getRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("demo1");
        cell = row.createCell(1);
        cell.setCellValue("demo2");
        cell = row.createCell(2);
        cell.setCellValue("demo3");

        row = sheet.getRow(1);
        cell = row.createCell(0);
        cell.setCellValue("demo1");
        cell = row.createCell(1);
        cell.setCellValue("demo2");
        cell = row.createCell(2);
        cell.setCellValue("demo3");

        // expand table
        XSSFTable table = sheet.getTables().get(0);
        final CellReference startRef = table.getStartCellReference();
        final CellReference endRef = table.getEndCellReference();
        table.getCTTable().setRef(new CellRangeAddress(startRef.getRow(), 1,
startRef.getCol(), endRef.getCol()).formatAsString());


        FileOutputStream stream = new FileOutputStream("e:\\output.xlsx");
        workbook.write(stream);
        stream.close();
    }

}

Excpetion:
==========
Exception in thread "main"
org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
    at
org.apache.xmlbeans.impl.values.XmlObjectBase.check_orphaned(XmlObjectBase.java:1213)
    at
org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTCellImpl.getF(Unknown
Source)
    at org.apache.poi.xssf.usermodel.XSSFCell.getCellType(XSSFCell.java:529)
    at
org.apache.poi.xssf.usermodel.XSSFCell.getRichStringCellValue(XSSFCell.java:264)
    at
org.apache.poi.xssf.usermodel.XSSFCell.getStringCellValue(XSSFCell.java:251)
    at
org.apache.poi.xssf.usermodel.XSSFTable.updateHeaders(XSSFTable.java:301)
    at org.apache.poi.xssf.usermodel.XSSFTable.writeTo(XSSFTable.java:84)
    at org.apache.poi.xssf.usermodel.XSSFTable.commit(XSSFTable.java:95)
    at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:322)
    at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:326)
    at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:326)
    at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:173)
    at Demo.main(PoiBug.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to