Java Convert Excel (XLSX/XLS) to HTML and Vice Versa [Pro Guide]

Casie LiuCasie Liu
4 min read

Excel is a popular spreadsheet format for data analysis, while HTML is used to create and display web content. Although they serve different purposes, converting between Excel and HTML is sometimes necessary—for example, to display spreadsheet data on a website or turn HTML content into structured Excel files. This article shows how to achieve that using Java, including how to convert an Excel file to HTML, export a specific worksheet with images, and convert HTML files back to Excel in XLS or XLSX format.

Install Free Python Library

To get started, you need to install Free Spire.XLS for Java, a free version of the Spire.XLS library that supports various Excel operations with certain limitations on the number of pages. You can choose one of the following installation methods:

  • Download the JAR package manually from the official product page, then add the JAR files to your project’s classpath.

  • Use Maven by adding the necessary dependency to your pom.xml. This is a convenient option if you're working with a Maven-based project.

Both methods allow you to quickly integrate the library and start handling Excel files with ease.

Directly Convert Excel (XLSX/XLS) to HTML in Python

In most cases, directly converting an Excel file to HTML is sufficient for display or sharing purposes. With the Workbook class, you can easily achieve this using the saveToFile(String fileName, FileFormat.HTML) method. This allows you to convert a loaded Excel document into an HTML file with just a single line of code.

Below is the complete example demonstrating how it works:

import com.spire.xls.*;

public class ToHtmlwithImage {
   public static void main(String[] args) {
       // Create a Workbook instance and load the Excel file
       Workbook wb = new Workbook();
       wb.loadFromFile("/input/Population.xlsx");


       // Convert the workbook to HTML
       wb.saveToFile("/output/ExceltoHtml.html",FileFormat.HTML);
   }
}

Convert XLSX to HTML in Java

💡
If you prefer using Python, feel free to check out my blog post that explains how to convert HTML to Excel and vice versa using Python.

Convert a Specific Worksheet to HTML with Embedded Images

In addition to converting the entire Excel workbook to an HTML file, you can also export a specific worksheet. This can be done using the saveToHtml() method of the Worksheet class.

To ensure that any images in the worksheet are embedded directly into the HTML output, you can use the HTMLOptions class and set setImageEmbedded(true). This makes the HTML file self-contained and easier to share.

Here’s how to do it in code:

import com.spire.xls.*;
import com.spire.xls.core.spreadsheet.HTMLOptions;

public class ExcelToHtml {
   public static void main(String[] args) {
       // Create an instance of Workbook class and load an Excel file
       Workbook wb = new Workbook();
       wb.loadFromFile("/input/Population.xlsx");

       // Get the second worksheet
       Worksheet sheet = wb.getWorksheets().get(1);

       // Set embedded image
       HTMLOptions options = new HTMLOptions();
       options.setImageEmbedded(true);

       // Save the worksheet to HTML
       sheet.saveToHtml("/output/SheetToHtml.html",options);

   }
}

Convert Excel to HTML with Pictures Embedded

Convert HTML to Excel (XLS/XLSX) in Python Easily

The free Excel library also supports converting HTML files back into Excel format. You can load an HTML file using the loadFromHtml() method of the Workbook class, and then save it as an Excel file in either XLS or XLSX format using the saveToFile() method.

This is useful when you need to extract structured data from web pages or HTML reports.

Here’s the code example of converting an HTML file to an Excel workbook:

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;

public class ConvertHtmlToExcel {
   public static void main(String[] args) {

       // Create a Workbook object and load HTML file
       Workbook workbook = new Workbook();
       workbook.loadFromHtml("/sales_report.html");

       // Save HTML as Excel XLSX or XLS
       workbook.saveToFile("/output/HtmltoXLSX.xlsx", ExcelVersion.Version2016);
       workbook.saveToFile("/output/HtmltoXLS.xls", ExcelVersion.Version97to2003);
       workbook.dispose();

   }

}

Convert HTML to XLSX in Java

The Conclusion

Converting between Excel and HTML formats in Java is straightforward with the help of Free Spire.XLS for Java. Whether you need to export entire workbooks, specific worksheets with images, or turn HTML content into Excel files, this library provides simple methods to handle each task effectively.

Check out my homepage to explore more tutorials! Feel free to leave a comment—I'd love to hear your thoughts or help with any questions you have!

FAQs about Converting Excel to HTML and Vice Versa

1. How do I convert an HTML file to Excel using Java?

You can use the loadFromHtml() method from Spire.XLS for Java to load an HTML file, and then save it as an Excel file using saveToFile() in either .xls or .xlsx format.

2. Can I export only one sheet from Excel to HTML with images?

Yes. You can select a specific worksheet and use its saveToHtml() method. To embed images directly in the HTML file, set HTMLOptions.setImageEmbedded(true) before exporting.

3. How do I convert Excel into a web-friendly HTML page?

Use the Workbook.saveToFile(fileName, FileFormat.HTML) method to convert your Excel workbook into a clean HTML file that can be easily displayed in any browser.

0
Subscribe to my newsletter

Read articles from Casie Liu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Casie Liu
Casie Liu