1

I am trying to read an excel file and convert the contents of the file to a datatable but I keep getting this exception IErrorInfo.GetDescription failed with E_FAIL(0x80004005) pointing at a specific line POCCommand.Fill(dt);This is what I tried so far. What could I be doing wrong ?

string POCpath = @"p.xlsx";
 string POCConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + POCpath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";

    OleDbConnection POCcon = new OleDbConnection(POCConnection);
    OleDbCommand POCcommand = new OleDbCommand();
    DataTable dt = new DataTable();
    OleDbDataAdapter POCCommand = new OleDbDataAdapter("select * from [Sheet1$] ", POCcon);
    POCCommand.Fill(dt);
    Console.WriteLine(dt.Rows.Count);
  • Possible duplicate of [Best /Fastest way to read an Excel Sheet into a DataTable?](http://stackoverflow.com/questions/14261655/best-fastest-way-to-read-an-excel-sheet-into-a-datatable) – MrAmazing Mar 01 '17 at 04:39

1 Answers1

0

First, since you're using .NET, you need to wrap anything that inherits or implements from IDisposable into a using statement.

such as:

using (OleDbConnection connection = new OleDbConnection("connectionstring")){
    //Do stuff here
}

Second: Please refer to this great answer by MethodMan on this SO Answer. It has everything you will need to know. He does both OleDb connections and a TextFieldParser which will be faster and less expensive.

Community
  • 1
  • 1
MrAmazing
  • 51
  • 7