From: Dominik Stadler Date: Mon, 3 Feb 2014 19:19:14 +0000 (+0000) Subject: Bug 55927: Handle date types when exporting to XML X-Git-Tag: REL_3_11_BETA1~280 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ddaab16081defdb6f8ab8aba66855e482314e455;p=poi.git 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 --- 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 aa68c26384..807a584181 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 cb70b358f8..6bcb71943f 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 0000000000..e3d5a52cc0 Binary files /dev/null and b/test-data/spreadsheet/55927.xlsx differ