Skip to content

Commit

Permalink
[New] QueryAsDataTable remove empty column keys. #298
Browse files Browse the repository at this point in the history
  • Loading branch information
shps951023 committed Oct 25, 2021
1 parent 70ec60d commit 9a05683
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 9 deletions.
3 changes: 3 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docs/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

---

### 0.19.1
- [New] QueryAsDataTable 删除空白 Column keys. #298

### 0.19.0
- [New] SaveAs 预设样式增加筛选功能. #190
- [New] 新增 ConvertCsvToXlsx、ConvertXlsxToCsv 方法. #292
Expand Down
3 changes: 3 additions & 0 deletions docs/README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

---

### 0.19.1
- [New] QueryAsDataTable 刪除空白 Column keys. #298

### 0.19.0
- [New] SaveAs 預設樣式增加篩選功能. #190
- [New] 新增 ConvertCsvToXlsx、ConvertXlsxToCsv 方法. #292
Expand Down
6 changes: 6 additions & 0 deletions samples/csv/TestIssue298.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ID,Name,,Age
1,Jack,,18
,,,
2,Henry,,33
,,,
3,Mike,,25
15 changes: 10 additions & 5 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -594,23 +594,28 @@ 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<string>();
foreach (IDictionary<string, object> 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();
first = false;
}

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
}
Expand Down
20 changes: 16 additions & 4 deletions tests/MiniExcelTests/MiniExcelIssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ public MiniExcelIssueTests(ITestOutputHelper output)
this.output = output;
}

/// <summary>
/// Column '' does not belong to table when csv convert to datatable #298
/// https://github.com/shps951023/MiniExcel/issues/298
/// </summary>
[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<DataColumn>().Select(_ => _.ColumnName));
}

/// <summary>
/// SaveAsByTemplate if there is & in the cell value, it will be &amp;
/// https://gitee.com/dotnetchina/MiniExcel/issues/I4DQUN
Expand All @@ -38,14 +50,14 @@ public void TestIssueI4DQUN()
var path = PathHelper.GetTempPath();
var value = new Dictionary<string, object>()
{
{ "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("<v>Hello &amp; World</v>", sheetXml);
Assert.Contains("<v>Hello &amp; Value</v>", sheetXml);
Assert.Contains("<v>Hello &amp; World &lt; , &gt; , \" , '</v>", sheetXml);
Assert.Contains("<v>Hello &amp; Value &lt; , &gt; , \" , '</v>", sheetXml);
}

/// <summary>
Expand Down

0 comments on commit 9a05683

Please sign in to comment.