Author: yegor
Date: Sun Dec 2 12:28:32 2012
New Revision: 1416166
URL: http://svn.apache.org/viewvc?rev=1416166&view=rev
Log:
sync table headers with worksheet on save
Modified:
poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
URL:
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java?rev=1416166&r1=1416165&r2=1416166&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
(original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java Sun
Dec 2 12:28:32 2012
@@ -47,7 +47,7 @@ import org.openxmlformats.schemas.spread
* @author Roberto Manicardi
*/
public class XSSFTable extends POIXMLDocumentPart {
-
+
private CTTable ctTable;
private List<XSSFXmlColumnPr> xmlColumnPr;
private CellReference startCellReference;
@@ -81,7 +81,9 @@ public class XSSFTable extends POIXMLDoc
}
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 POIXMLDoc
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 POIXMLDoc
}
return rowCount;
}
+
+ /**
+ * Synchronize table headers with cell values in the parent sheet.
+ * Headers <em>must</em> 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());
+ }
+ }
+
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]