Convert Excel files to HTML in Java

GemBox.Spreadsheet for Java allows you to save an Excel workbook or worksheet in HTML or MHTML format.

The following example shows how to convert an Excel file to HTML in Java.

Note the use of HtmlSaveOptions to set the worksheet options that are supported when saving an Excel file in HTML format. The example demonstrates saving of an entire Excel workbook and all its images in HTML format.

Since worksheet files must be physically saved on a disk, the following example cannot be run interactively from the Example Explorer.

Screenshot of HTML converted from Excel file with GemBox.Spreadsheet for Java
HTML converted from Excel file with GemBox.Spreadsheet for Java
import com.gembox.spreadsheet.*;

class Program {

    public static void main(String[] args) throws java.io.IOException {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.setLicense("FREE-LIMITED-KEY");

        ExcelFile workbook = ExcelFile.load("%#HtmlExport.xlsx%");

        ExcelWorksheet worksheet = workbook.getWorksheet(0);

        // Some of the properties from ExcelPrintOptions class are supported in HTML export.
        worksheet.getPrintOptions().setPrintHeadings(true);
        worksheet.getPrintOptions().setPrintGridlines(true);

        // Print area can be used to specify custom cell range which should be exported to HTML.
        worksheet.getNamedRanges().setPrintArea(worksheet.getCells().getSubrange("A1", "J42"));

        HtmlSaveOptions options = new HtmlSaveOptions();
        options.setHtmlType(HtmlType.HTML);
        options.setSelectionType(SelectionType.ENTIRE_FILE);

        workbook.save("HtmlExport.html", options);
    }
}

Check next example or download examples from GitHub.