使用 Apache POI 将一些数据从 DB 写入 Excel 文件模板。该模板包含标题,但不包含行。当我尝试如下更新表格区域时正确添加所有行后
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)
该文件无法再在 Excel 中打开。 错误如下:
</summary><removedParts><removedPart>Removed Part: /xl/queryTables/queryTable1.xml part. (External data range)</removedPart>
文件可以恢复,但它不再是一个只有行和标题的表格。
如果我删除代码行,文件将正确打开,但添加的行不会被识别为表行。
到目前为止我所尝试的方法都没有解决这个问题。
<!-- 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>
如果我尝试在 Excel 中应用相同的区域,它可以正常工作。
一个肮脏的解决方案是调整模板文件中表格的大小,并使表格有足够的区域来引用大量行,这样我们要添加的行数就永远不会超过该数量。 但这会向表中添加不必要的行。 示例 A1:B500.
问题出在 Excel 表格模板上。已解决谢谢! 谢谢@Alex 的金子弹。在您发表评论后,我刚刚重新创建了一个表并且它正在工作。看起来我使用的现有模板包含一些遗留数据。