C# Convert Excel to PDF

2023-10-27 09:33:21

Installed via NuGet

PM> Install-Package Spire.XLS

Related Links

By converting an Excel file to PDF format, anyone can open the file even when there is no Office installed in the system. Also, converting Excel documents to PDF is useful as PDF files can be easily shared and printed. In this article, you will learn how to convert a whole Excel document or a specific worksheet into PDF by using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Convert a Whole Excel Document to PDF

The following are the steps to convert a whole Excel document to PDF using Spire.XLS for .NET.

  • Create a Workbook object.
  • Load a sample Excel document using Workbook.LoadFromFile() method.
  • Set the Excel to PDF conversion options through the properties under the ConverterSetting class.
  • Convert the whole Excel document to PDF using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;
    
    namespace ConvertExcelToPDF
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Create a Workbook instance
                Workbook workbook = new Workbook();
    
                //Load a sample Excel file
                workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.xlsx");
    
                //Set worksheets to fit to page when converting
                workbook.ConverterSetting.SheetFitToPage = true;
    
                //Save to PDF
                workbook.SaveToFile("ExcelToPdf.pdf", FileFormat.PDF);
            }
        }
    }

C#/VB.NET: Convert Excel to PDF

Convert a Specific Worksheet to PDF

The following are the steps to convert a specific worksheet to PDF using Spire.XLS for .NET.

  • Create a Workbook object.
  • Load a sample Excel document using Workbook.LoadFromFile() method.
  • Set the Excel to PDF conversion options through the properties under the ConverterSetting class.
  • Get a specific worksheet through the Workbook.Worksheets[index] property.
  • Convert the worksheet to PDF using Worksheet.SaveToPdf() method.
  • C#
  • VB.NET
using Spire.Xls;
    
    namespace ConvertWorksheetToPdf
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Create a Workbook instance
                Workbook workbook = new Workbook();
    
                //Load a sample Excel file
                workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.xlsx");
    
                //Set worksheets to fit to page when converting
                workbook.ConverterSetting.SheetFitToPage = true;
    
                //Get the first worksheet
                Worksheet worksheet = workbook.Worksheets[0];
    
                //Save to PDF
                worksheet.SaveToPdf("WorksheetToPdf.pdf");
            }
        }
    }

C#/VB.NET: Convert Excel to PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

See Also