简体   繁体   English

在命令行应用程序中将asp.net 4.0转换为excel 2010无法正常工作-即使在安装驱动程序后也找不到驱动程序

[英]asp.net 4.0 to excel 2010 in command line app not working - drivers not found even after driver install

I have 100 excel files. 我有100个Excel文件。 In each file I need one row from it to be inserted into SQL server. 在每个文件中,我需要从其中插入一行到SQL Server中。 I need this automated but ultimately don't care if it is asp.net in an app or excel macros. 我需要这个自动化的工具,但是最终不管它是应用程序还是excel宏中的asp.net。 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. 问题我写了asp.net 4.0命令行应用程序代码,然后收到错误消息:Microsoft.Ace.OleDb.4.0提供程序未在本地计算机上注册。

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. 试图安装Microsoft Access数据库引擎2010可再发行后( http://www.microsoft.com/en-us/download/details.aspx?id=13255 )上有我的盒子没有更多的ODBC驱动程序。 Just the SQL drivers as before. 像以前一样只是SQL驱动程序。

My box is x64 but I have x86 office installed so I installed the Microsoft Access Database Engine 2010 Redistributable for x86. 我的盒子是x64,但是我安装了x86 office,所以我安装了x86的Microsoft Access数据库引擎2010 Redistributable。

I also checked the registry and can't see any drivers but the SQL server drivers. 我还检查了注册表,除了SQL Server驱动程序之外,看不到任何驱动程序。

I've read several articles: 我读过几篇文章:

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

The 'Microsoft.ACE.OLEDB.12.0' provider is not registered in the local machine 未在本地计算机中注册“ Microsoft.ACE.OLEDB.12.0”提供程序

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);
            } 
        }
    }
}

Your connection string is incorrect. 您的连接字符串不正确。

The provider should be Microsoft.ACE.OLEDB.12.0 提供程序应该是Microsoft.ACE.OLEDB.12.0

I believe the JET provider is 4.0. 我相信JET提供程序是4.0。 Were you trying that beforehand? 您是否事先尝试过?

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 . 我根据此kb文章http://support.microsoft.com/kb/209805注册了dll。 Before registering them, I verified there were no entries in the registry. 在注册它们之前,我验证了注册表中没有条目。 This got me to the ISAM not found exception. 这使我无法找到ISAM异常。

Then, thanks to Dave R, I changed my connection string. 然后,多亏了Dave R,我更改了连接字符串。 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. 我没有意识到文件扩展名(.xlsx)与连接字符串有关,但是在此处( http://www.connectionstrings.com/excel-2007 )阅读了连接字符串信息后,我对其进行了修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM