0

I have 100 excel files. In each file I need one row from it to be inserted into SQL server. I need this automated but ultimately don't care if it is asp.net in an app or excel macros. If you have any ideas, please let me know.

Issue I wrote the asp.net 4.0 command line app code then got the error: The 'Microsoft.Ace.OleDb.4.0' provider is not registered on the local machine.

Tried After I installed Microsoft Access Database Engine 2010 Redistributable, (http://www.microsoft.com/en-us/download/details.aspx?id=13255) there are no more odbc drivers on my box. Just the SQL drivers as before.

My box is x64 but I have x86 office installed so I installed the Microsoft Access Database Engine 2010 Redistributable for x86.

I also checked the registry and can't see any drivers but the SQL server drivers.

I've read several articles:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/eeaa2d7b-fbfa-401b-8efe-9170f04059b0

The 'Microsoft.ACE.OLEDB.12.0' provider is not registered in the local machine

While I'm going to include my code below, I don't think it makes a difference until I have the drivers -

public void ImportExcelFilesForPieChart()
{
    List<DataTable> listDataTables = new List<DataTable>();
    List<CompanyInfo> newlist = new List<CompanyInfo>();
    string sheetName = "Output";

    string[] fileList = Directory.GetFiles(dropBoxExcelLocation);
    foreach (string filename in fileList)
    {
        DataTable dt = new DataTable();
        string connectionString = string.Format("Provider=Microsoft.Ace.OleDb.4.0;data source={0};Extended Properties=Excel 8.0;HDR=No;IMEX=1", dropBoxExcelLocation + filename);
        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {
            string strSQL = String.Format("SELECT 4A, 4B, 4C, 4D, 4E, 4F FROM [{0}$]", sheetName); //[A4:F4]";
            OleDbCommand objCmd = new OleDbCommand(strSQL, connection);
            connection.Open();

            using (OleDbDataAdapter da = new OleDbDataAdapter(strSQL, connection))
            {
                da.Fill(dt);
                listDataTables.Add(dt);
            } 
        }
    }
}
Community
  • 1
  • 1
DFBerry
  • 1,818
  • 1
  • 19
  • 37

2 Answers2

1

Your connection string is incorrect.

The provider should be Microsoft.ACE.OLEDB.12.0

I believe the JET provider is 4.0. Were you trying that beforehand?

Dave R.
  • 7,206
  • 3
  • 30
  • 52
  • I changed the connection string and now my error is "Cound not find installable ISAM." I'm following this KB now - http://support.microsoft.com/kb/209805. – DFBerry Aug 15 '12 at 15:35
0

I did several things so I'm not sure if all the steps are important.

I registered my dlls according to this kb article http://support.microsoft.com/kb/209805. Before registering them, I verified there were no entries in the registry. This got me to the ISAM not found exception.

Then, thanks to Dave R, I changed my connection string. The final connection string that worked was (notice the escaped quotes on the Extended Properties)

string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;data source={0};Extended Properties=\"Excel 12.0 Xml;IMEX=1\"", filename);

I didn't realize the file extension (.xlsx) was relavant to the connection string but after reading the connection string information here (http://www.connectionstrings.com/excel-2007), I fixed it.

DFBerry
  • 1,818
  • 1
  • 19
  • 37