From 503ce1e6c4083ffcc467911c93c4c65274b476ce Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Mon, 3 Feb 2014 19:19:14 +0000 Subject: [PATCH] Bug 55927: Handle date types when exporting to XML git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1564011 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xssf/extractor/XSSFExportToXml.java | 13 ++++++- .../xssf/extractor/TestXSSFExportToXML.java | 33 ++++++++++++++++-- test-data/spreadsheet/55927.xlsx | Bin 0 -> 11844 bytes 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 test-data/spreadsheet/55927.xlsx diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java index aa68c2638..807a58418 100644 --- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java +++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java @@ -19,6 +19,8 @@ package org.apache.poi.xssf.extractor; import java.io.IOException; import java.io.OutputStream; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; @@ -41,6 +43,7 @@ import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFMap; import org.apache.poi.xssf.usermodel.XSSFRow; @@ -281,7 +284,14 @@ public class XSSFExportToXml implements Comparator{ case XSSFCell.CELL_TYPE_BOOLEAN: value += cell.getBooleanCellValue(); break; case XSSFCell.CELL_TYPE_ERROR: value = cell.getErrorCellString(); break; case XSSFCell.CELL_TYPE_FORMULA: value = cell.getStringCellValue(); break; - case XSSFCell.CELL_TYPE_NUMERIC: value += cell.getRawValue(); break; + case XSSFCell.CELL_TYPE_NUMERIC: + if (DateUtil.isCellDateFormatted(cell)) { + DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + value += sdf.format(cell.getDateCellValue()); + } else { + value += cell.getRawValue(); + } + break; default: ; } @@ -383,6 +393,7 @@ public class XSSFExportToXml implements Comparator{ * Compares two xpaths to define an ordering according to the XML Schema * */ + @Override public int compare(String leftXpath, String rightXpath) { int result = 0; diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java index cb70b358f..6bcb71943 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java @@ -52,7 +52,7 @@ public final class TestXSSFExportToXML extends TestCase { String xml = os.toString("UTF-8"); assertNotNull(xml); - assertTrue(!xml.equals("")); + assertFalse(xml.equals("")); String docente = xml.split("")[1].split("")[0].trim(); String nome = xml.split("")[1].split("")[0].trim(); @@ -95,7 +95,7 @@ public final class TestXSSFExportToXML extends TestCase { String xml = os.toString("UTF-8"); assertNotNull(xml); - assertTrue(!xml.equals("")); + assertFalse(xml.equals("")); String docente = xml.split("")[1].split("")[0].trim(); String nome = xml.split("")[1].split("")[0].trim(); @@ -199,7 +199,7 @@ public final class TestXSSFExportToXML extends TestCase { String xmlData = os.toString("UTF-8"); assertNotNull(xmlData); - assertTrue(!xmlData.equals("")); + assertFalse(xmlData.equals("")); String a = xmlData.split("")[1].split("")[0].trim(); String b = a.split("")[1].split("")[0].trim(); @@ -214,4 +214,31 @@ public final class TestXSSFExportToXML extends TestCase { assertEquals("19", chf); } } + + public void testFormulaCells_Bugzilla_55927() throws Exception { + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55927.xlsx"); + + for (POIXMLDocumentPart p : wb.getRelations()) { + + if (!(p instanceof MapInfo)) { + continue; + } + MapInfo mapInfo = (MapInfo) p; + + XSSFMap map = mapInfo.getXSSFMapById(1); + + assertNotNull("XSSFMap is null", map); + + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + String xmlData = os.toString("UTF-8"); + + assertNotNull(xmlData); + assertFalse(xmlData.equals("")); + + String date = xmlData.split("")[1].split("")[0].trim(); + assertEquals("2012-01-13", date); + } + } } diff --git a/test-data/spreadsheet/55927.xlsx b/test-data/spreadsheet/55927.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..e3d5a52cc0b44ff6edb2026614085de56f2930ba GIT binary patch literal 11844 zcmeHtRa6~o*6qgK-CY8~A-D&EZQNZ01b2tv8r5SdV|iB_4Ix2Vi!hpf z(Ky$ER=bTBk4KC|2#X6Mdpuvj>@A`wsi6_=rou1)dOyZmr$8Goa81U;Q-7ZZwMFCV{v#NO%~&~_=DPs%Sv6g2<$v*Oohfs^hXFmku43SUuxg33CwI-}hzAGAq7r z9p+mv3RnXgUmJAxpeXD8y_ewYP5?j&?kfZUfDeELbG2mp!%@5s!Y%G4JxPLkg z7%0zy{`;T3wZsj)>taR~y9#{tPxj)kn+QTDH&ueya8w#cLH%(pY$<6r8T{1tg+Ugg zb1B&plFk2!-`2FQxv~v|WV5kWTqH4{^sAXxU18#hvum3NWT$Y7qSJJAfG@6d&*9Kz z0uR(xY~}7dJC#5rQiGat}ilV&<8~`ZN;I_C;4Z9Pu_md8nSJLyrt^$tsB+`T{FCh4u+t&&H zF3K)>irN~1Y{Cu{Wl=ze0kWSzTTVo*g4`Eo)V>wqJu+~mKno8NmLODtf~og)z)sV$ zy66m})z0dI#{#(Fd1q1c{=?P&QoB|~WCC^8YD*1u!6|Jnq>Oa|>s@@CP3FH(@kB$wJ?5w4JFMWc_!R5H@Jio3N8OmSpyv^@I>no&qE;zJ@5c2 zHexX)RXCGfA@znQW=n;Sgv6H1%KBdbcG+Sbr(VVHG~=qp-I41{jJh zz$b+zROskeV71ogYq57HZhkP_HkFzP2Q(TuDyx;b7kJ;|f4I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