Skip to content

Commit

Permalink
Force dot as decimal separator column width, fixes bug triggered by T…
Browse files Browse the repository at this point in the history
…estIssueI4ZYUU
  • Loading branch information
Jef Van den Brandt authored and Jef Van den Brandt committed Mar 21, 2024
1 parent 76fa1d7 commit b95e3f9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
24 changes: 12 additions & 12 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private void GenerateSheetByEnumerable(MiniExcelStreamWriter writer, IEnumerable
{
mode = "IDictionary";
props = GetDictionaryColumnInfo(null, dic);
//maxColumnIndex = dic.Keys.Count;
//maxColumnIndex = dic.Keys.Count;
maxColumnIndex = props.Count; // why not using keys, because ignore attribute ![image](https://user-images.githubusercontent.com/12729184/163686902-286abb70-877b-4e84-bd3b-001ad339a84a.png)
}
else
Expand All @@ -190,7 +190,7 @@ private void GenerateSheetByEnumerable(MiniExcelStreamWriter writer, IEnumerable
writer.Write($@"<?xml version=""1.0"" encoding=""utf-8""?><x:worksheet xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" xmlns:x=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"" >");

long dimensionWritePosition = 0;

// We can write the dimensions directly if the row count is known
if (_configuration.FastMode && rowCount == null)
{
Expand Down Expand Up @@ -328,7 +328,7 @@ private void SetDictionaryColumnInfo(List<ExcelColumnInfo> _props, object key)
p.ExcelColumnName = key?.ToString();
p.Key = key;
// TODO:Dictionary value type is not fiexed
//var _t =
//var _t =
//var gt = Nullable.GetUnderlyingType(p.PropertyType);
var isIgnore = false;
if (_configuration.DynamicColumns != null && _configuration.DynamicColumns.Length > 0)
Expand Down Expand Up @@ -449,7 +449,7 @@ private void WriteCell(MiniExcelStreamWriter writer, int rowIndex, int cellIndex
Type type = null;
if (p == null || p.Key != null)
{
// TODO: need to optimize
// TODO: need to optimize
// Dictionary need to check type every time, so it's slow..
type = value.GetType();
type = Nullable.GetUnderlyingType(type) ?? type;
Expand Down Expand Up @@ -655,7 +655,7 @@ private void GenerateSheetByIDataReader(MiniExcelStreamWriter writer, IDataReade
maxColumnIndex = props.Count;

WriteColumnsWidths(writer, props);

writer.Write("<x:sheetData>");
int fieldCount = reader.FieldCount;
if (_printHeader)
Expand Down Expand Up @@ -701,9 +701,9 @@ private ExcelColumnInfo GetColumnInfosFromDynamicConfiguration(string columnName
Key = columnName
};

if (_configuration.DynamicColumns == null || _configuration.DynamicColumns.Length <= 0)
if (_configuration.DynamicColumns == null || _configuration.DynamicColumns.Length <= 0)
return prop;

var dynamicColumn = _configuration.DynamicColumns.SingleOrDefault(_ => _.Key == columnName);
if (dynamicColumn == null || dynamicColumn.Ignore)
{
Expand Down Expand Up @@ -760,12 +760,12 @@ private static void WriteColumnsWidths(MiniExcelStreamWriter writer, IEnumerable
foreach (var p in ecwProps)
{
writer.Write(
$@"<x:col min=""{p.ExcelColumnIndex + 1}"" max=""{p.ExcelColumnIndex + 1}"" width=""{p.ExcelColumnWidth}"" customWidth=""1"" />");
$@"<x:col min=""{p.ExcelColumnIndex + 1}"" max=""{p.ExcelColumnIndex + 1}"" width=""{p.ExcelColumnWidth?.ToString(CultureInfo.InvariantCulture)}"" customWidth=""1"" />");
}

writer.Write($@"</x:cols>");
}

private static void WriteC(MiniExcelStreamWriter writer, string r, string columnName)
{
writer.Write($"<x:c r=\"{r}\" t=\"str\" s=\"1\">");
Expand All @@ -784,7 +784,7 @@ private void GenerateEndXml()
}
}

// styles.xml
// styles.xml
{
var styleXml = string.Empty;

Expand Down Expand Up @@ -878,7 +878,7 @@ private void GenerateEndXml()
}
workbookRelsXml.AppendLine($@"<Relationship Type=""http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"" Target=""/{s.Path}"" Id=""{s.ID}"" />");

//TODO: support multiple drawing
//TODO: support multiple drawing
//TODO: ../drawings/drawing1.xml or /xl/drawings/drawing1.xml
var sheetRelsXml = $@"<Relationship Type=""http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"" Target=""../drawings/drawing{sheetId}.xml"" Id=""drawing{sheetId}"" />";
CreateZipEntry($"xl/worksheets/_rels/sheet{s.SheetIdx}.xml.rels", "",
Expand All @@ -890,7 +890,7 @@ private void GenerateEndXml()
_defaultWorkbookXmlRels.Replace("{{sheets}}", workbookRelsXml.ToString()));
}

//[Content_Types].xml
//[Content_Types].xml
{
var sb = new StringBuilder(@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?><Types xmlns=""http://schemas.openxmlformats.org/package/2006/content-types""><Default ContentType=""application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings"" Extension=""bin""/><Default ContentType=""application/xml"" Extension=""xml""/><Default ContentType=""image/jpeg"" Extension=""jpg""/><Default ContentType=""image/png"" Extension=""png""/><Default ContentType=""image/gif"" Extension=""gif""/><Default ContentType=""application/vnd.openxmlformats-package.relationships+xml"" Extension=""rels""/>");
foreach (var p in _zipDictionary)
Expand Down
2 changes: 1 addition & 1 deletion tests/MiniExcelTests/MiniExcelIssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public void TestIssue369()
public void TestIssueI4ZYUU()
{
var path = PathHelper.GetTempPath();
var value = new TestIssueI4ZYUUDto[] { new TestIssueI4ZYUUDto { MyProperty = "1", MyProperty2 = new DateTime(2022, 10, 15) } };
var value = new TestIssueI4ZYUUDto[] { new() { MyProperty = "1", MyProperty2 = new DateTime(2022, 10, 15) } };
MiniExcel.SaveAs(path, value);
var rows = MiniExcel.Query(path).ToList();
Assert.Equal("2022-10", rows[1].B);
Expand Down

0 comments on commit b95e3f9

Please sign in to comment.