From e469414ef211f27588943d1842a84523dbeb44e5 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Sun, 2 Dec 2012 12:28:32 +0000 Subject: [PATCH] sync table headers with worksheet on save git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1416166 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xssf/usermodel/XSSFTable.java | 38 ++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java index 851aeaa3a..4f71c2d8c 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java @@ -47,7 +47,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.TableDocument; * @author Roberto Manicardi */ public class XSSFTable extends POIXMLDocumentPart { - + private CTTable ctTable; private List xmlColumnPr; private CellReference startCellReference; @@ -81,7 +81,9 @@ public class XSSFTable extends POIXMLDocumentPart { } public void writeTo(OutputStream out) throws IOException { - TableDocument doc = TableDocument.Factory.newInstance(); + updateHeaders(); + + TableDocument doc = TableDocument.Factory.newInstance(); doc.setTable(ctTable); doc.save(out, DEFAULT_XML_OPTIONS); } @@ -234,9 +236,11 @@ public class XSSFTable extends POIXMLDocumentPart { if(startCellReference==null){ String ref = ctTable.getRef(); - String[] boundaries = ref.split(":"); - String from = boundaries[0]; - startCellReference = new CellReference(from); + if(ref != null) { + String[] boundaries = ref.split(":"); + String from = boundaries[0]; + startCellReference = new CellReference(from); + } } return startCellReference; } @@ -275,4 +279,28 @@ public class XSSFTable extends POIXMLDocumentPart { } return rowCount; } + + /** + * Synchronize table headers with cell values in the parent sheet. + * Headers must be in sync, otherwise Excel will display a + * "Found unreadable content" message on startup. + */ + public void updateHeaders(){ + XSSFSheet sheet = (XSSFSheet)getParent(); + CellReference ref = getStartCellReference(); + if(ref == null) return; + + int headerRow = ref.getRow(); + int firstHeaderColumn = ref.getCol(); + XSSFRow row = sheet.getRow(headerRow); + + if(row != null) for(CTTableColumn col : getCTTable().getTableColumns().getTableColumnList()){ + int colIdx = (int)col.getId() - 1 + firstHeaderColumn; + XSSFCell cell = row.getCell(colIdx); + if(cell != null) { + col.setName(cell.getStringCellValue()); + } + } + + } }