diff --git a/Demo/Program.cs b/Demo/Program.cs index 2ea88b38c..477461f09 100644 --- a/Demo/Program.cs +++ b/Demo/Program.cs @@ -52,6 +52,7 @@ static void Main(string[] args) Demo11(); Demo12(); Demo13(); + Demo14(); /* ### PERFORMANCE TESTS ### */ // # Use tests in this section to test the performance of NanoXLSX @@ -576,6 +577,13 @@ private static void Demo13() wb2.Save(); // Save the workbook } + private static void Demo14() + { + Workbook wb = new Workbook("demo14fix.xlsx", "semicolon test"); // Create a new workbook + wb.CurrentWorksheet.AddCellFormula("IF(1=1;42;0)", "A1"); + wb.Save(); + } + #region Utils /// /// Prints information about the passed cell diff --git a/NanoXLSX/LowLevel/XlsxWriter.cs b/NanoXLSX/LowLevel/XlsxWriter.cs index 8d0f81fb3..356b32087 100644 --- a/NanoXLSX/LowLevel/XlsxWriter.cs +++ b/NanoXLSX/LowLevel/XlsxWriter.cs @@ -940,7 +940,7 @@ private string CreateRowString(DynamicRow dynamicRow, Worksheet worksheet) sb.Append(""); if (item.DataType == Cell.CellType.FORMULA) { - sb.Append("").Append(EscapeXmlChars(item.Value.ToString())).Append(""); + sb.Append("").Append(EscapeXmlChars(item.Value.ToString(), sanitizeSemicolon: true)).Append(""); } else { @@ -1469,7 +1469,7 @@ private List GetSortedSheetData(Worksheet sheet) /// Input string to process /// Escaped string /// Note: The XML specs allow characters up to the character value of 0x10FFFF. However, the C# char range is only up to 0xFFFF. NanoXLSX will neglect all values above this level in the sanitizing check. Illegal characters like 0x1 will be replaced with a white space (0x20) - public static string EscapeXmlChars(string input) + public static string EscapeXmlChars(string input, bool sanitizeSemicolon = false) { if (input == null) { return ""; } int len = input.Length; @@ -1499,6 +1499,11 @@ public static string EscapeXmlChars(string input) illegalCharacters.Add(i); characterTypes.Add(3); } + else if (input[i] == 0x3B && sanitizeSemicolon) + { + illegalCharacters.Add(i); + characterTypes.Add(4); + } } if (illegalCharacters.Count == 0) { @@ -1527,6 +1532,10 @@ public static string EscapeXmlChars(string input) { sb.Append("&"); } + else if (characterTypes[i] == 4 && sanitizeSemicolon) // replace ; + { + sb.Append(","); + } lastIndex = illegalCharacters[i] + 1; } sb.Append(input.Substring(lastIndex));