Bug 52233: try to fix this without breaking the format of xlsx-files.

The set to null is necessary to not have an empty <cols/> element in the
xlsx, however later on stuff breaks if no colsArray is availalbe,
therefore we now re-create the empty cols array if we did remove it
before.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1515916 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2013-08-20 18:44:44 +00:00
parent 4a675204cb
commit 9e62269eef
2 changed files with 26 additions and 6 deletions

View File

@ -1554,7 +1554,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
* be the third row if say for instance the second row is undefined.
* Call getRowNum() on each row if you care which one it is.
*/
public Iterator<Row> rowIterator() {
@SuppressWarnings("unchecked")
public Iterator<Row> rowIterator() {
return (Iterator<Row>)(Iterator<? extends Row>) _rows.values().iterator();
}
@ -2689,9 +2690,11 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
}
protected void write(OutputStream out) throws IOException {
boolean setToNull = false;
if(worksheet.sizeOfColsArray() == 1) {
CTCols col = worksheet.getColsArray(0);
if(col.sizeOfColArray() == 0) {
setToNull = true;
// this is necessary so that we do not write an empty <cols/> item into the sheet-xml in the xlsx-file
// Excel complains about a corrupted file if this shows up there!
worksheet.setColsArray(null);
@ -2728,6 +2731,11 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
xmlOptions.setSaveSuggestedPrefixes(map);
worksheet.save(out, xmlOptions);
// Bug 52233: Ensure that we have a col-array even if write() removed it
if(setToNull) {
worksheet.addNewCols();
}
}
/**

View File

@ -462,15 +462,20 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
sh.getCTWorksheet().getSheetPr().getTabColor().getIndexed());
}
// TODO: disabled as the fix for this had severe side-effects
public void doNotRuntestColumnWidthPOI52233() throws Exception {
public void testColumnWidthPOI52233() throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("hello world");
assertEquals("hello world", workbook.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
assertEquals(2048, workbook.getSheetAt(0).getColumnWidth(0)); // <-works
sheet = workbook.createSheet();
sheet.setColumnWidth(4, 5000);
sheet.setColumnWidth(5, 5000);
sheet.groupColumn((short) 4, (short) 5);
accessWorkbook(workbook);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
@ -479,7 +484,14 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
stream.close();
}
accessWorkbook(workbook);
}
private void accessWorkbook(XSSFWorkbook workbook) {
workbook.getSheetAt(1).setColumnGroupCollapsed(4, true);
workbook.getSheetAt(1).setColumnGroupCollapsed(4, false);
assertEquals("hello world", workbook.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
assertEquals(2048, workbook.getSheetAt(0).getColumnWidth(0)); // <- did throw IndexOutOfBoundsException before fixing the bug
assertEquals(2048, workbook.getSheetAt(0).getColumnWidth(0)); // <-works
}
}