diff --git a/docs/README.md b/docs/README.md index 045cf62f..ea6277fd 100644 --- a/docs/README.md +++ b/docs/README.md @@ -16,6 +16,9 @@ --- +### 0.19.1 +- [New] QueryAsDataTable remove empty column keys. #298 + ### 0.19.0 - [New] SaveAs default style with autoFilter mode. #190 - [New] Add ConvertCsvToXlsx、ConvertXlsxToCsv method. #292 diff --git a/docs/README.zh-CN.md b/docs/README.zh-CN.md index b9cb493e..f88433f2 100644 --- a/docs/README.zh-CN.md +++ b/docs/README.zh-CN.md @@ -23,6 +23,9 @@ --- +### 0.19.1 +- [New] QueryAsDataTable 删除空白 Column keys. #298 + ### 0.19.0 - [New] SaveAs 预设样式增加筛选功能. #190 - [New] 新增 ConvertCsvToXlsx、ConvertXlsxToCsv 方法. #292 diff --git a/docs/README.zh-Hant.md b/docs/README.zh-Hant.md index 3e7a0d72..5c6a7136 100644 --- a/docs/README.zh-Hant.md +++ b/docs/README.zh-Hant.md @@ -17,6 +17,9 @@ --- +### 0.19.1 +- [New] QueryAsDataTable 刪除空白 Column keys. #298 + ### 0.19.0 - [New] SaveAs 預設樣式增加篩選功能. #190 - [New] 新增 ConvertCsvToXlsx、ConvertXlsxToCsv 方法. #292 diff --git a/samples/csv/TestIssue298.csv b/samples/csv/TestIssue298.csv new file mode 100644 index 00000000..3459776a --- /dev/null +++ b/samples/csv/TestIssue298.csv @@ -0,0 +1,6 @@ +ID,Name,,Age +1,Jack,,18 +,,, +2,Henry,,33 +,,, +3,Mike,,25 diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs index a9f04831..2f103507 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs @@ -594,15 +594,20 @@ internal static DataTable QueryAsDataTableImpl(Stream stream, bool useHeaderRow, var dt = new DataTable(sheetName); var first = true; var rows = ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType)).Query(useHeaderRow, sheetName, startCell, configuration); + + var keys = new List(); foreach (IDictionary row in rows) - { + { if (first) { - foreach (var key in row.Keys) { - var column = new DataColumn(key, typeof(object)) { Caption = key }; - dt.Columns.Add(column); + if (!string.IsNullOrEmpty(key)) // avoid #298 : Column '' does not belong to table + { + var column = new DataColumn(key, typeof(object)) { Caption = key }; + dt.Columns.Add(column); + keys.Add(key); + } } dt.BeginLoadData(); @@ -610,7 +615,7 @@ internal static DataTable QueryAsDataTableImpl(Stream stream, bool useHeaderRow, } var newRow = dt.NewRow(); - foreach (var key in row.Keys) + foreach (var key in keys) { newRow[key] = row[key]; //TODO: optimize not using string key } diff --git a/tests/MiniExcelTests/MiniExcelIssueTests.cs b/tests/MiniExcelTests/MiniExcelIssueTests.cs index 521a7e4d..5a384f13 100644 --- a/tests/MiniExcelTests/MiniExcelIssueTests.cs +++ b/tests/MiniExcelTests/MiniExcelIssueTests.cs @@ -27,6 +27,18 @@ public MiniExcelIssueTests(ITestOutputHelper output) this.output = output; } + /// + /// Column '' does not belong to table when csv convert to datatable #298 + /// https://github.com/shps951023/MiniExcel/issues/298 + /// + [Fact] + public void TestIssue298() + { + var path = PathHelper.GetSamplePath("/csv/TestIssue298.csv"); + var dt = MiniExcel.QueryAsDataTable(path); + Assert.Equal(new[] { "ID", "Name", "Age" }, dt.Columns.Cast().Select(_ => _.ColumnName)); + } + /// /// SaveAsByTemplate if there is & in the cell value, it will be & /// https://gitee.com/dotnetchina/MiniExcel/issues/I4DQUN @@ -38,14 +50,14 @@ public void TestIssueI4DQUN() var path = PathHelper.GetTempPath(); var value = new Dictionary() { - { "Title","Hello & World" }, - { "Details",new[]{ new { Value = "Hello & Value" } } }, + { "Title","Hello & World < , > , \" , '" }, + { "Details",new[]{ new { Value = "Hello & Value < , > , \" , '" } } }, }; MiniExcel.SaveAsByTemplate(path, templatePath, value); var sheetXml = Helpers.GetZipFileContent(path, "xl/worksheets/sheet1.xml"); - Assert.Contains("Hello & World", sheetXml); - Assert.Contains("Hello & Value", sheetXml); + Assert.Contains("Hello & World < , > , \" , '", sheetXml); + Assert.Contains("Hello & Value < , > , \" , '", sheetXml); } ///