]> source.dussan.org Git - poi.git/commitdiff
Bug #52369 - XSSFExcelExtractor should format numeric cells based on the format strin...
authorNick Burch <nick@apache.org>
Tue, 20 Dec 2011 05:54:18 +0000 (05:54 +0000)
committerNick Burch <nick@apache.org>
Tue, 20 Dec 2011 05:54:18 +0000 (05:54 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1221108 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java
src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java

index 40fe9bb2efd43bcc17c2c67e0734b116c81208db..6f49c59b98f5c40bd34a116cb52dee0ef563742e 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta6" date="2012-??-??">
+           <action dev="poi-developers" type="fix">52369 - XSSFExcelExtractor should format numeric cells based on the format strings applied to them</action>
            <action dev="poi-developers" type="fix">52369 - Event based XSSF parsing should handle formatting of formula values in XSSFSheetXMLHandler</action>
            <action dev="poi-developers" type="fix">52348 - Avoid exception when creating cell style in a workbook that has an empty xf table</action>
            <action dev="poi-developers" type="fix">52219 - fixed XSSFSimpleShape to set rich text attributes from XSSFRichtextString</action>
index 5d7e2dcaa736485892b5f5575986a4238bcc9f54..0c470f8e6cc1ddebc910a3c7cad473d4c0996a49 100644 (file)
@@ -254,7 +254,7 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
                             thisStr = fv;
                          }
                       } else {
-                         // No formatter supplied, just do raw value in all cases
+                         // No formating applied, just do raw value in all cases
                          thisStr = fv;
                       }
                    }
index c606471f86d7f7146269f48caccbb7ffedd2ae0c..5e687b5d798c17668ac905db5e2898f6162f86a2 100644 (file)
@@ -18,13 +18,16 @@ package org.apache.poi.xssf.extractor;
 
 import java.io.IOException;
 import java.util.Iterator;
+import java.util.Locale;
 
 import org.apache.poi.POIXMLTextExtractor;
 import org.apache.poi.hssf.extractor.ExcelExtractor;
 import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
 import org.apache.poi.openxml4j.opc.OPCPackage;
 import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
 import org.apache.poi.ss.usermodel.Comment;
+import org.apache.poi.ss.usermodel.DataFormatter;
 import org.apache.poi.ss.usermodel.HeaderFooter;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.xssf.usermodel.XSSFCell;
@@ -43,6 +46,7 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
       XSSFRelation.MACROS_WORKBOOK
    };
    
+   private Locale locale;
        private XSSFWorkbook workbook;
        private boolean includeSheetNames = true;
        private boolean formulasNotResults = false;
@@ -96,14 +100,28 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
     public void setIncludeHeadersFooters(boolean includeHeadersFooters) {
         this.includeHeadersFooters = includeHeadersFooters;
     }
-
-       /**
-        * Retreives the text contents of the file
-        */
-       public String getText() {
-               StringBuffer text = new StringBuffer();
-
-               for(int i=0; i<workbook.getNumberOfSheets(); i++) {
+    /**
+     * What Locale should be used for formatting numbers (based
+     *  on the styles applied to the cells)
+     */
+    public void setLocale(Locale locale) {
+       this.locale = locale;
+    }
+    
+
+   /**
+    * Retreives the text contents of the file
+    */
+   public String getText() {
+      DataFormatter formatter;
+      if(locale == null) {
+         formatter = new DataFormatter();
+      } else  {
+         formatter = new DataFormatter(locale);
+      }
+      
+      StringBuffer text = new StringBuffer();
+      for(int i=0; i<workbook.getNumberOfSheets(); i++) {
                        XSSFSheet sheet = workbook.getSheetAt(i);
                        if(includeSheetNames) {
                                text.append(workbook.getSheetName(i)).append("\n");
@@ -129,13 +147,20 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
                                        Cell cell = ri.next();
 
                                        // Is it a formula one?
-                                       if(cell.getCellType() == Cell.CELL_TYPE_FORMULA && formulasNotResults) {
-                                               text.append(cell.getCellFormula());
+                                       if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
+                                          if (formulasNotResults) {
+                                             text.append(cell.getCellFormula());
+                                          } else {
+                                             if (cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) {
+                                                handleStringCell(text, cell);
+                                             } else {
+                                                handleNonStringCell(text, cell, formatter);
+                                             }
+                                          }
                                        } else if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
-                                               text.append(cell.getRichStringCellValue().getString());
+                  handleStringCell(text, cell);
                                        } else {
-                                               XSSFCell xc = (XSSFCell)cell;
-                                               text.append(xc.getRawValue());
+                  handleNonStringCell(text, cell, formatter);
                                        }
 
                                        // Output the comment, if requested and exists
@@ -169,6 +194,31 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
 
                return text.toString();
        }
+       
+   private void handleStringCell(StringBuffer text, Cell cell) {
+      text.append(cell.getRichStringCellValue().getString());
+   }
+   private void handleNonStringCell(StringBuffer text, Cell cell, DataFormatter formatter) {
+      int type = cell.getCellType();
+      if (type == Cell.CELL_TYPE_FORMULA) {
+         type = cell.getCachedFormulaResultType();
+      }
+
+      if (type == Cell.CELL_TYPE_NUMERIC) {
+         CellStyle cs = cell.getCellStyle();
+
+         if (cs.getDataFormatString() != null) {
+            text.append(formatter.formatRawCellContents(
+                  cell.getNumericCellValue(), cs.getDataFormat(), cs.getDataFormatString()
+            ));
+            return;
+         }
+      }
+
+      // No supported styling applies to this cell
+      XSSFCell xcell = (XSSFCell)cell;
+      text.append( xcell.getRawValue() );
+   }
 
        private String extractHeaderFooter(HeaderFooter hf) {
                return ExcelExtractor._extractHeaderFooter(hf);