将 Excel 数据导入 C# 而不将第一行变成列名? [英] Importing Excel data into C# without first row becoming column names?
本文介绍了将 Excel 数据导入 C# 而不将第一行变成列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 c# 将数据从 excel 导入数据表.这是我用来执行此操作的代码...
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);
}
当我在调试时查看数据表时,第一行数据已成为表列名.当我将 HDR=No 放入连接字符串时,我不明白为什么会发生这种情况.有没有办法强制DataTable不把第一行作为列名?
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?
推荐答案
您提供的代码示例具有 HRD=No
而不是 HDR=No
The code sample you provided has HRD=No
instead of HDR=No
这篇关于将 Excel 数据导入 C# 而不将第一行变成列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文