开发者

Major issues working with XLS in c# .net 4.0

I have an xls file i would like to read using c# and populate the information in a data table. The code I am using is :

public static DataTable GetExcelData(string excelFilePath)
{
    OleDbConnection objConn = null;
    string oledbConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFilePath + ";Extended Properties=Excel 10.0;";
    objConn = new OleDbConnection(oledbConnectionString);

    if (objConn.State == ConnectionState.Closed)
    {
        objConn.Open();
    }
    var objCmdSelect = new OleDbCommand("Select * from [Sheet1$]", objConn);
    var objAdapter = new OleDbDataAdapter();
    objAdapter.SelectCommand = objCmdSelect;
    var objDataset = new DataSet();
    objAdapter.Fill(objDataset, "ExcelDataTable");
    objConn.Close();
    return objDataset.Tables[0];
}

Once this data table is populated, I need to remove the first 5 or so rows which contain header information, and loop through the data table populating an access database table. I have had no luck with this or any of the other 10,000 way开发者_如何学编程s suggested. Does anyone have any information that can help me. I am running VS2010 .net 4.0 framework. Any and all help would be super appreciated.

Thanks, John


I've had a great deal of trouble trying to get Excel data into a DataTable using OLEDB. I finally solved the issue by switching to a solution that uses Excel Interop. Please see this answer for further explanation and sample code:

Importing from Excel: some cells become null

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜