无法创建Excel.Application的对象

时间:2015-05-18 11:45:15

标签: c# .net excel interop

在我的C#应用​​程序中,我将数据导入并导出到excel。我有办公室2013年。

我正在使用以下代码:

using Excel = Microsoft.Office.Interop.Excel;
Excel.Application _excelApp = null;
_excelApp = new Excel.Application();

此代码工作正常但最近我安装了Microsoft project professional 2013,它还更新了办公室。在此之后我在_excelApp = new Excel.Application();

中收到错误

错误是:

  

无法将“System .__ ComObject”类型的COM对象强制转换为接口   输入“Microsoft.Office.Interop.Excel.Application”。这个操作   失败,因为QueryInterface调用了COM组件   与IID'{000208D5-0000-0000-C000-000000000046}'的接口失败   出现以下错误:加载类型库/ DLL时出错。 (例外   来自HRESULT:0x80029C4A(TYPE_E_CANTLOADLIBRARY))。

3 个答案:

答案 0 :(得分:0)

@Deepak gupta,我不能评论所以我在这里发布。也许Soner是对的。我曾经遇到过同样的问题所以我决定用旧方法打开一个Excel文件(用oledb命令)。

你需要什么?阅读Excel或保存新的?

例如在我的情况下,我需要阅读Excel(即和.xls或.xlsx):

               //Check whether file extension is xls or xslx
            if (fileExtension == ".xls")
                connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            else if (fileExtension == ".xlsx")
                connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";

            //Create OleDB Connection and OleDb Command            
            OleDbCommand cmd = new OleDbCommand();
            OleDbConnection con = new OleDbConnection(connectionString);

            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = con;

            OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
            con.Open();
            DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();

            //send query, send query, fill adapter:
            cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "A1:GR255] WHERE [0] IS NOT NULL ";
            dAdapter.SelectCommand = cmd;
            dAdapter.Fill(dtExcelRecords);
            con.Close();

或许您可以尝试使用此帖子Create Excel (.XLS and .XLSX) file from C#中描述的此库ExcelLibrary。

答案 1 :(得分:0)

这是创建Object的方法,从而将工作表从一个Excel复制到另一个Excel

Dim CopyFrom As Object         Dim CopyTo As Object         Dim CopyThis As Object         Dim xl As Object

    xl = CreateObject("Excel.Application")
    xl.Visible = False
    CopyFrom = xl.Workbooks.Open("E:\EXCEL\From.xls")
    CopyTo = xl.Workbooks.Open("E:\EXCEL\To.xls")
    For i = 0 To 1
        ''To use a password: Workbooks.Open Filename:="Filename", Password:="Password"
        If i = 0 Then
            CopyThis = CopyFrom.Sheets(1)
            CopyThis.Copy(After:=CopyTo.Sheets(CopyTo.Sheets.Count))
            CopyTo.Sheets(3).Name = "Sheet3"
        Else
            CopyThis = CopyFrom.Sheets(2)
            CopyThis.Copy(After:=CopyTo.Sheets(CopyTo.Sheets.Count))
            CopyTo.Sheets(4).Name = "Sheet4"
        End If
    Next
    CopyTo.Sheets(1).Activate()
    CopyTo.Save()
    'CopyTo.SaveAs("E:\EXCEL\Check.xls")
    xl.Quit()

答案 2 :(得分:0)

问题已经解决。当我安装Microsoft project professional 2013时,它也更新了microsoft office,但办公室的更新并不成功。

当我修理办公室时,它开始正常工作。