【发布时间】:2020-05-07 12:49:27
【问题描述】:
我在 Visual Studio 中的 VB.Net 程序创建 Excel 文件,然后将它们保存为 PDF,但经过这么多(30、40 或其他,不是重点)后,它会出错说“错误 System.Runtime.InteropServices.COMException:' HRESULT 异常:0x800A03EC'"。
可能我认为这是 Excel 关闭/发布的方式?
在我打开 Excel 文档并尝试将其另存为 PDF 后,错误来自这行代码。同样,这总是在某些文档已经保存为 PDF(有时 30,有时 50...)之后发生:
xwb.ActiveSheet.ExportAsFixedFormat(0, "\\ken-resourcesan\fileshares\fieldshare\IT\nsantagata\ARStatements_CustomerInvoicesPDF\" & originalCustomerName & " " & customerNumber & " " & todaysDate & ".pdf")
这是我的全部代码:
这将创建 Excel 文档并用数据填充它:
Public Sub PopulateSheet(ByVal dt As Data.DataTable, ByVal File As String)
Dim oXL As Excel.Application = CType(CreateObject("Excel.Application"), Excel.Application)
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
oWB = oXL.Workbooks.Add
oSheet = CType(oWB.ActiveSheet, Excel.Worksheet)
'****Spreadsheet gets populated
......
'****Then
oWB.SaveAs(File)
oRng = Nothing
oXL.Quit()
GC.Collect()
GC.WaitForPendingFinalizers()
Marshal.FinalReleaseComObject(oXL)
Marshal.FinalReleaseComObject(oSheet)
Marshal.FinalReleaseComObject(oWB)
oSheet = Nothing
oWB = Nothing
oXL = Nothing
最后将文档保存为 PDF:
Dim xl As Object
Dim xwb As Object
xl = CreateObject("Excel.Application")
dt = CreateTable()
PopulateSheet(dt, "\\ken-resourcesan\fileshares\fieldshare\IT\nsantagata\ARStatements_CustomerInvoicesExcel\" & originalCustomerName & " " & customerNumber & " " & todaysDate & ".xlsx")
'****Open xlsx doc to save as pdf
xwb = xl.Workbooks.Open("\\ken-resourcesan\fileshares\fieldshare\IT\nsantagata\ARStatements_CustomerInvoicesExcel\" & originalCustomerName & " " & customerNumber & " " & todaysDate & ".xlsx")
xwb.ActiveSheet.PageSetup.Zoom = False
xwb.ActiveSheet.PageSetup.FitToPagesWide = 1
xwb.ActiveSheet.PageSetup.FitToPagesTall = False
'****Save as pdf
xwb.ActiveSheet.ExportAsFixedFormat(0, "\\ken-resourcesan\fileshares\fieldshare\IT\nsantagata\ARStatements_CustomerInvoicesPDF\" & originalCustomerName & " " & customerNumber & " " & todaysDate & ".pdf")
xl.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xl)
xl = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(xwb)
xwb = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
高度赞赏任何帮助。谢谢
********评论:** 我觉得 Excel 在两个 Excel 进程结束时如何释放/关闭存在问题。我认为这是因为程序运行良好并将 Excel 文件保存为 PDF,但每次它都不会创建所有文件。它在有“ExportAsFixedFormat”的行创建了许多 PDF 文件后停止。它永远不会停留在同一个特定文件上,所以我会说任何特定的 PDF 都没有问题。
【问题讨论】:
-
我觉得 Excel 在两个 Excel 进程结束时如何释放/关闭存在问题。我认为这是因为程序运行良好并将 Excel 文件保存为 PDF,但每次它都不会创建所有文件。它在有“ExportAsFixedFormat”的行创建了许多 PDF 文件后停止。它永远不会停留在同一个特定文件上,所以我会说任何特定的 PDF 都没有问题。
标签: excel vb.net pdf visual-studio-2017