Get column name from excel worksheet

回眸只為那壹抹淺笑 提交于 2019-12-02 07:13:10

Does you connection string include the HDR=YES ?:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES";

Once you populate your DataTable or DataSet you can treat the usual way:

dt.Columns[0].ColumnName

Or:

// For each DataTable, print the ColumnName.
foreach(DataTable table in dataSet.Tables)
{
    foreach(DataColumn column in table.Columns)
    {
        Console.WriteLine(column.ColumnName);
    }
}

Also this does not look syntax correct:

OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM ["xlWorksheet"$]", objConn); 

Should be something like:

OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" + xlWorksheet + "$]", objConn); 

Finally, if you time - investigate EPPlus (open source) for reading/writing Excel - http://epplus.codeplex.com as it works in both 32-bit and 64-bit environments.

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