web*_*orm 1 datatable excel closedxml
我想将Excel工作表的内容读入C#DataTable。Excel工作表可能具有可变数量的列和行。Excel工作表中的第一行将始终包含列名,但其他行可能为空白。
我在此处看到的所有建议都假定存在Microsoft.ACE.OLEDB。我没有在系统上安装此库,因为当我尝试其中一些解决方案时遇到此错误。
Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
Run Code Online (Sandbox Code Playgroud)
考虑到我已安装Office 2016,这很奇怪。
因此,我希望通过Nuget使用ClosedXML库,但在他们的Wiki中看不到任何将Excel工作表读取为C#数据表的示例。
web*_*orm 10
这不是我的例子。我不记得我从档案馆那里得到的东西。但是,这对我有用。我遇到的唯一问题是空白单元格。根据ClosedXML GitHUb Wiki页面上的讨论,它与Excel无关,它不跟踪不受数据限制的空单元格。我发现,如果我将数据添加到单元格,然后删除相同的数据,则该过程将起作用。
public static DataTable ImportExceltoDatatable(string filePath, string sheetName)
{
// Open the Excel file using ClosedXML.
// Keep in mind the Excel file cannot be open when trying to read it
using (XLWorkbook workBook = new XLWorkbook(filePath))
{
//Read the first Sheet from Excel file.
IXLWorksheet workSheet = workBook.Worksheet(1);
//Create a new DataTable.
DataTable dt = new DataTable();
//Loop through the Worksheet rows.
bool firstRow = true;
foreach (IXLRow row in workSheet.Rows())
{
//Use the first row to add columns to DataTable.
if (firstRow)
{
foreach (IXLCell cell in row.Cells())
{
dt.Columns.Add(cell.Value.ToString());
}
firstRow = false;
}
else
{
//Add rows to DataTable.
dt.Rows.Add();
int i = 0;
foreach (IXLCell cell in row.Cells(row.FirstCellUsed().Address.ColumnNumber, row.LastCellUsed().Address.ColumnNumber))
{
dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
i++;
}
}
}
return dt;
}
}
Run Code Online (Sandbox Code Playgroud)
需要添加
using System.Data;
using ClosedXML.Excel;
Run Code Online (Sandbox Code Playgroud)
以及ClosedXML nuget包
对于其他日期时间数据类型,这可能会有所帮助... 参考
if (cell.Address.ColumnLetter=="J") // Column with date datatype
{
DateTime dtime = DateTime.FromOADate(double.Parse(cell.Value.ToString()));
dt.Rows[dt.Rows.Count - 1][i] = dtime;
}
else
{
dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*dri 10
使用此代码,您可以读取 Excel 工作表的内容。您可以指定工作表的名称或编号,将返回一个包含工作表内容的数据集。
public static DataTable GetDataFromExcel(string path, dynamic worksheet)
{
//Save the uploaded Excel file.
DataTable dt = new DataTable();
//Open the Excel file using ClosedXML.
using (XLWorkbook workBook = new XLWorkbook(path))
{
//Read the first Sheet from Excel file.
IXLWorksheet workSheet = workBook.Worksheet(worksheet);
//Create a new DataTable.
//Loop through the Worksheet rows.
bool firstRow = true;
foreach (IXLRow row in workSheet.Rows())
{
//Use the first row to add columns to DataTable.
if (firstRow)
{
foreach (IXLCell cell in row.Cells())
{
if (!string.IsNullOrEmpty(cell.Value.ToString()))
{
dt.Columns.Add(cell.Value.ToString());
}
else
{
break;
}
}
firstRow = false;
}
else
{
int i = 0;
DataRow toInsert = dt.NewRow();
foreach (IXLCell cell in row.Cells(1, dt.Columns.Count))
{
try
{
toInsert[i] = cell.Value.ToString();
}
catch (Exception ex)
{
}
i++;
}
dt.Rows.Add(toInsert);
}
}
return dt;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9074 次 |
| 最近记录: |