我試圖將Excel電子表格保存爲使用Visual Basic的PDF文件。我在網上找到了一些示例代碼(見下文),但是它讓我打開了一個Visual Basic似乎無法識別的Workbook對象。建議...將Excel電子表格保存爲PDF
' load Excel file
Dim workbook As New Workbook()
workbook.LoadFromFile("D:\test.xlsx")
' Set PDF template
Dim pdfDocument As New PdfDocument()
pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape
pdfDocument.PageSettings.Width = 970
pdfDocument.PageSettings.Height = 850
'Convert Excel to PDF using the template above
Dim pdfConverter As New PdfConverter(workbook)
Dim settings As New PdfConverterSettings()
settings.TemplateDocument = pdfDocument
pdfDocument = pdfConverter.Convert(settings)
' Save and preview PDF
pdfDocument.SaveToFile("sample.pdf")
System.Diagnostics.Process.Start("sample.pdf")
您需要安裝PDF庫。例如PDFSharp – SSS
謝謝SSS。我得到的錯誤是在Dim workbook As New Workbook()行中。它表示Workbook類不存在。這是我需要建立的課程嗎?我認爲這可能是一個系統課。 –
Workbook類是Interop DLL for Excel的一部分,它不是.NET中System名稱空間的一部分。有關如何引用Excel Interop的更多信息:http://stackoverflow.com/questions/21817898/where-can-i-find-microsoft-office-interop-excel-in-the-assembly-com-browser –