使用VB.NET将Excel转换为PDF的代码

时间:2013-11-06 06:22:32

标签: c# vb.net excel sql-server-2005 ssis

我有一些VB代码可以在SCRIPT TASK中将.xlsx转换为.pdf。 当我运行任务时,它运行成功没有任何错误。 但Excel文件未转换为pdf格式。

我已将Microsoft.Office.Interop.Excel引用添加到该任务中。 请查看以下代码。 我不知道这是什么错误。

请帮忙。 提前谢谢。

Imports Excel = Microsoft.Office.Interop.Excel
Imports System.IO
Imports System.Data.SqlClient
Imports System.Text
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.SqlServer.Dts.Runtime


<System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _
<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

Enum ScriptResults
    Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
    Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
End Enum

Public Sub Main()

    Dim oApplication As ApplicationClass = New ApplicationClass()
    Dim oWorkbook As Workbook = Nothing
    Dim PDFFile As String = "D:\Convert XL to PDF\Test.pdf"
    Dim pFormatType As XlFixedFormatType = XlFixedFormatType.xlTypePDF
    Dim pQuality As XlFixedFormatQuality = XlFixedFormatQuality.xlQualityMinimum
    Dim pIncludeDocProperties As Boolean = True
    Dim pIgnorePrintAreas As Boolean = True
    Dim pFrom As Object = Type.Missing
    Dim pTo As Object = Type.Missing
    Dim pOpenAfterPublish As Boolean = False

    Try
        oWorkbook = oApplication.Workbooks.Open("D:\Convert XL to PDF\convert.xlsx")
        Dim oWorksheet As Excel.Worksheet
        oWorksheet = oApplication.Worksheets(1)
        oWorksheet.PageSetup.FitToPagesWide = 1
        oWorksheet.PageSetup.FitToPagesTall = 1
        oWorksheet.PageSetup.Zoom = False
        If Not oWorkbook Is Nothing Then
            oWorkbook.ExportAsFixedFormat(pFormatType, PDFFile, pQuality, _
                                          pIncludeDocProperties, _
                                          pIgnorePrintAreas, _
                                          pFrom, pTo, pOpenAfterPublish)
        End If
    Catch ex As Exception

    End Try

    If Not oWorkbook Is Nothing Then
        oWorkbook.Close(False)
        oWorkbook = Nothing
    End If

    If Not oApplication Is Nothing Then
        oApplication.Quit()
        oApplication = Nothing
    End If

    GC.Collect()
    GC.WaitForPendingFinalizers()

    Dts.TaskResult = ScriptResults.Success
End Sub
End Class

0 个答案:

没有答案