Importing Excel data into C# without first row becoming column names?

二次信任 提交于 2019-12-01 03:56:48

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/4435276/importing-excel-data-into-c-sharp-without-first-row-becoming-column-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!