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;
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");
value += cell.getRawValue();
}
break;
+
default: ;
}
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);
+ }
+ }
}