[bug-64595] SXSSF: Missing quoting of pre-evaluated string values in formula cells causes corrupt files. Thanks to Bastian Isensee

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1879834 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2020-07-13 17:34:09 +00:00
parent abdd50a4d1
commit af83fda6b8
2 changed files with 29 additions and 3 deletions

View File

@ -298,7 +298,7 @@ public class SheetDataWriter implements Closeable {
String value = cell.getStringCellValue(); String value = cell.getStringCellValue();
if(value != null && !value.isEmpty()) { if(value != null && !value.isEmpty()) {
_out.write("<v>"); _out.write("<v>");
_out.write(value); outputQuotedString(value);
_out.write("</v>"); _out.write("</v>");
} }
break; break;
@ -311,7 +311,7 @@ public class SheetDataWriter implements Closeable {
FormulaError error = FormulaError.forInt(cell.getErrorCellValue()); FormulaError error = FormulaError.forInt(cell.getErrorCellValue());
_out.write("><v>"); _out.write("><v>");
_out.write(error.getString()); outputQuotedString(error.getString());
_out.write("</v>"); _out.write("</v>");
break; break;
} }
@ -358,7 +358,7 @@ public class SheetDataWriter implements Closeable {
writeAttribute("t", "e"); writeAttribute("t", "e");
_out.write("><v>"); _out.write("><v>");
_out.write(error.getString()); outputQuotedString(error.getString());
_out.write("</v>"); _out.write("</v>");
break; break;
} }

View File

@ -238,4 +238,30 @@ public final class TestSXSSFBugs extends BaseTestBugzillaIssues {
} }
} }
} }
@Test
public void test64595() throws Exception {
try (Workbook workbook = new SXSSFWorkbook(100)) {
Sheet sheet = workbook.createSheet("RawData");
Row row = sheet.createRow(0);
Cell cell;
cell = row.createCell(0);
cell.setCellValue("Ernie & Bert");
cell = row.createCell(1);
// Set a precalculated formula value containing a special character.
cell.setCellValue("Ernie & Bert are cool!");
cell.setCellFormula("A1 & \" are cool!\"");
// While unfixed reading the workbook would throw a POIXMLException
// since the file was corrupt due to missing quotation.
try (Workbook wbBack = SXSSFITestDataProvider.instance.writeOutAndReadBack(workbook)) {
assertNotNull(wbBack);
cell = wbBack.getSheetAt(0).getRow(0).getCell(1);
assertEquals("Ernie & Bert are cool!", cell.getStringCellValue());
assertEquals("A1 & \" are cool!\"", cell.getCellFormula());
}
}
}
} }