如何使用VB.NET将XLS中的单个工作表转换为PDF

时间:2011-07-06 22:53:13

标签: vb.net excel pdf

我有工作代码将xls转换为pdf,但是,代码转换整个工作簿,我真的只需要从工作簿中选择一个工作表,但我无法弄清楚如何做到这一点。

我目前使用的代码是:

Dim fileName As String = "filepath\filename"
Dim xlsApp = New Microsoft.Office.Interop.Excel.Application
xlsApp.ScreenUpdating = False
Dim xlsBook As Microsoft.Office.Interop.Excel.Workbook
Dim paramExportFormat As XlFixedFormatType = XlFixedFormatType.xlTypePDF
Dim paramExportQuality As XlFixedFormatQuality = XlFixedFormatQuality.xlQualityStandard
Dim paramOpenAfterPublish As Boolean = False
Dim paramIncludeDocProps As Boolean = True
Dim paramIgnorePrintAreas As Boolean = True
Dim paramFromPage As Object = Type.Missing
Dim paramToPage As Object = Type.Missing
xlsBook = xlsApp.Workbooks.Open(fileName & ".xls", UpdateLinks:=False, ReadOnly:=False)
xlsBook.ExportAsFixedFormat(paramExportFormat, fileName & ".pdf", paramExportQuality, paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage, paramToPage, paramOpenAfterPublish)
xlsBook.Close(SaveChanges:=False)
xlsApp.Quit()

在代码中我需要在工作簿中指定工作表吗?请注意,我需要这样做,以便取决于输入到应用程序的选项,表格将会改变,但我不认为这应该有所作为,但我想我会提到它。

1 个答案:

答案 0 :(得分:3)

尝试使用ExportAsFixedFormat方法

Private Sub SaveWorksheetAsPDF()
    Dim mySheet As Microsoft.Office.Tools.Excel.Worksheet = xlsBook(0)

    mySheet.ExportAsFixedFormat( _
        Excel.XlFixedFormatType.xlTypePDF, _
        "c:\myWorksheet", _
        Excel.XlFixedFormatQuality.xlQualityStandard, _
        True, _
        True, _
        1, _
        10, _
        False)
End Sub