Convert Excel Spreadsheet to PDF in Java

When we want to share the data in worksheets that do not require editing, we often convert such Excel workbooks or specific sheets to PDF format. In this article, we will learn 4 different ways of converting Excel spreadsheets into PDF format in Java using the document conversion API.

Each conversion approach can be adopted with a little change in the code while loading the spreadsheets or with some tweaks in conversion options of the PDF format. Some of the possible ways to convert the spreadsheets in the article are as follows.

Java API for Converting Excel Files to PDF

GroupDocs.Conversion provides Java API that allows the many Excel file-formats including XLS, XLSX to convert into PDF format. In this article, we will use its GroupDocs.Conversion for Java. In addition to spreadsheets, the API supports the conversion of word-processing documents, presentations, eBooks, images, etc that are mentioned in the documentation.

You can download the JAR file from the downloads section or use the latest repository and dependency Maven configurations within your Java applications.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-conversion</artifactId>
        <version>21.10</version> 
</dependency>

Convert All Sheets of Excel Files to PDF - Java

The simplest way is to convert all the sheets to PDF format. The following steps convert the complete workbook (all sheets) to PDF format in Java.

The following are the 2 lines of Java source code that convert the complete Excel workbook to PDF.

Convert PDF from Excel Data

Successive Excel Sheets Conversion to PDF - Java

If you are interested in extracting the subset of spreadsheets in sequence, you can easily do it by providing starting sheet number and the number of successive sheets. The following are the steps to convert any sub-sequence of the Excel workbook sheet(s) into PDF format in Java.

  • Load the spreadsheet using the Converter.
  • Define the PDF conversion options using PdfConvertOptions.
  • Set the starting sheet number and the count of subsequent sheets.
  • Convert the selected sheets into PDF according the settings using convert() method.

The following is the Java code snippet that converts the first two sheets to PDF. (Sheet numbers 1 and 2)

Specific Excel Sheets to PDF Conversion - Java

If you are thinking to convert random sheets (like sheet numbers 1, 2, 4, 7, …), you can simply provide the exact sheet numbers as a list while converting. The following are the steps for converting any specific list of sheet numbers into PDF format in Java.

  • Load the Excel file using the Converter.
  • Provide the exact sheets numbers as a list using PdfConvertOptions.
  • Convert the listed sheets into PDF format using the convert() method.

The following code snippet converts the sheet numbers 1 and 3 to PDF format.

Convert the Cell Range of Excel Sheet to PDF - Java

Here is the unusual way to convert any segment of the spreadsheet(s). We can convert any cell ranges of Excel sheet(s) in almost a similar way as other approaches discussed above. The following are the steps for converting the cell range of workbook sheet(s) into PDF format in Java.

  • Define the cell range of the sheets.
  • Load the spreadsheet file.
  • Select the sheets either by providing successive sheets range or the exact sheet numbers.
  • Convert the sheets into PDF format according to settings.

The following code converts the cell range (A1:C20) of sheet number 1 to PDF format in Java.

Get a Free API License

You can get a free temporary license in order to use the API without the evaluation limitations.

Conclusion

Let’s sum up what we learned today. We learned four different ways for converting Excel spreadsheets into PDF format in Java using GroupDocs.Conversion. Initially, we converted the complete workbook into PDF format, then the consecutive sheets. Next, multiple sheets were changed to PDF by listing their exact sheet numbers. Finally, we obtained the PDF file from the selected cell range of the selected sheet(s).

Learn more about GroupDocs.Conversion APIs from the documentation. For queries, contact us via the forum.

See Also