连接到Excel电子表格时出错

时间:2014-01-13 22:27:33

标签: c# .net excel

我不知道我在这里做错了什么,但我在connection.Open();行继续收到以下异常:

IErrorInfo.GetDescription failed with E_FAIL(0x80004005)

问题是我在另一个批处理作业中几乎完全相同的代码,它工作正常。我们甚至从同一位置提取电子表格。有没有人看到我的连接或查询字符串有什么问题?

    static void Main(string[] args)
    {
        string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= \\prdhilfs03\l&i-sales&mkt\WORKAREA\Agencyservices\Shared\AIC\Analysts_and_Reporting\Realignments\2014\MassUpdateTesting\ZipCodeTest.xslx;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
        string queryString = "SELECT * FROM [Query1$]";


        try
        {
            OleDbDataReader reader;
            using (OleDbConnection connection = new OleDbConnection(connString))
            {
                //Set connection objects to pull from spreadsheet
                OleDbCommand command = new OleDbCommand(queryString, connection);
                connection.Open();

为了更好地衡量,这里是我正在尝试连接的工作簿的屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:1)

您是否尝试检查是否可以访问该文件?

string xlFile = @"\\prdhilfs03\l&i-sales&mkt\......\ZipCodeTest.xlsx"; 
Console.WriteLine(File.Exists(xlFile) ? "Excel File exists." : "Excel File does not exist.");