Apache POI XSSFTable update AreaReference

156 Views Asked by At

Working with Apache POI to write some data from DB to Excel file template. The template contains the header and no rows. After adding all the rows properly when I try to update the table area as below

AreaReference newArea = new AreaReference("A1:Bxxx", SpreadsheetVersion.EXCEL2007);//Update xxx right bottom based on rows added.
        XSSFTable table = sheet.getTables().get(0);
        log.error(table.getArea().formatAsString());//Existing area is going to  print(A1:BJ1) // No rows only table header in file
        table.setArea(newArea);//Set new area
        log.error(table.getArea().formatAsString());//Will print new area (A1:Bxxx)

The file can not open in Excel any more. Error as below:

</summary><removedParts><removedPart>Removed Part: /xl/queryTables/queryTable1.xml part.  (External data range)</removedPart>

File can be recovered but it's not anymore a table just rows and headers.

If I remove the lines of code the file is open properly but the rows added are not recognize as table rows.

What I have tried so far it's not fixing this issue.

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.3</version>
        </dependency>

If I try to apply the same area in Excel it works properly.

A dirty solution would be to resize the table in the template file and make the table enough area to refer to a large number of rows so the number of rows that we are going to add will never exceed that. But that will add unnecessary rows to the table. Example A1:B500.

1

There are 1 best solutions below

0
java programming On

The issue was with the Excel table template. Fixed thank you! Thank you @Alex for the gold bullet. After your comment, I just re-created a table and it was working. Looks like the existing template I was using contained some legacy data.