]> source.dussan.org Git - poi.git/commitdiff
Updated patch from Marc from #55927 - handle date formulas too
authorNick Burch <nick@apache.org>
Wed, 30 Jul 2014 16:10:56 +0000 (16:10 +0000)
committerNick Burch <nick@apache.org>
Wed, 30 Jul 2014 16:10:56 +0000 (16:10 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1614696 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java
src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java
test-data/spreadsheet/55927.xlsx

index edd0665b51da6f53461c00e2dd358c8be85b43b3..e9b75e53650db6a1c2d16d1cd5bbe614f57ac119 100644 (file)
@@ -293,14 +293,17 @@ public class XSSFExportToXml implements Comparator<String>{
            if (cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) {
                value = cell.getStringCellValue();
            } else {
-               value += cell.getNumericCellValue();
+               if (DateUtil.isCellDateFormatted(cell)) {
+                  value = getFormattedDate(cell);
+               } else {
+                  value += cell.getNumericCellValue();
+               }
            }
            break;
         
         case XSSFCell.CELL_TYPE_NUMERIC: 
              if (DateUtil.isCellDateFormatted(cell)) {
-                  DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
-                  value += sdf.format(cell.getDateCellValue());
+                  value = getFormattedDate(cell);
               } else {
                  value += cell.getRawValue();
               }
@@ -321,7 +324,10 @@ public class XSSFExportToXml implements Comparator<String>{
         return elementName.matches(".*:.*")?elementName.split(":")[1]:elementName;
     }
 
-
+    private String getFormattedDate(XSSFCell cell) {
+        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        return sdf.format(cell.getDateCellValue());
+    }
 
     private Node getNodeByXPath(String xpath,Node rootNode,Document doc,boolean createMultipleInstances) {
         String[] xpathTokens = xpath.split("/");
index 3e279426c30e339ab7a75ba8a30928caba99ab4b..9621b91eb47612b0deadfe5baa28a3fdd17247a6 100644 (file)
@@ -279,8 +279,8 @@ public final class TestXSSFExportToXML extends TestCase {
            assertNotNull(xmlData);
            assertFalse(xmlData.equals(""));
            
-           String date = xmlData.split("<DATE>")[1].split("</DATE>")[0].trim();
-           assertEquals("2012-01-13", date);
+           assertEquals("2012-01-13", xmlData.split("<DATE>")[1].split("</DATE>")[0].trim());
+           assertEquals("2012-02-16", xmlData.split("<FORMULA_DATE>")[1].split("</FORMULA_DATE>")[0].trim());
            
            parseXML(xmlData);
            
index e3d5a52cc0b44ff6edb2026614085de56f2930ba..62fd046f521d0ac685f225eb7f4682a99b02a8fe 100644 (file)
Binary files a/test-data/spreadsheet/55927.xlsx and b/test-data/spreadsheet/55927.xlsx differ