]> source.dussan.org Git - poi.git/commitdiff
Bug 55926: Handle numeric formula values when exporting to XML
authorDominik Stadler <centic@apache.org>
Mon, 3 Feb 2014 19:46:41 +0000 (19:46 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 3 Feb 2014 19:46:41 +0000 (19:46 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1564028 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/55926.xlsx [new file with mode: 0644]

index 807a5841814c5dcd6f58622ae7ef990661e56b55..49b095eded2378d80f134793cfd3f900d0fc70d1 100644 (file)
@@ -43,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.Cell;
 import org.apache.poi.ss.usermodel.DateUtil;
 import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFMap;
@@ -283,7 +284,14 @@ public class XSSFExportToXml implements Comparator<String>{
         case XSSFCell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break;
         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_FORMULA:
+           if (cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) {
+               value = cell.getStringCellValue();
+           } else {
+               value += cell.getNumericCellValue();
+           }
+           break;
+        
         case XSSFCell.CELL_TYPE_NUMERIC: 
              if (DateUtil.isCellDateFormatted(cell)) {
                   DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@@ -292,6 +300,7 @@ public class XSSFExportToXml implements Comparator<String>{
                  value += cell.getRawValue();
               }
             break;
+
         default: ;
 
         }
index 6bcb71943f3faa2a85338cee20a375ed7e1e5e71..fbbf1e0eb5df29bef14bbe5be0959704c8b9e0ec 100644 (file)
@@ -241,4 +241,35 @@ public final class TestXSSFExportToXML extends TestCase {
            assertEquals("2012-01-13", date);
        }
    }
+
+   public void testFormulaCells_Bugzilla_55926() throws Exception {
+       XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.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 a = xmlData.split("<A>")[1].split("</A>")[0].trim();
+           String doubleValue = a.split("<DOUBLE>")[1].split("</DOUBLE>")[0].trim();
+           String stringValue = a.split("<STRING>")[1].split("</STRING>")[0].trim();
+           
+           assertEquals("Hello World", stringValue);
+           assertEquals("5.1", doubleValue);
+       }
+   }
 }
diff --git a/test-data/spreadsheet/55926.xlsx b/test-data/spreadsheet/55926.xlsx
new file mode 100644 (file)
index 0000000..23907c1
Binary files /dev/null and b/test-data/spreadsheet/55926.xlsx differ