.Net Interop Excel ExportAsFixedFormat()异常HResult:0x800A03EC [英] .Net Interop Excel ExportAsFixedFormat() Exception HResult: 0x800A03EC

查看:245
本文介绍了.Net Interop Excel ExportAsFixedFormat()异常HResult:0x800A03EC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在实现一种方法,该方法可以生成多张图纸并将其导出为PDF.为此,我将Microsoft.Office.Interop库(v14.0.0.0)与.NET 4.5.2结合使用.正在运行的办公室是2016年

I'm currently implementing a method that generated multiple sheets and export them as PDF. For this I'm using the Microsoft.Office.Interop Library (v14.0.0.0) with .NET 4.5.2 . Running Office is 2016

我的代码:

Dim excel As New Application()
excel.Visible = False
excel.DisplayAlerts = False
Dim workbooks As Workbooks
workbooks = excel.Workbooks
Dim workbook As Workbook = workbooks.Add(Type.Missing)

[...]

workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, String.Format(<a Path>)
ReleaseComObject(workSheet)
workbook.Close()
ReleaseComObject(workbook)
excel.Quit()
ReleaseComObject(excel)

ReleaseComObject()看起来像这样(根据

The ReleaseComObject() looks like this (according to Microsoft Support):

Private Sub ReleaseComObject(objectToRelease As Object)
        While System.Runtime.InteropServices.Marshal.ReleaseComObject(objectToRelease) > 0
        End While
        objectToRelease = Nothing
End Sub

如果我将此代码运行一次迭代,则效果很好.但是,我注意到EXCEL-Process仍在运行.

This works fine if I run this code for one iteration BUT I noticed that the EXCEL-Process is still running.

如果我尝试以批处理方式执行此操作(即for循环的意思),则在进入第二次交互时会感到兴奋:

If I try to do this in batch-mode (in the meaning of a for-loop) I get an excetion when entering the 2nd interation:

System.Runtime.InteropServices.COMException(0x800A03EC):Ausnahme von HRESULT:0x800A03EC bei Microsoft.Office.Interop.Excel.WorkbookClass.ExportAsFixedFormat(XlFixedFormatType类型,对象文件名,对象质量,对象IncludeDocProperties,对象IgnorePrintAreas,对象来自,对象到,对象OpenAfterPublish,对象FixedFormatExtClassPtr) bei Controller.CreateListing(DataTable数据,Int32年,字符串mandantShortName)位于...

System.Runtime.InteropServices.COMException (0x800A03EC): Ausnahme von HRESULT: 0x800A03EC bei Microsoft.Office.Interop.Excel.WorkbookClass.ExportAsFixedFormat(XlFixedFormatType Type, Object Filename, Object Quality, Object IncludeDocProperties, Object IgnorePrintAreas, Object From, Object To, Object OpenAfterPublish, Object FixedFormatExtClassPtr) bei Controller.CreateListing(DataTable data, Int32 year, String mandantShortName) in ...

抛出异常的行:

workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, String.Format(<a Path>)

为了进行研究/测试,我在重新进入循环之前进行了调试,并杀死了excel进程,但是没有任何更改.

For reseach/testing I debugged before reentering the loop and killed the excel-process but w/o any changes.

有人也遇到这个问题吗?解决方案/建议?

Anyone faced this problem as well? Solutions/Suggestions?

推荐答案

为了解决Excel无法正确关闭的问题,请替换:

In order to address the issue with Excel not closing properly replace:

Private Sub ReleaseComObject(objectToRelease As Object)
    While System.Runtime.InteropServices.Marshal.ReleaseComObject(objectToRelease) > 0
    End While
    objectToRelease = Nothing
End Sub

使用代码,如您可以使用您的While语句而不是Do语句,但是我觉得这样更好.重要的地方是GC.Collect().

You could use your While statement instead of the Do statement but I feel it reads better this way. The important bit here is GC.Collect().

您还必须确保以正确的顺序发布并发布所有内容.通常按向后的顺序.因此,根据您的情况,先从workSheet开始,然后依次是workbookworkbooks,最后是excel:

You also have to make sure you release in the right order and release everything. This is usually in backwards order. So in your case, start off with the workSheet then the workbook then workbooks and then lastly excel:

ReleaseObject(workSheet)
workbook.Close()
ReleaseObject(workbook)
ReleaseObject(workbooks)
excel.Quit()
ReleaseObject(excel)

这是我一起测试的代码:

This is the code I put together to test:

Dim app As New Excel.Application()
app.Visible = False
app.DisplayAlerts = False

Dim wbs As Excel.Workbooks = app.Workbooks
Dim wb As Excel.Workbook = wbs.Add()
Dim ws As Excel.Worksheet = CType(wb.Sheets(1), Excel.Worksheet)

ReleaseObject(ws)
wb.Close()
ReleaseObject(wb)
ReleaseObject(wbs)
app.Quit()
ReleaseObject(app)

该过程开始,一旦调用ReleaseObject(app),该过程就关闭.

The process starts and once ReleaseObject(app) has been called the process then closes.

这篇关于.Net Interop Excel ExportAsFixedFormat()异常HResult:0x800A03EC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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