sync table headers with worksheet on save

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1416166 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2012-12-02 12:28:32 +00:00
parent 7adad438bf
commit e469414ef2
1 changed files with 33 additions and 5 deletions

View File

@ -81,6 +81,8 @@ public class XSSFTable extends POIXMLDocumentPart {
} }
public void writeTo(OutputStream out) throws IOException { public void writeTo(OutputStream out) throws IOException {
updateHeaders();
TableDocument doc = TableDocument.Factory.newInstance(); TableDocument doc = TableDocument.Factory.newInstance();
doc.setTable(ctTable); doc.setTable(ctTable);
doc.save(out, DEFAULT_XML_OPTIONS); doc.save(out, DEFAULT_XML_OPTIONS);
@ -234,10 +236,12 @@ public class XSSFTable extends POIXMLDocumentPart {
if(startCellReference==null){ if(startCellReference==null){
String ref = ctTable.getRef(); String ref = ctTable.getRef();
if(ref != null) {
String[] boundaries = ref.split(":"); String[] boundaries = ref.split(":");
String from = boundaries[0]; String from = boundaries[0];
startCellReference = new CellReference(from); startCellReference = new CellReference(from);
} }
}
return startCellReference; return startCellReference;
} }
@ -275,4 +279,28 @@ public class XSSFTable extends POIXMLDocumentPart {
} }
return rowCount; 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());
}
}
}
} }