简体   繁体   English

Vista上的Excel 2007操作异常

[英]Exception in Excel 2007 operation on Vista

I have developed an console application in C# .net which creates a new excel & performs some operations in it [Interop]. 我已经在C#.net中开发了一个控制台应用程序,该应用程序创建了一个新的excel,并在其中执行了一些操作[Interop]。

I have added Interop.Microsoft.Office.Interop.Excel.dll as reference in my project 我在项目中添加了Interop.Microsoft.Office.Interop.Excel.dll作为参考

The code works fine on XP & in Vista too. 该代码在XP和Vista中也能正常工作。 But if i keep my exe in one of the folder then it gives me excception 但是,如果我将我的exe保留在其中一个文件夹中,那么它给我的印象是

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C0 00-000000000046} failed due to the following error: 80090006. 由于以下错误,检索具有CLSID {00024500-0000-0000-C0 00-000000000046}的组件的COM类工厂失败,原因是:80090006。

Unhandled Exception: System.Exception: Unhandled exception during execution ---> System.NullReferenceException: Object reference not set to an instance of an object. 未处理的异常:System.Exception:执行期间未处理的异常---> System.NullReferenceException:对象引用未设置为对象的实例。

Anyone has any idea whether this could be possible because of stricter policies which might have been set on any folder. 任何人都不知道这是可能的,因为可能已在任何文件夹上设置了更严格的策略。 I am asking this because, if i copy this exe to another folder, the exe works perfectly. 我问这个问题是因为,如果我将此exe复制到另一个文件夹,则该exe可以完美运行。

The piece of code where execption is thrown is, 引发执行的代码是,

/* Create spreadsheet update data */
InteropExcel.Workbook oWorkbook = null;
InteropExcel.Worksheet oWorkSheet = null;
InteropExcel.ApplicationClass appExcel = null;

try
{
  // Probable at this line
  appExcel = new InteropExcel.ApplicationClass();

  oWorkbook = (InteropExcel.Workbook)appExcel.Workbooks.Add(true);
  oWorkSheet = (InteropExcel.Worksheet)oWorkbook.ActiveSheet;
  // ...

I am also not able to handle it even i have written this code in try-catch block. 即使我已经在try-catch块中编写了此代码,我也无法处理它。

Can anyone suggest any solution for it? 谁能提出任何解决方案?

Thanks, 谢谢,

Amit 阿米特

It is difficult to know for sure what could be causing this. 很难确定是什么原因造成的。 So, first a few questions, which you can reply to or clarify by editing your original question, above. 因此,首先要回答一些问题,您可以在上面编辑原始问题来回答或澄清这些问题。

(1) When you say that you have referenced "Interop.Microsoft.Office.Interop.Excel.dll", I assume that you mean Microsoft.Office.Interop.Excel.dll ? (1)当您说已引用“ Interop.Microsoft.Office.Interop.Excel.dll”时,我假设您的意思是Microsoft.Office.Interop.Excel.dll

Also, if you check the properties for this reference does it show that it's location is in the GAC? 另外,如果您检查此引用的属性,是否表明它的位置在GAC中? You can check this by going into the Solution Explorer, expanding the Project, expanding the References, right clicking on the Microsoft.Office.Interop.Excel reference and then checking the Path property. 您可以通过以下方法进行检查:进入解决方案资源管理器,展开项目,展开引用,右键单击Microsoft.Office.Interop.Excel引用,然后检查Path属性。 If it is in the GAC, the Path property should begin with "C:\\Windows\\Assembly\\GAC...". 如果它在GAC中,则Path属性应以“ C:\\ Windows \\ Assembly \\ GAC ...”开头。

(2) It also appears that you have a using statement that defines InteropExcel somewhere? (2)您似乎还具有在某处定义InteropExcel的using语句? Something like this: 像这样:

using InteropExcel = Microsoft.Office.Interop.Excel;

Is this correct, or is the InteropExcel a separate reference? 这是正确的,还是InteropExcel是单独的参考?

(3) When you say that you are moving your exe, are you moving the exe only, or the entire folder that holds the exe and the associated files? (3)当您说要移动exe时,是仅移动exe还是移动该exe及其相关文件的整个文件夹? A .NET assembly is not self contained, it needs the references, config files, and/or other files to be within the same folder as the exe, or sometimes within a subfolder of the same folder that holds the exe. .NET程序集不是自包含的,它需要引用,配置文件和/或其他文件位于与exe相同的文件夹中,或者有时位于包含exe的同一文件夹的子文件夹中。 Make sure that you move everything together. 确保将所有内容一起移动。

(4) You should run your code in debug mode, hosted from within the Visual Studio IDE, so that you can know exactly which line is throwing an exception. (4)您应该在Visual Studio IDE中托管的调试模式下运行代码,以便您可以准确知道哪一行引发异常。 My guess is that your first line is returning null : 我的猜测是您的第一行返回null

appExcel = new InteropExcel.ApplicationClass();

And then I think your next line is throwing a NullReferenceException , because your 'appExcel' reference is null: 然后我认为您的下一行将引发NullReferenceException ,因为您的“ appExcel”引用为null:

oWorkbook = (InteropExcel.Workbook)appExcel.Workbooks.Add(true);

(Hmmm... actually, the more I think about this, the less this makes sense. Not tested, but I would not expect that a call to appExcel = new InteropExcel.ApplicationClass() would quietly return null . I would expect it to either correctly return an application object or throw an exception, but not quietly fail by returning null . But maybe. If not, then I think your exception might be occurring in some code outside of the code you show. So you really need to run this within the Visual Studio IDE so that you can know which line is failing.) (嗯……实际上,我对这个问题的思考越多,它的意义就越小。未经测试,但我不希望对appExcel = new InteropExcel.ApplicationClass()的调用会悄悄地返回null 。我希望这样做要么正确地返回一个应用程序对象或抛出一个异常,但不会因返回null而悄然失败,但是也许,如果没有,那么我认为您的异常可能发生在所显示代码之外的某些代码中,因此您确实需要运行此代码在Visual Studio IDE中,这样您就可以知道哪一行失败了。)

(5) Additionally, your call to the Workbooks.Add method is not correct. (5)此外,您对Workbooks.Add方法的调用不正确。 The Template parameter is used to determine which workbook, if any, shall act as the template for the new workbook. Template参数用于确定哪个工作簿(如果有)应充当新工作簿的模板。 In general, the argument passed in should be Type.Missing in order to omit this parameter; 通常,传入的参数应为Type.Missing以便省略此参数; this will allow a new workbook to be created based on a default, blank workbook. 这将允许基于默认的空白工作簿创建新的工作簿。 If you do pass in an argument, it can either be a string holding a full path to the workbook you wish to act as the template for the new workbook, or an Excel.XlWBATemplate constant such as Excel.XlWBATemplate.xlWBATChart or Excel.XlWBATemplate.xlWBATWorksheet to determine the kind of workbook to be created. 如果您确实传入了一个参数,则它可以是一个字符串,其中包含要用作新工作簿模板的工作簿的完整路径,也可以是Excel.XlWBATemplate常量,例如Excel.XlWBATemplate.xlWBATChartExcel.XlWBATemplate.xlWBATWorksheet确定要创建的工作簿的类型。 Again, in general, this parameter should be omitted by passing in Type.Missing . 同样,通常,应通过传入Type.Missing来省略此参数。 (For more on this, see help on the Workbooks.Add Method .) (有关更多信息,请参见Workbooks.Add方法上的帮助。)

Therefore, I suggest that you change your line from: 因此,我建议您将线路更改为:

oWorkbook = (InteropExcel.Workbook)appExcel.Workbooks.Add(true);

to

oWorkbook = (InteropExcel.Workbook)appExcel.Workbooks.Add(Type.Missing);

(6) In general you should be making use of Application and not the ApplicationClass . (6)通常,您应该使用Application而不是ApplicationClass I know that this seems odd because Excel.Application is an interface, and creating a new interface seems paradoxical, but this is how it should be done when automating MS Office programs. 我知道这似乎很奇怪,因为Excel.Application是一个接口,并且创建一个new接口似乎是自相矛盾的,但这是在自动化MS Office程序时应如何做。 (For more on this, see Don't use ApplicationClass (unless you have to) and Excel interop: _Worksheet or Worksheet? .) (有关此内容的更多信息,请参见不要使用ApplicationClass(除非必须使用)Excel互操作:_Worksheet或Worksheet ? 。)

Therefore, your lines: 因此,您的行:

InteropExcel.ApplicationClass appExcel = null;
appExcel = new InteropExcel.ApplicationClass();

should instead be: 相反,应为:

InteropExcel.Application appExcel = null;
appExcel = new InteropExcel.Application();

I don't know which of these issues is causing your problem, probably a few of them are in play here, but all of them should be checked and/or corrected. 我不知道这些问题中的哪个导致了您的问题,可能其中一些正在起作用,但是应检查和/或更正所有这些问题。 Hopefully one of these will correct your problem. 希望其中之一可以解决您的问题。

If none of these fixes it, you should post back with some more information on all of these items that I have highlighted, and/or include anything else you may have figured out in the mean time. 如果这些方法都不能解决问题,则应针对我已突出显示的所有这些项目回发更多信息,和/或同时提供您可能发现的其他内容。

I've got my fingers crossed for you... 我为你用手指交叉了...

Mike 麦克风

SpreadsheetGear for .NET is easier to use and deploy from .NET applications (including console applications) and much faster than Excel via COM Interop. SpreadsheetGear for .NET通过.NET应用程序(包括控制台应用程序)更易于使用和部署,并且比通过COM Interop的Excel更快。

You can download a free trial here . 您可以在此处下载免费试用版。

Disclaimer: I own SpreadsheetGear LLC 免责声明:我拥有SpreadsheetGear LLC

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

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