我正在尝试使用Visual Basic将Excel电子表格另存为PDF文件。我在网上找到了一些示例代码(见下文),但它让我打开了一个Visual Basic似乎不再识别的Workbook对象。建议...
' 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")发布于 2015-09-15 07:29:24
使用.ExportAsFixedFormat函数可以更简单,例如
Dim workbook As New Workbook()
workbook.LoadFromFile("D:\test.xlsx")
workbook.activesheet.ExportAsFixedFormat xlTypePDF, "D:\test.pdf"发布于 2015-09-15 11:16:50
nutsch的答案的后期版本..
Option Strict Off 'Required for Late Binding
Module XL
Sub ExcelPDF()
Dim xl As Object
xl = CreateObject("Excel.Application")
Dim xwb As Object = xl.Workbooks.Open("D:\test.xlsx")
xwb.ActiveSheet.ExportAsFixedFormat(0, "D:\sample.pdf")
xl.Quit()
End Sub
End Module附注:我建议使用Office PIA进行开发(这样你就可以获得Intellisense & help),然后在发布之前切换到后期绑定,这样你就不会被某个特定版本的Office所束缚(而且,你也不需要在你的应用中分发PIA)
发布于 2015-09-15 10:51:44
这只是你在Excel之外创建PDF的方法之一,或者就这一点而言-任何你可以通过编程访问的程序(以扩展你的经验)。它是通过使用虚拟打印机。您将需要
Print,在其中设置打印机-您的虚拟PDF打印机,并根据打印机的不同,设置选项“打印到文件”。有些虚拟打印机不需要这样做,因为您预先设置了它们的属性,甚至(某些打印机)注册表项,您可以在其中设置文件名。代码示例通常可以从打印机供应商处获得。在不同的相关博客上有更多。谷歌“.net虚拟打印机”
我的想法是,如果你使用旧的interop/excel (对不起,我真的不能告诉你截止的版本)--这是唯一的方法。
https://stackoverflow.com/questions/32575318
复制相似问题