为什么Excel.WorkSheet.Copy抛出异常的第一次机会异常类型'System.Runtime.InteropServices.COMException'与HRESULT:0x800A03EC [英] Why Excel.WorkSheet.Copy throws exception of A first chance exception of type 'System.Runtime.InteropServices.COMException' with HRESULT: 0x800A03EC

查看:2155
本文介绍了为什么Excel.WorkSheet.Copy抛出异常的第一次机会异常类型'System.Runtime.InteropServices.COMException'与HRESULT:0x800A03EC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在.NET中使用Excel Interop,在Excel._WorkSheet.Copy(Type.Missing,Type.Missing)中遇到COMException。



行为非常奇怪:我正在从基于.xls的模板复制工作表,并使用指定的工作表(可能由用户从GUI选择)生成新的工作簿。一旦遇到Excel._Worksheet WorkSheet.Copy(Type.Missing,Type.Missing),它会抛出以下COMException。



发生了System.Runtime.InteropServices.COMException类型的第一次机会异常。



其他信息:HRESULT异常:0x800A03EC



在尝试不同的方式后,我将手动指定的工作表复制到另一个新创建的Excel实例,并将此工作簿作为源代码添加到相同的代码。在这里,它工作正常!它通过打开一个新的excel实例创建了一个精确的副本,我根本没有例外。



我不知道为什么会发生这种行为?它是否与工作表内的内容相关,如果是这样,相同和精确的工作表被复制到另一个新创建的空白工作表,一切正常。



我必须生产报告用户给出的指定工作表,所以我必须打开模板化的工作簿,从那里我需要复制这些指定的工作表,然后这些工作表将被填入指定的数据。



我使用以下代码:

  Excel._Worksheet WorkSheet = null; 

Excel.Application excel = new Excel.Application();
excel.Visible = true;
excel.DisplayAlerts = false;

_ExcelWorkbook = excel.Workbooks.Open(a_WorkbookName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,类型。型号,型号,型号,型号,型号,型号,

WorkSheet =(Excel.Worksheet)_ExcelWorkbook.Sheets [1];
WorkSheet.Copy(Type.Missing,Type.Missing); //抛出COM异常0x800A03EC。

Regards
Usman

解决方案

我相信你需要声明一个来源,然后是一个目的地。看来你只是声明一个来源。

  Excel.Worksheet wksh1 =((Excel.Worksheet)Application.ActiveWorkbook.Worksheets [ 1]); 
Excel.Worksheet wksh2 =((Excel.Worksheet)Application.ActiveWorkbook.Worksheets [2]);
wksh1.Copy(wksh2);

编辑:



Application.ActiveWorkbook 只是可以在此代码模式中使用的工作簿对象的示例。它本质上是Excel的this指针,因为它是重点工作簿,但是ThisWorkbook引用了当前正在运行VBA代码的工作簿,因此ActiveWorkbook是语法。 请参阅此处的规格和<关于Workbook界面的更多信息,请参阅href =https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbook.aspx =nofollow> here 。



要定位其他工作簿,您可以声明所需的特定工作簿。

  Excel.Application excel = new Excel.Application(); 
excel.xlWorkbook = excel.Workbooks.Add(1);
工作簿newWorkbook = excel.Workbooks [1];
工作簿oldWorkbook = excel.Workbooks.Open(fileLocation);
Excel.Worksheet newWorksheet =((Excel.Worksheet)newWorkbook.Worksheets [1]);
Excel.Worksheet oldWorksheet =((Excel.Worksheet)oldWorkbook.Worksheets [1]);
oldWorksheet.Copy(newWorksheet);


I am playing around with Excel Interop in .NET where I am encountering COMException over "Excel._WorkSheet.Copy(Type.Missing,Type.Missing)".

The behavior is very strange : I am copying worksheets from a template based .xls and generating a new workbook with specified worksheets( probably selected by user from GUI ). As soon as it encounters Excel._Worksheet WorkSheet.Copy(Type.Missing,Type.Missing), it throws following COMException.

"A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred.

Additional information: Exception from HRESULT: 0x800A03EC"

After experimenting with different way, I copied manually specified worksheet to another newly created instance of Excel and given this workbook as source to the same code. Here, it worked fine! It created an exact copy by opening a new excel instance and I had no exception at all.

I don't know why this behavior is occuring? Is it related to the contents inside that worksheet, If so, same and exact worksheet is being copied to another newly created blank worksheet and everything worked fine.

I have to produce reports for the specified worksheets given by the user, So I have to open the templated workbook from where, I need to copy those specified worksheets and later those worksheets would be filled in with the specified data.

I am using the following code :

        Excel._Worksheet WorkSheet = null;

        Excel.Application excel = new Excel.Application();
        excel.Visible = true;
        excel.DisplayAlerts = false;

       _ExcelWorkbook = excel.Workbooks.Open(a_WorkbookName, Type.Missing,         Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,true);

        WorkSheet = (Excel.Worksheet)_ExcelWorkbook.Sheets[1];
        WorkSheet.Copy(Type.Missing, Type.Missing); // throws COM Exception of 0x800A03EC.

Regards Usman

解决方案

I believe you need to declare a source and then a destination. It appears you are only declaring a source.

Excel.Worksheet wksh1 = ((Excel.Worksheet)Application.ActiveWorkbook.Worksheets[1]);
Excel.Worksheet wksh2 = ((Excel.Worksheet)Application.ActiveWorkbook.Worksheets[2]);
wksh1.Copy(wksh2);

EDIT:

Application.ActiveWorkbook is only an example of a workbook object that can be used in this code pattern. It is essentially a 'this' pointer for Excel in that it is the workbook in focus, but 'ThisWorkbook' references the workbook that is currently running VBA code so 'ActiveWorkbook' is the syntax. See spec here and here for more on the Workbook interface.

To target other workbooks, you can declare the specific workbook you want.

Excel.Application excel = new Excel.Application();
excel.xlWorkbook = excel.Workbooks.Add(1);
Workbook newWorkbook = excel.Workbooks[1];
Workbook oldWorkbook = excel.Workbooks.Open(fileLocation);
Excel.Worksheet newWorksheet = ((Excel.Worksheet)newWorkbook.Worksheets[1]);
Excel.Worksheet oldWorksheet = ((Excel.Worksheet)oldWorkbook.Worksheets[1]);
oldWorksheet.Copy(newWorksheet);

这篇关于为什么Excel.WorkSheet.Copy抛出异常的第一次机会异常类型'System.Runtime.InteropServices.COMException'与HRESULT:0x800A03EC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆