Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private static <T> void writeCache(Writer w, Map<T, Integer> cache, String name,
*/
void write(Writer w) throws IOException {
w.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><styleSheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">");
writeCache(w, valueFormattings, "numFmts", e -> w.append("<numFmt numFmtId=\"").append(e.getValue()).append("\" formatCode=\"").append(e.getKey()).append("\"/>"));
writeCache(w, valueFormattings, "numFmts", e -> w.append("<numFmt numFmtId=\"").append(e.getValue()).append("\" formatCode=\"").append(XmlEscapeHelper.escape(e.getKey())).append("\"/>"));
writeCache(w, fonts, "fonts", e -> e.getKey().write(w));
writeCache(w, fills, "fills", e -> e.getKey().write(w));
writeCache(w, borders, "borders", e -> e.getKey().write(w));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,4 +757,29 @@ void testForIssue450() throws Exception {
});
}

@Test
void testFormatCodeWithSpecialCharacters() throws Exception {
// Test for GitHub issue #472: Format codes with quotes should be XML-escaped
writeWorkbook(wb -> {
Worksheet ws = wb.newWorksheet("Sheet1");

// ISO 8601 format with literal "T" - this was causing XML parsing errors
ws.value(0, 0, LocalDateTime.of(2024, 1, 9, 14, 30, 0));
ws.style(0, 0).format("yyyy-MM-dd\"T\"HH:mm:ss").set();

// Format with other special XML characters
ws.value(1, 0, 1234.56);
ws.style(1, 0).format("#,##0.00 \"<units>\"").set();

// Format with ampersand
ws.value(2, 0, 100);
ws.style(2, 0).format("\"A&B: \"0").set();

// Format with apostrophe
ws.value(3, 0, 2024);
ws.style(3, 0).format("\"Year '\"0").set();
});
// If we reach here without exception, the XML was valid
}

}