DEV Community

Jeremy K.
Jeremy K.

Posted on

Java: Convert Excel to HTML (Complete Guide with Examples)

Introduction

In modern web application development, presenting Excel-based reports as web-accessible content is a common requirement—whether for real-time online preview, system embedding, or cross-platform sharing. This guide provides a streamlined, production-ready approach to converting Excel files (.xls/.xlsx) to HTML in Java using Free Spire.XLS for Java (a free, lightweight library). No Microsoft Office installation is required, and the solution supports core use cases like full workbook conversion, targeted worksheet export, and inline image embedding—with clean, maintainable code.

--

Environment Setup

1. Add Dependencies (Maven)

Integrate the library into your Maven project by adding the following repository and dependency to your pom.xml:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue Public Repository</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>

<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.xls.free</artifactId>
    <version>16.3.1</version>
    <scope>compile</scope>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Basic Conversion: Full Workbook to HTML

This method converts an entire Excel workbook to HTML while preserving critical formatting (cell styles, merged cells, images, and multiple worksheets).

Code Implementation

import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;

public class FullWorkbookToHtml {
    public static void main(String[] args) {
        // Initialize workbook and load Excel file
        Workbook workbook = new Workbook();
        try {
            workbook.loadFromFile("sample.xlsx");

            // Save entire workbook as HTML
            workbook.saveToFile("output/full_workbook.html", FileFormat.HTML);
            System.out.println("Full workbook converted to HTML successfully!");
        } catch (Exception e) {
            // Basic error handling for file operations
            e.printStackTrace();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Key Notes

  • The generated HTML includes tab navigation for switching between worksheets (visible at the bottom of the browser).
  • Supports both legacy .xls and modern .xlsx file formats.
  • Added basic exception handling to catch file I/O errors (best practice for production code).

Advanced Conversion: Targeted Worksheet + Inline Images

For use cases where only a specific worksheet (e.g., "Sales Report") needs to be exported—and images should be embedded directly in the HTML (no external links)—use the HTMLOptions class to configure inline image embedding (base64 encoding).

Code Implementation

import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.spire.xls.core.spreadsheet.HTMLOptions;

public class SingleSheetToHtmlWithImages {
    public static void main(String[] args) {
        Workbook workbook = new Workbook();
        try {
            // Load the target Excel file
            workbook.loadFromFile("product.xlsx");

            // Select the first worksheet (0-indexed)
            Worksheet targetSheet = workbook.getWorksheets().get(0);

            // Configure HTML options for inline images
            HTMLOptions htmlOptions = new HTMLOptions();
            htmlOptions.setImageEmbedded(true); // Encode images to base64 and embed in HTML

            // Export the selected worksheet to HTML
            targetSheet.saveToHtml("output/single_sheet.html", htmlOptions);
            System.out.println("Single worksheet converted with inline images!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Key Benefits

  • setImageEmbedded(true) encodes images to base64 and embeds them directly in the HTML—no external image files to manage or share.
  • The 0-indexed worksheet selection aligns with Java’s standard collection indexing (clearer for developers).

Troubleshooting Common Issues

1. Font Rendering Issues

The generated HTML relies on the browser’s font parsing and the host system’s installed fonts.

  • Fix: On Linux servers (a common deployment environment), install core fonts (e.g., fonts-dejavu, fonts-liberation) via the package manager (e.g., apt install fonts-dejavu for Debian/Ubuntu).
  • Best Practice: Use web-safe fonts (e.g., Arial, Calibri) in your Excel templates to ensure consistency.

2. Missing Images in HTML

  • Verify setImageEmbedded(true) is enabled (critical for inline images).
  • Check that Excel images are embedded objects (not linked to external files—linked images cannot be embedded).
  • Ensure the Excel file path is correct (avoid relative path issues in production).

3. Large HTML File Sizes (High Data Volume)

  • Preprocess: Filter or truncate unnecessary data in Excel before conversion (e.g., remove empty rows/columns).

Summary

This guide provides production-ready Java solutions for Excel-to-HTML conversion using Free Spire.XLS for Java:

  1. No Office Dependency: The library works independently of Microsoft Office, making it ideal for server-side deployment.
  2. Flexible Conversion: Support for full workbooks or targeted worksheets, with inline image embedding for self-contained HTML files.
  3. Production-Ready: Added error handling and troubleshooting tips to avoid common pitfalls in real-world use.

Limitation Note: The free version of Spire.XLS for Java has a worksheet limit, which is sufficient for most small-to-medium scale use cases.

Top comments (0)