From 9e62269eef793b81fe9939ec3bd938fa27958df4 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Tue, 20 Aug 2013 18:44:44 +0000 Subject: [PATCH] Bug 52233: try to fix this without breaking the format of xlsx-files. The set to null is necessary to not have an empty 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 --- .../apache/poi/xssf/usermodel/XSSFSheet.java | 10 ++++++++- .../poi/xssf/usermodel/TestXSSFWorkbook.java | 22 ++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index f494c6712..0af15ead3 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -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 rowIterator() { + @SuppressWarnings("unchecked") + public Iterator rowIterator() { return (Iterator)(Iterator) _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 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(); + } } /** diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index 8f583efbc..96eaef571 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -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 } }