当我导入Excel时,一个额外的行被添加到数据库中的C#
我正在通过下面的代码通过C#导入Excel文件。 但是,当我检查数据库时,我看到一个空行被添加。
你能帮我改变这个代码来删除这个空行吗?
public static class ExcelTools { static DataSet result = new DataSet(); public static DataTable ExcelToDataTable(string path) { var pck = new OfficeOpenXml.ExcelPackage(); pck.Load(File.OpenRead(path)); var ws = pck.Workbook.Worksheets.First(); DataTable tbl = new DataTable(); bool hasHeader = true; foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column]) { tbl.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column)); } var startRow = hasHeader ? 2 : 1; for (var rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++) { var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column]; var row = tbl.NewRow(); foreach (var cell in wsRow) { row[cell.Start.Column - 1] = cell.Text; } tbl.Rows.Add(row); } pck.Dispose(); tbl.TableName = System.IO.Path.GetFileNameWithoutExtension(path); return tbl; } }
你可能被最后一个NULL
行弄糊涂了:
允许您手动添加一行。 这意味着它不在你的数据库中。 可以肯定的是,你可以计算空行:
select sum(case when [COL] is null then 1 else 0 end) count_nulls from [TABLE]