Java: Convert Excel to HTML

2021-12-03 07:48:17 Written by  support iceblue
Rate this item
(0 votes)

HTML files are Hypertext Markup Language files designed for displaying information in web browsers. In some cases, you might need to convert your Excel document to HTML in order to view it on the web. This article will demonstrate how to achieve this task programmatically in Java using Spire.XLS for Java.

Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls</artifactId>
        <version>14.11.0</version>
    </dependency>
</dependencies>
    

Convert Excel to HTML

The following are the steps to convert an Excel file to HTML:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.loadFromFile() method.
  • Save the file to HTML using Workbook.saveToFile() method.
  • Java
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;

public class ConvertExcelToHTML {
    public static void main(String []args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load an Excel file
        workbook.loadFromFile("Sample1.xlsx");

        //Save the file to HTML
        workbook.saveToFile("ToHtml.html", FileFormat.HTML);
    }
}

Java: Convert Excel to HTML

Convert Excel to HTML with Image Embedded

The following are the steps to convert an Excel worksheet to HTML with image embedded:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.loadFromFile() method.
  • Get the first worksheet using Workbook.getWorksheets().get() method.
  • Create a HTMLOptions instance and enable image embedding using HTMLOptions.setImageEmbedded() method.
  • Save the worksheet to HTML with image embedded using Worksheet.saveToHtml(String, HTMLOptions) method.
  • Java
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.spire.xls.core.spreadsheet.HTMLOptions;

public class ConvertExcelToHtmlWithImageEmbeded {
    public static void main(String []args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load an excel file
        workbook.loadFromFile("Sample2.xlsx");

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

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

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

Java: Convert Excel to HTML

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Tuesday, 27 September 2022 02:33