将 Excel 数据导入 C# 而不将第一行变成列名称?

发布于 2024-10-07 04:00:07 字数 670 浏览 0 评论 0原文

我正在尝试使用 C# 将数据从 excel 导入到数据表中。这是我用来执行此操作的代码...

string ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                    "Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;" +
                    "Extended Properties=\"Excel 8.0;HRD=No;IMEX=1;\"";
OleDbDataAdapter SheetAdapter = new OleDbDataAdapter("select * from ["Sheet1"]", conn);
System.Data.DataTable excelData = new System.Data.DataTable();
SheetAdapter.Fill(excelData);
excelData.TableName = "excelData";

foreach (DataRow row in excelData.Rows)
{
   ProcessDataRow(row);
}

当我在调试时查看数据表时,第一行数据已成为表列名称。我不明白为什么当我将 HDR=No 放入连接字符串时会发生这种情况。有没有办法强制 DataTable 不将第一行作为列名?

I am trying to import data from excel into a datatable using c#. Here is the code I use to do so...

string ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                    "Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;" +
                    "Extended Properties=\"Excel 8.0;HRD=No;IMEX=1;\"";
OleDbDataAdapter SheetAdapter = new OleDbDataAdapter("select * from ["Sheet1"]", conn);
System.Data.DataTable excelData = new System.Data.DataTable();
SheetAdapter.Fill(excelData);
excelData.TableName = "excelData";

foreach (DataRow row in excelData.Rows)
{
   ProcessDataRow(row);
}

When I look at the datatable while debugging the first row of data has become the tables column names. I don't understand why this is happening when I put HDR=No into the connection string. Is there a way to force the DataTable to not take the first row as column names?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离线来电— 2024-10-14 04:00:07

您提供的代码示例具有 HRD=No 而不是 HDR=No

The code sample you provided has HRD=No instead of HDR=No

拥醉 2024-10-14 04:00:07

我使用 NPOI 库 来完成您询问的任务以及更多任务。唯一的限制是它尚无法处理 Excel 2007 格式,因此您只能使用 97-2003 格式。

I used the NPOI library for just the task you inquired about and more. The only limitation is that it cannot yet handle the Excel 2007 format, so you are limited to the 97-2003 format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文