How to Convert Excel File to Pdf in .NET Console App using NuGet Package

Shehzad Shoukat 0 Reputation points
2024-02-06T11:07:29.67+00:00

I have installed Microsoft Visual Studio Professional 2019. I selected Console App (.NET Framework) Visual Basic. The console application will extract data from the database, write them in an excel file, and then convert the excel file to pdf file. I installed EPPlus NuGet Package that made it easy to develop the Excel file. However, I am not finding any NuGet Package that could convert the developed Excel file to Pdf file. I installed sautinsoft.exceltopdf nuget package to convert xlsx to pdf. It worked successfully but the generated pdf shows SautinSoft.ExcelToPdf and trial in footer. Perhaps it occurs because SautinSoft is a licensed package. Is there any free NuGet package that I could use to convert the generated Excel file to pdf file?

Microsoft 365 and Office Excel For business Windows
Windows for business Windows Client for IT Pros User experience Other
Developer technologies .NET Other
Developer technologies VB
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2024-02-06T14:53:45.41+00:00

    Hi @Shehzad Shoukat ,

    You can consder using Workbook.ExportAsFixedFormat Method to export Excel documents in PDF format.

    Imports Microsoft.Office.Interop.Excel
    
    Public Class ExcelToPDFExporter
    
        Public Sub ExportToPDF(ByVal excelFilePath As String, ByVal pdfSavePath As String)
            Dim excelApp As New Application()
            Dim excelWorkbook As Workbook = excelApp.Workbooks.Open(excelFilePath)
    
            ' Export the workbook to PDF
            excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, pdfSavePath)
    
            ' Close the workbook and quit Excel application
            excelWorkbook.Close(False)
            excelApp.Quit()
    
            ' Release COM objects to avoid memory leaks
            ReleaseComObject(excelWorkbook)
            ReleaseComObject(excelApp)
    
            MessageBox.Show("Excel workbook has been exported to PDF successfully.")
        End Sub
    
        Private Sub ReleaseComObject(ByVal obj As Object)
            Try
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
                obj = Nothing
            Catch ex As Exception
                obj = Nothing
                MessageBox.Show("Exception Occurred while releasing object " & ex.ToString())
            Finally
                GC.Collect()
            End Try
        End Sub
    
    End Class
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Shehzad Shoukat 0 Reputation points
    2024-02-07T06:36:04.2266667+00:00

    Thanks Jiachen Li. This code is very helpful but it uses Office.Interop.Excel. I need to run my script on a server machine and I have been told that Excel might not be available on that server. Office.Interop.Excel requires that Excel is installed on the machine. I searched for free packages and the following two packages are working for me now: EPPlus FreeSpire.XLS Here is my code that I tested and it worked:

    Imports System.IO
    Imports OfficeOpenXml
    Imports Spire.Xls
    
    Sub Main()
            Dim excelFilePath As String = "D:\testing2\SampleExcelFile.xlsx"
            ' Specify the path where you want to save the output PDF file
            Dim pdfFilePath As String = "D:\testing2\SamplePdfFile.pdf"
            ' Create a new Excel package with EPPlus
            Using package As New ExcelPackage()
                ' Add a new worksheet to the workbook
                Dim worksheet = package.Workbook.Worksheets.Add("Sheet1")
                ' Write sample values to the worksheet
                worksheet.Cells("A1").Value = "Name"
                worksheet.Cells("B1").Value = "Age"
                worksheet.Cells("A2").Value = "John"
                worksheet.Cells("B2").Value = 25
                worksheet.Cells("A3").Value = "Jane"
                worksheet.Cells("B3").Value = 100
                ' Save the Excel package to a file
                package.SaveAs(New FileInfo(excelFilePath))
            End Using
            Dim Workbook As New Spire.Xls.Workbook()
            Workbook.LoadFromFile(excelFilePath)      
            
            Workbook.SaveToFile(pdfFilePath, Spire.Xls.FileFormat.PDF)
    End Sub
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.