简体   繁体   English

从C#.Net(VS2010)打开Excel文件时出错

[英]Error in Opening the Excel file from C#.Net (VS2010)

I am trying to open an excel file (.xlsx) using C# (VS2010 professional) code. 我正在尝试使用C#(VS2010专业版)代码打开Excel文件(.xlsx)。 I am getting an (untraceable, to me) exception when executing/single stepping the last 2 lines of the below code. 在执行/单步执行以下代码的最后两行时,我得到了(对我来说是不可追踪的)异常。 Below mentioned is my code for opening an existing excel file. 下面提到的是我用于打开现有excel文件的代码。

        string tesfile = "C:\\Users\\AWaheed3\\Desktop\\1.xlsx";
        Microsoft.Office.Interop.Excel.Application xlApp;
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        object misValue = System.Reflection.Missing.Value;
        xlApp = new Microsoft.Office.Interop.Excel.Application();
        xlApp.Visible = true;
        xlWorkBook = xlApp.Workbooks.Open(tesfile, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);   

Also I have included this below line at the start of the code. 另外,我还在代码的开始部分将此行包括在内。 Further more I have Added the reference of the Microsoft.Office.Interop.Excel from Project->Add Reference (.NET Tab) 另外,我已经从Project-> Add Reference(.NET选项卡)中添加了Microsoft.Office.Interop.Excel的引用。

 using Microsoft.Office.Interop.Excel;

Can anybody advise why my code is failing/throwing an error? 谁能告诉我为什么我的代码失败/引发错误?

Regards Asad 问候阿萨德

EDITED* * ** * ** * ** * ** * ** * ** * ** * ** * * 编辑* * ** * ** * ** * ** * ** * ** * ** * ** * *

Here is the message/error I am receiving. 这是我收到的消息/错误。 Note that the code is failing even while executing the xlApp.Visible = ture line. 请注意,即使执行xlApp.Visible = ture行,代码也会失败。 Error is 错误是

Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. 无法将类型为“ Microsoft.Office.Interop.Excel.ApplicationClass”的COM对象转换为接口类型为“ Microsoft.Office.Interop.Excel._Application”。 This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Library not registered. 此操作失败,因为具有以下错误的IID为“ {000208D5-0000-0000-C000-000000000046}”的接口在COM组件上的QueryInterface调用由于以下错误而失败:库未注册。 (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED)). (来自HRESULT的异常:0x8002801D(TYPE_E_LIBNOTapped))。


Try to remove the double backslash and write instead \\ or you can do is 尝试删除双反斜杠并改为写\\,或者您可以做的是

Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Excel.Workbook xls = null;
try
{
     excel = new Microsoft.Office.Interop.Excel.Application();
     object missing = Type.Missing;
     object trueObject = true;
     try
     {
          excel.Visible = false; // or true
          excel.DisplayAlerts = false;
     }
     catch(Exception e)
     {
                Console.WriteLine("-------Error hiding the application-------");
                Console.WriteLine("Occured error might be: " + e.StackTrace);
     }
     xls = excel.Workbooks.Open(excelFile, missing, trueObject,    missing,missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
     //xls = excel.Workbooks.Open(@"file.xls", missing, trueObject,    missing,missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
}
catch (Exception ex)
{
            MessageBox.Show("Error accessing Excel document.\n\n" +
            ex.Message);
}
// Must be surrounded by try catch to work.
// http://naimishpandya.wordpress.com/2010/12/31/hide-power-point-application-window-in-net-office-automation/

The @ removes any issue with the path. @删除路径的任何问题。 Code is similar to yours of course. 代码与您的代码相似。 Just I'm rewriting it in different way. 我只是用不同的方式重写它。 Look for the System.Reflection, I'm using System.Type instead. 寻找System.Reflection,我改用System.Type。 And some advice for the future, if you work with COM files, try to close them correctly at the end. 还有一些将来的建议,如果您使用COM文件,请尝试在最后正确关闭它们。 **Edited. **编辑。

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

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