在阅读Excel时出现此错误

时间:2010-05-04 11:09:47

标签: c# excel

我有代码

string conn = @"Provider=Microsoft.ACE.OLEDB.12.0;DSN=Excel Files;DBQ=" + strSheetPath + @";DriverId=1046;FIL=excel 12.0;MaxBufferSize=2048;PageTimeout=5;";
 //string conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strSheetPath +";Extended Properties=Excel 12.0 Xml;HDR=YES;IMEX=1";
string destinationConnectionString = ConfigurationManager.ConnectionStrings["KGD6DBConnectionString"].ConnectionString;
string query = "Select * From [Sheet1$]";

using (var myConnection = new OleDbConnection(conn))
using (var destinationConnection = new SqlConnection(destinationConnectionString))
using (var bulkCopy = new SqlBulkCopy(destinationConnection))
{
    //Map first column in source to second column in sql table (skipping the ID column).
    //Excel schema[CompanyName,Phone] Table schema[ShipperID, CompanyName, Phone]
    bulkCopy.ColumnMappings.Add(0, 1);
    bulkCopy.ColumnMappings.Add(1, 2);
    bulkCopy.ColumnMappings.Add(2, 3);
    bulkCopy.ColumnMappings.Add(3, 4);
    bulkCopy.ColumnMappings.Add(4, 5);
    bulkCopy.ColumnMappings.Add(5, 6);
    bulkCopy.ColumnMappings.Add(6, 7);
    bulkCopy.ColumnMappings.Add(7, 8);
    bulkCopy.ColumnMappings.Add(8, 9);
    bulkCopy.ColumnMappings.Add(9, 10);
    bulkCopy.ColumnMappings.Add(10, 11);
    bulkCopy.ColumnMappings.Add(11, 12);
    bulkCopy.ColumnMappings.Add(12, 13);
    bulkCopy.ColumnMappings.Add(13, 14);
    bulkCopy.ColumnMappings.Add(14, 15);
    bulkCopy.ColumnMappings.Add(15, 16);
    bulkCopy.ColumnMappings.Add(16, 17);
    bulkCopy.ColumnMappings.Add(17, 18);
    bulkCopy.ColumnMappings.Add(18, 19);
    bulkCopy.ColumnMappings.Add(19, 20);
    bulkCopy.ColumnMappings.Add(20, 21);
    bulkCopy.ColumnMappings.Add(21, 22);
    bulkCopy.ColumnMappings.Add(22, 23);
    bulkCopy.ColumnMappings.Add(23, 24);

    bulkCopy.DestinationTableName = "dbo.sampleInventory";

    using (var myCommand = new OleDbCommand(query, myConnection))
    {
        myConnection.Open();
        destinationConnection.Open();

        var myReader = myCommand.ExecuteReader();
        while (myReader.Read())
        {
            bulkCopy.WriteToServer(myReader);
        }
    }
}
return (new DataTable());

连接开放线上出现错误。

错误: 无法找到可安装的ISAM。

2 个答案:

答案 0 :(得分:0)

确保您正在构建32位应用程序,否则不应该工作,因为Microsoft.ACE.OLEDB.12.0仅适用于x86平台

如果这无效,请转到http://support.microsoft.com/kb/209805

答案 1 :(得分:0)

看起来可能是允许查询Excel数据的组件,因为数据库丢失或在您的计算机上注册不正确。

此链接中有一些说明可能有所帮助:http://support.microsoft.com/kb/209805