VB.NET ExcelファイルからPDF出力
VB.NETでExcelからPDF出力するときに、以前はBullzipを使用してPDF化していた。
(Excelを開いて、BullzipPrinterに印刷形でPDF化)(
Office2007以降からExcel自体にPDF出力機能が追加されたので簡単にPDF出力できるようになった。またPDF化する速度が速い。
[参考コード]
Private Function OpenExcelToPDF() As Boolean
Dim rtn As Boolean = False
Dim fn As String = "C:\temp\aaa.xls"
Dim xlApp As Object
xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
Try
Dim xlBooks As Object = xlApp.Workbooks
Dim xlBook As Object = xlBooks.Open(fn)
Dim xlSheets As Object = xlBook.Sheets
Dim newFn As String = "C:\temp\bbb.pdf"
xlBook.ExportAsFixedFormat(Type:=0, _
Filename:=newFn, _
Quality:=0, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False)
xlBook.Save()
If Not xlSheets Is Nothing Then
Try
Finally
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets)
End Try
End If
If Not xlBook Is Nothing Then
Try
xlBook.Close()
Finally
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
End Try
End If
If Not xlBooks Is Nothing Then
Try
Finally
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks)
End Try
End If
rtn = True
Catch ex As Exception
errmsg = ex.Message
Console.WriteLine("ERROR! ->" & errmsg)
Finally
If Not xlApp Is Nothing Then
Try
xlApp.Quit()
Finally
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
End Try
End If
End Try
Return rtn
End Function
最近のコメント