Java convert Excel to PDF

This tutorial helps to demonstrate how to convert Excel to PDF using Spire.XLS for Java from the following two parts:

  • Convert the whole Excel workbook to PDF.
  • Convert a single Excel worksheet to PDF.

Spire.XLS for Java offers a method of workbook.saveToFile() to save the whole Excel workbook to PDF in Java.

import com.spire.xls.*;

public class ExceltoPDF {
    public static void main(String[] args) {
        //Create a Workbook
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");

        //Fit to page
        workbook.getConverterSetting().setSheetFitToPage(true);

        workbook.saveToFile("ToPDF.pdf",FileFormat.PDF);
    }
}

Effective screenshot of converting Excel workbook to PDF:

Java convert Excel to PDF

Spire.XLS for Java offers a method of worksheet.saveToPdf() to save a single Excel worksheet to PDF in Java.

import com.spire.xls.*;

public class ExceltoPDF {
    public static void main(String[] args) {
        //Create a Workbook
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet worksheet = workbook.getWorksheets().get(0);

        worksheet.saveToPdf("ToPDF2.pdf");
    }
}

Effective screenshot of converting Excel worksheet to PDF:

Java convert Excel to PDF