]> source.dussan.org Git - poi.git/commitdiff
Bug 56011: Use default style if the cell style attribute is not present
authorDominik Stadler <centic@apache.org>
Sat, 1 Feb 2014 20:54:09 +0000 (20:54 +0000)
committerDominik Stadler <centic@apache.org>
Sat, 1 Feb 2014 20:54:09 +0000 (20:54 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1563470 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java
src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFEventBasedExcelExtractor.java
test-data/spreadsheet/56011.xlsx [new file with mode: 0644]

index 0c470f8e6cc1ddebc910a3c7cad473d4c0996a49..f0f01bb8518c506665dd9a9633ccf31b3f5e325c 100644 (file)
@@ -132,6 +132,7 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
       return false;
    }
    
+   @Override
    public void startElement(String uri, String localName, String name,
                             Attributes attributes) throws SAXException {
 
@@ -207,18 +208,26 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
                nextDataType = xssfDataType.SST_STRING;
            else if ("str".equals(cellType))
                nextDataType = xssfDataType.FORMULA;
-           else if (cellStyleStr != null) {
-              // Number, but almost certainly with a special style or format
-               int styleIndex = Integer.parseInt(cellStyleStr);
-               XSSFCellStyle style = stylesTable.getStyleAt(styleIndex);
-               this.formatIndex = style.getDataFormat();
-               this.formatString = style.getDataFormatString();
-               if (this.formatString == null)
-                   this.formatString = BuiltinFormats.getBuiltinFormat(this.formatIndex);
+           else {
+               // Number, but almost certainly with a special style or format
+               XSSFCellStyle style = null;
+               if (cellStyleStr != null) {
+                   int styleIndex = Integer.parseInt(cellStyleStr);
+                   style = stylesTable.getStyleAt(styleIndex);
+               } else if (stylesTable.getNumCellStyles() > 0) {
+                   style = stylesTable.getStyleAt(0);
+               }
+               if (style != null) {
+                   this.formatIndex = style.getDataFormat();
+                   this.formatString = style.getDataFormatString();
+                   if (this.formatString == null)
+                       this.formatString = BuiltinFormats.getBuiltinFormat(this.formatIndex);
+               }
            }
        }
    }
 
+   @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
        String thisStr = null;
@@ -316,6 +325,7 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
     * Captures characters only if a suitable element is open.
     * Originally was just "v"; extended for inlineStr also.
     */
+   @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if (vIsOpen) {
index 1b0e6c4797a3c300def1f724528690be3402d5da..0fb28ef9dcfb6bb5844df294850f3ec8e246240f 100644 (file)
@@ -17,7 +17,6 @@
 
 package org.apache.poi.xssf.extractor;
 
-import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -26,11 +25,7 @@ import junit.framework.TestCase;
 import org.apache.poi.POITextExtractor;
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.hssf.extractor.ExcelExtractor;
-import org.apache.poi.openxml4j.opc.OPCPackage;
 import org.apache.poi.xssf.XSSFTestDataSamples;
-import org.apache.poi.xssf.eventusermodel.XSSFReader;
-import org.apache.poi.xssf.usermodel.XSSFShape;
-import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
 
 /**
  * Tests for {@link XSSFEventBasedExcelExtractor}
@@ -187,4 +182,31 @@ public class TestXSSFEventBasedExcelExtractor extends TestCase {
            assertTrue(text.indexOf("Line 3") > -1);
 
     }
+
+    /**
+     * Test that we return the same output for unstyled numbers as the
+     * non-event-based XSSFExcelExtractor.
+     */
+    public void testUnstyledNumbersComparedToNonEventBasedExtractor()
+            throws Exception {
+
+        String expectedOutput = "Sheet1\n99.99\n";
+
+        XSSFExcelExtractor extractor = new XSSFExcelExtractor(
+                XSSFTestDataSamples.openSampleWorkbook("56011.xlsx"));
+        try {
+            assertEquals(expectedOutput, extractor.getText().replace(",", "."));
+        } finally {
+            extractor.close();
+        }
+
+        XSSFEventBasedExcelExtractor fixture =
+                new XSSFEventBasedExcelExtractor(
+                        XSSFTestDataSamples.openSamplePackage("56011.xlsx"));
+        try {
+            assertEquals(expectedOutput, fixture.getText().replace(",", "."));
+        } finally {
+            fixture.close();
+        }
+    }
 }
diff --git a/test-data/spreadsheet/56011.xlsx b/test-data/spreadsheet/56011.xlsx
new file mode 100644 (file)
index 0000000..7b5f70e
Binary files /dev/null and b/test-data/spreadsheet/56011.xlsx differ