Updated patch from Marc from #55927 - handle date formulas too

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1614696 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2014-07-30 16:10:56 +00:00
parent 27dd2eafe1
commit 7844d4dafb
3 changed files with 12 additions and 6 deletions

View File

@ -292,15 +292,18 @@ public class XSSFExportToXml implements Comparator<String>{
case XSSFCell.CELL_TYPE_FORMULA: case XSSFCell.CELL_TYPE_FORMULA:
if (cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) { if (cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) {
value = cell.getStringCellValue(); value = cell.getStringCellValue();
} else {
if (DateUtil.isCellDateFormatted(cell)) {
value = getFormattedDate(cell);
} else { } else {
value += cell.getNumericCellValue(); value += cell.getNumericCellValue();
} }
}
break; break;
case XSSFCell.CELL_TYPE_NUMERIC: case XSSFCell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) { if (DateUtil.isCellDateFormatted(cell)) {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); value = getFormattedDate(cell);
value += sdf.format(cell.getDateCellValue());
} else { } else {
value += cell.getRawValue(); value += cell.getRawValue();
} }
@ -321,7 +324,10 @@ public class XSSFExportToXml implements Comparator<String>{
return elementName.matches(".*:.*")?elementName.split(":")[1]:elementName; 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) { private Node getNodeByXPath(String xpath,Node rootNode,Document doc,boolean createMultipleInstances) {
String[] xpathTokens = xpath.split("/"); String[] xpathTokens = xpath.split("/");

View File

@ -279,8 +279,8 @@ public final class TestXSSFExportToXML extends TestCase {
assertNotNull(xmlData); assertNotNull(xmlData);
assertFalse(xmlData.equals("")); assertFalse(xmlData.equals(""));
String date = xmlData.split("<DATE>")[1].split("</DATE>")[0].trim(); assertEquals("2012-01-13", xmlData.split("<DATE>")[1].split("</DATE>")[0].trim());
assertEquals("2012-01-13", date); assertEquals("2012-02-16", xmlData.split("<FORMULA_DATE>")[1].split("</FORMULA_DATE>")[0].trim());
parseXML(xmlData); parseXML(xmlData);

Binary file not shown.