Convert Excel Worksheets to HTML and vice versa in Java
Tags: java, covnert, worksheet, html
Published: 2021-08-27 08:20:30

---
title: Convert Excel Worksheets to HTML and vice versa in Java
published: true
description: This article demonstrates how to convert Excel worksheets to HTML and vice versa in Java.
tags: java,covnert,worksheet,html
//cover_image: https://direct_url_to_image.jpg
---

Excel files are widely used to store and organize the data in the form of worksheets. In addition, you can perform various calculations as well as analysis of the data. However, in some cases, you have to convert Excel files to other formats. For example, you might need to convert an Excel worksheet to HTML or image in order to display it within your web application.  Since HTML is more suitable for websites, therefore, this article covers how to convert Excel worksheets to HTML and vice versa by using Spire.XLS for Java.

##Installing Spire.Xls.jar

If you use Maven, you can easily import the jar in your application by adding the following code to your project’s pom.xml file. For non-Maven projects, please download the jar file from [this link](https://www.e-iceblue.com/Download/xls-for-java.html) and manually add it as a dependency in your program.
```java
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId> e-iceblue </groupId>
        <artifactId>spire.xls</artifactId>
        <version>4.8.1</version>
    </dependency>
</dependencies>
```
##Example 1. Convert Worksheet to HTML

Converting a worksheet to HTML is quite simply and straightforward by using the Worksheet.saveToHtml() method. Besides, you can use the HTMLOptions class to control the convert options, for example, embed the images in HTML while converting. 
```java
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.spire.xls.core.spreadsheet.HTMLOptions;

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

        //Create a Workbook object
        Workbook workbook = new Workbook();

        //Load the sample HTML file
        workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Create a HTMLOptions object
        HTMLOptions options = new HTMLOptions();

        //Embed image in HTML when converting
        options.setImageEmbedded(true);

        //Save to a HTML file
        sheet.saveToHtml("output/ToHtml.html",options);
    }
}
```
![ToHtml](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r4f0pgfvflcknjv9xhes.png)
##Example 2. Convert HTML to Worksheet
```java
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class ConvertHtmlToWorksheet {

    public static void main(String[] args) {

        //Create a Workbook object
        Workbook workbook = new Workbook();

        //Load the sample HTML file
        workbook.loadFromHtml("C:\\Users\\Administrator\\Desktop\\sample.html");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Auto fit column width and row height
        sheet.getAllocatedRange().autoFitColumns();
        sheet.getAllocatedRange().autoFitRows();

        //Save to an Excel file
        workbook.saveToFile("output/ToWorksheet.xlsx", FileFormat.Version2016);
    }
}
```
![ToExcel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7edvimofu5zl5gbtm7mb.png)