]> source.dussan.org Git - poi.git/commitdiff
Bug 52233: try to fix this without breaking the format of xlsx-files.
authorDominik Stadler <centic@apache.org>
Tue, 20 Aug 2013 18:44:44 +0000 (18:44 +0000)
committerDominik Stadler <centic@apache.org>
Tue, 20 Aug 2013 18:44:44 +0000 (18:44 +0000)
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

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java

index f494c67122851dcb4b73ea7be4dc5aa9e9f307b1..0af15ead35f2b5a107d79c48d9c826f968d11f4b 100644 (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();
+       }
     }
 
     /**
index 8f583efbc5b511b739104f96ef250e58dd345774..96eaef5719ccd250e7dd57d2b5dedc8123de33f2 100644 (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
        }
 }