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
2 changes: 1 addition & 1 deletion src/MiniExcel.OpenXml/Constants/ExcelXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal static string SharedStrings(Dictionary<string, int> sharedStrings)
{
sb.Append("<x:si><x:t");

if (text.StartsWith(" ") || text.EndsWith(" "))
if (XmlHelper.HasLeadingOrTrailingWhiteSpace(text))
sb.Append(" xml:space=\"preserve\"");

sb.Append($">{XmlHelper.EncodeXml(text)}</x:t></x:si>");
Expand Down
6 changes: 6 additions & 0 deletions src/MiniExcel.OpenXml/Helpers/XmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ internal static partial class XmlHelper
return XmlConvert.DecodeName(decodeStr);
}

/// <summary>
/// Excel drops leading and trailing blanks (spaces, tabs, line breaks) from a &lt;t&gt; without xml:space="preserve".
/// </summary>
public static bool HasLeadingOrTrailingWhiteSpace(string? text)
=> text is { Length: > 0 } && (char.IsWhiteSpace(text[0]) || char.IsWhiteSpace(text[text.Length - 1]));

public static string MinifyXml(string xml) => xml
.Replace("\r", "")
.Replace("\n", "")
Expand Down
2 changes: 1 addition & 1 deletion src/MiniExcel.OpenXml/Writer/OpenXmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ private async Task WriteCellAsync(MiniExcelStreamWriter writer, int rowIndex, in
}

/*Prefix and suffix blank space will lost after SaveAs #294*/
var preserveSpace = cellValue.StartsWith(" ") || cellValue.EndsWith(" ");
var preserveSpace = XmlHelper.HasLeadingOrTrailingWhiteSpace(cellValue);
await writer.WriteAsync(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, columnType: columnType), cancellationToken).ConfigureAwait(false);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,25 @@ public void TestIssue294()
}
}

/// A leading or trailing tab or line break is a blank too, inline and shared - https://github.com/mini-software/MiniExcel/issues/294
[Theory]
[InlineData("\tJack", StringStorageMode.Inline, true)]
[InlineData("Jack\n", StringStorageMode.Inline, true)]
[InlineData("Ja\tck", StringStorageMode.Inline, false)]
[InlineData("\tJack", StringStorageMode.Shared, true)]
[InlineData("Jack\n", StringStorageMode.Shared, true)]
[InlineData("Ja\tck", StringStorageMode.Shared, false)]
public void TestIssue294WhiteSpace(string name, StringStorageMode mode, bool preserved)
{
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";
var xml = SheetHelper.GetZipFileContent(path.ToString(), part);
Assert.Contains(preserved ? $"<x:t xml:space=\"preserve\">{name}</x:t>" : $"<x:t>{name}</x:t>", xml);
}

/// Prefix and suffix blank space are hidden by Excel after FillTemplate - https://github.com/mini-software/MiniExcel/issues/295
[Fact]
public void TestIssue295()
Expand Down
Loading