In this article, I will introduce how to convert an Excel file to popular file formats like PDF, Image, HTML, CSV, XML and XPS in Java using Spire.XLS for Java library.
Spire.XLS for Java is a standalone Excel library that empowers developers to create, read, edit, convert and print Excel files in Java applications without installing Microsoft Office, it supports Excel 97-2003, Excel 2007, Excel 2010, Excel 2013, Excel 2016 (.xls, .xlsx, .xlsb, .xlsm) along with Open Office(.ods) file formats.
Before getting started, please download Spire.XLS for Java package through this link, unzip the package and then import Spire.Xls.jar from lib folder into our application.
For maven projects, please add the following maven dependencies in pom.xml to import Spire.Xls.jar.
Using the code
Before coding, please add the needed namespaces as follows.
Converting Excel to PDF, XML, XPS
To convert an Excel file to PDF, XML and XPS, firstly, we need to instantiate a Workbook object and load the Excel from file using the loadFromFile(String fileName) method.
Secondly, invoking saveToFile(String fileName, FileFormat fileFormat) method to save the Excel file to PDF, XML and XPS format.
Converting Excel to Image, HTML, CSV
The code to convert Excel to Image, HTML and CSV is slightly different from the above code. After loading the Excel from file, we need to get a specific worksheet and then save the worksheet to HTML, Image and CSV.
Note if the data in 1 Excel worksheet come to 2 or more pages after converting, please add the following code to fit the data to 1 page before saving to other file formats.
Converting Excel to popular file formats is very easy with Spire.XLS for Java library. Apart from the file conversion feature, Spire.XLS for Java offers many other Excel manipulation features which are also worth trying, for example, import/export data, create chart, create pivot table, merge Excel files, print Excel files and many more.