vb.net 将 Excel 电子表格另存为 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32575318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:27:26  来源:igfitidea点击:

Save Excel spreadsheet as PDF

vb.netexcelvisual-studiopdfexcel-interop

提问by Ross from Brooklin

I'm trying to save an Excel spreadsheet as a PDF file using Visual Basic. I've found some sample code online (see below) but it has me open a Workbook object which Visual Basic doesn't seem to recognize any more. Suggestions...

我正在尝试使用 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")

回答by nutsch

You can go simpler by using the .ExportAsFixedFormatfunction, e.g.

您可以通过使用该.ExportAsFixedFormat功能来更简单,例如

Dim workbook As New Workbook()
workbook.LoadFromFile("D:\test.xlsx")

workbook.activesheet.ExportAsFixedFormat xlTypePDF, "D:\test.pdf"

回答by SSS

Late-bound version of nutsch's answer..

纳奇答案的后期版本..

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

P.S. I recommend developing with the Office PIA's (so you get Intellisense & help) and then switching to late-binding before publishing, so you aren't locked into a specific version of Office (also, so you don't need to distribute the PIA's with your app)

PS 我建议使用 Office PIA 进行开发(因此您可以获得 Intellisense 和帮助),然后在发布之前切换到后期绑定,这样您就不会被锁定到特定版本的 Office(同样,因此您不需要分发PIA 与您的应用程序)

回答by Hymany

The Workbookis not a class of Excel Interop nor a class of System. This sample code is based on Spire.XLS, a 3rd party component which enables users to manipulate Excel file in .NET. Before you can run the program, you need to download and reference the DLL to your project.

工作簿是不是一类的Excel互操作的,也没有一类系统。此示例代码基于Spire.XLS,这是一个第 3 方组件,使用户能够在 .NET 中操作 Excel 文件。在运行程序之前,您需要下载 DLL 并将其引用到您的项目中。

回答by T.S.

This is just one of the ways of how you can create PDF out of Excel or for that matter - any program that you can access programmatically (to broaden your experience). It is by using virtual printer. You will need

这只是您如何从 Excel 或就此而言创建 PDF 的方法之一 - 您可以以编程方式访问的任何程序(以扩大您的经验)。它是通过使用虚拟打印机。你会需要

  • Download virtual PDF printer (many are available and some are free). Also, understand what type of PDF any given printer creates - raster pdfor vector pdf. Raster creates much larger files.
  • Install it (virtual printer)
  • In code - you usually setup the printer properties and then
  • you, in code, open excel using interop and call Print, in which you set the printer - your virtual PDF printer and depending on the printer, you set option "Print To File". Some virtual printers don't require that because you preset their properties or even (some printers) registry keys, where you set the file name.
  • 下载虚拟 PDF 打印机(许多可用,有些是免费的)。此外,了解任何给定打印机创建的 PDF 类型 -光栅 pdf矢量 pdf。光栅创建更大的文件。
  • 安装它(虚拟打印机)
  • 在代码中 - 您通常设置打印机属性,然后
  • 您在代码中使用互操作打开 excel 并调用Print,在其中设置打印机 - 您的虚拟 PDF 打印机,并根据打印机设置选项“打印到文件”。一些虚拟打印机不需要,因为您预设了它们的属性或什至(某些打印机)注册表项,您可以在其中设置文件名。

Code examples are usually available from printer vendors. And there are more on different related blogs. Google "virtual printer for .net"

代码示例通常可从打印机供应商处获得。在不同的相关博客上还有更多。谷歌“.net 虚拟打印机”

And what I think, if you using older interop/excel (sorry, I can't really tell the version cut off) - this is only way to do it.

我的想法是,如果您使用较旧的互操作/excel(抱歉,我无法确定版本已被切断)-这是唯一的方法。