Skip to content
Open
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
13 changes: 9 additions & 4 deletions src/MiniExcel.OpenXml/Helpers/XmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
internal static partial class XmlHelper
{
#if NET
[GeneratedRegex("_(x[\\dA-Fa-f]{4})_", RegexOptions.Compiled)] private static partial Regex X4LRegexImpl();
[GeneratedRegex("_(?=x[\\dA-Fa-f]{4}_)", RegexOptions.Compiled)] private static partial Regex X4LRegexImpl();
private static readonly Regex X4LRegex = X4LRegexImpl();

[GeneratedRegex("_(X[\\dA-Fa-f]{4})_", RegexOptions.Compiled)] private static partial Regex UppercaseX4LRegexImpl();
private static readonly Regex UppercaseX4LRegex = UppercaseX4LRegexImpl();
#else
private static readonly Regex X4LRegex = new("_(x[\\dA-Fa-f]{4})_", RegexOptions.Compiled);
private static readonly Regex X4LRegex = new("_(?=x[\\dA-Fa-f]{4}_)", RegexOptions.Compiled);
private static readonly Regex UppercaseX4LRegex = new("_(X[\\dA-Fa-f]{4})_", RegexOptions.Compiled);
#endif

Expand All @@ -19,12 +19,17 @@ internal static partial class XmlHelper
if (encodeStr is null)
return null;

encodeStr = X4LRegex.Replace(encodeStr, "_x005F_$1_");
// Escape the underscore of every _xHHHH_, also when two of them
// share one (_x0041_x0042_), so Excel shows them as written
encodeStr = X4LRegex.Replace(encodeStr, "_x005F_");

var sb = new StringBuilder(encodeStr.Length);
foreach (var ch in encodeStr)
{
if (XmlConvert.IsXmlChar(ch))
// XML parsers read a raw CR (or CRLF) as LF, so write CR the way Excel does
if (ch == '\r')
sb.Append("_x000D_");
else if (XmlConvert.IsXmlChar(ch))
sb.Append(ch);
else
sb.Append(XmlConvert.EncodeName(ch.ToString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public async Task Issue149()
for (int i = 0; i < chars.Length; i++)
{
_output.WriteLine($"{i}, {chars[i]}, {rows2[i]}");
if (i is not (9 or 10 or 13))
if (i is not (9 or 10))
Assert.Equal(strings[i], rows2[i]);
}

Expand All @@ -394,7 +394,7 @@ public async Task Issue149()
for (int i = 0; i < chars.Length; i++)
{
_output.WriteLine($"{i}, {chars[i]}, {rows[i]}");
if (i is not (13 or 9 or 10))
if (i is not (9 or 10))
Assert.Equal(strings[i], rows[i]);
}
}
Expand Down
32 changes: 29 additions & 3 deletions tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ public void Issue149()
for (int i = 0; i < chars.Length; i++)
{
_output.WriteLine($"{i}, {chars[i]}, {rows[i]}");
if (i is 13 or 9 or 10)
if (i is 9 or 10)
continue;

Assert.Equal(strings[i], rows[i]);
Expand All @@ -560,7 +560,7 @@ public void Issue149()
for (int i = 0; i < chars.Length; i++)
{
_output.WriteLine($"{i}, {chars[i]}, {rows[i]}");
if (i is 13 or 9 or 10)
if (i is 9 or 10)
continue;

Assert.Equal(strings[i], rows[i]);
Expand Down Expand Up @@ -1718,7 +1718,33 @@ public void TestIssue294NewLine(StringStorageMode mode)

var part = mode == StringStorageMode.Shared ? "xl/sharedStrings.xml" : "xl/worksheets/sheet1.xml";
var xml = SheetHelper.GetZipFileContent(path.ToString(), part);
Assert.Contains($"<x:t xml:space=\"preserve\">{name}</x:t>", xml);
// CR is written as _x000D_, and GetZipFileContent writes the remaining \n as Environment.NewLine
Assert.Contains($"<x:t xml:space=\"preserve\">{name.Replace("\r", "_x000D_").Replace("\n", Environment.NewLine)}</x:t>", xml);
}

/// A carriage return, and _xHHHH_ sequences that share an underscore, are written so that they read back as exported
[Theory]
[InlineData("a\r\nb", "a_x000D_\nb", StringStorageMode.Inline)]
[InlineData("a\r\nb", "a_x000D_\nb", StringStorageMode.Shared)]
[InlineData("a\rb", "a_x000D_b", StringStorageMode.Inline)]
[InlineData("a\rb", "a_x000D_b", StringStorageMode.Shared)]
[InlineData("_x0041_x0042_", "_x005F_x0041_x005F_x0042_", StringStorageMode.Inline)]
[InlineData("_x0041_x0042_", "_x005F_x0041_x005F_x0042_", StringStorageMode.Shared)]
[InlineData("_x0041_", "_x005F_x0041_", StringStorageMode.Inline)]
[InlineData("_x0041_", "_x005F_x0041_", StringStorageMode.Shared)]
public void TestExportCarriageReturnAndEscapedUnderscores(string name, string encoded, StringStorageMode mode)
{
using var path = AutoDeletingPath.Create();
var value = new[] { new { Name = name } };
_excelExporter.Export(path.ToString(), value, configuration: new OpenXmlConfiguration { StringStorageMode = mode });

var part = mode == StringStorageMode.Shared ? "xl/sharedStrings.xml" : "xl/worksheets/sheet1.xml";
using var archive = ZipFile.OpenRead(path.ToString());
using var reader = new StreamReader(archive.GetEntry(part)!.Open());
Assert.Contains($">{encoded}</x:t>", reader.ReadToEnd());

var rows = _excelImporter.Query(path.ToString(), true).ToList();
Assert.Equal(name, (string)rows[0].Name);
}

/// Prefix and suffix blank space are hidden by Excel after FillTemplate - https://github.com/mini-software/MiniExcel/issues/295
Expand Down
Loading