mirror of
https://gitee.com/dotnetchina/MiniExcel.git
synced 2024-11-29 18:38:08 +08:00
Fix QueryAsDataTable can't read Excel with only header rows (#647)
* Fixed an issue with QueryAsDataTable that only header rows cannot be read correctly * handling null value
This commit is contained in:
parent
6161e299fd
commit
4b7f1696db
@ -184,31 +184,35 @@
|
|||||||
|
|
||||||
var dt = new DataTable(sheetName);
|
var dt = new DataTable(sheetName);
|
||||||
var first = true;
|
var first = true;
|
||||||
var rows = ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType), configuration).Query(useHeaderRow, sheetName, startCell);
|
var rows = ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType), configuration).Query(false, sheetName, startCell);
|
||||||
|
|
||||||
var keys = new List<string>();
|
var columnDict = new Dictionary<string, string>();
|
||||||
foreach (IDictionary<string, object> row in rows)
|
foreach (IDictionary<string, object> row in rows)
|
||||||
{
|
{
|
||||||
if (first)
|
if (first)
|
||||||
{
|
{
|
||||||
foreach (var key in row.Keys)
|
foreach (var entry in row)
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(key)) // avoid #298 : Column '' does not belong to table
|
var columnName = useHeaderRow ? entry.Value?.ToString() : entry.Key;
|
||||||
|
if (!string.IsNullOrWhiteSpace(columnName)) // avoid #298 : Column '' does not belong to table
|
||||||
{
|
{
|
||||||
var column = new DataColumn(key, typeof(object)) { Caption = key };
|
var column = new DataColumn(columnName, typeof(object)) { Caption = columnName };
|
||||||
dt.Columns.Add(column);
|
dt.Columns.Add(column);
|
||||||
keys.Add(key);
|
columnDict.Add(entry.Key, columnName);//same column name throw exception???
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dt.BeginLoadData();
|
dt.BeginLoadData();
|
||||||
first = false;
|
first = false;
|
||||||
|
if (useHeaderRow)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var newRow = dt.NewRow();
|
var newRow = dt.NewRow();
|
||||||
foreach (var key in keys)
|
foreach (var entry in columnDict)
|
||||||
{
|
{
|
||||||
newRow[key] = row[key]; //TODO: optimize not using string key
|
newRow[entry.Value] = row[entry.Key]; //TODO: optimize not using string key
|
||||||
}
|
}
|
||||||
|
|
||||||
dt.Rows.Add(newRow);
|
dt.Rows.Add(newRow);
|
||||||
|
Loading…
Reference in New Issue
Block a user