【发布时间】:2017-07-12 08:25:41
【问题描述】:
我目前正在实施一种生成多张工作表并将它们导出为 PDF 的方法。为此,我将 Microsoft.Office.Interop Library (v14.0.0.0) 与 .NET 4.5.2 一起使用。运行 Office 是 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() 看起来像这样(根据Microsoft Support):
Private Sub ReleaseComObject(objectToRelease As Object)
While System.Runtime.InteropServices.Marshal.ReleaseComObject(objectToRelease) > 0
End While
objectToRelease = Nothing
End Sub
如果我将此代码运行一次迭代,这会很好但是我注意到 EXCEL 进程仍在运行。
如果我尝试在批处理模式下执行此操作(在 for 循环的含义中),我会在进入第二次交互时得到一个异常:
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 data, Int32 year, String mandantShortName) in ...
抛出异常的行:
workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, String.Format(<a Path>)
对于研究/测试,我在重新进入循环之前进行了调试并终止了 excel 进程,但没有任何更改。
有人也遇到过这个问题吗?解决方案/建议?
【问题讨论】:
标签: vb.net excel-interop comexception