1

我正在将 excel 导入 DataTable。excel 文件包含 50x7 的单元格和数据。问题是 Fill() 方法导入 368(?) 行,而不管数据在前 50 行中。知道可能是什么问题吗?

我正在使用 OleDbDataAdapter 进行导入。

 connectionString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=Excel 8.0;";
 string  commandString = "select * from [" + worksheetName + "]";
 OleDbDataAdapter adapter = new OleDbDataAdapter(commandString, connectionString);
 DataTable fileTable = new DataTable();
 adapter.Fill(fileTable);
4

1 回答 1

1

试试这个来删除空单元格DataTable

adapter.Fill(fileTable);
fileTable = fileTable.AsEnumerable()
           .Where(row => !row.ItemArray.All(f => f is System.DBNull || String.IsNullOrWhiteSpace(f.ToString())))
           .CopyToDataTable();

请注意,它还会删除工作表内的空行。

于 2012-04-10T11:59:56.390 回答