Excel spreadsheets serve as a useful tool for organizing, managing, and computing data, while HTML plays a similar role in presenting content through web browsers. In some situation, you may need to save Excel to HTML conversion to convert worksheets to HTML pages. For example, when embedding the content of the spreadsheets into web pages. As HTML pages can be easily shared and accessed through web browsers and HTML page can also be interactive, allowing users to input or manipulate data directly on the webpage, which can be useful for collaborative projects or online surveys. This article will show you how to convert Excel XLSX files to HTML programmatically using Java from the following three parts.
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>13.3.6</version>
</dependency>
</dependencies>
Convert Excel to HTML
Spire.XLS for Java offers workbook.saveToFile() method to convert Excel workbook to HTML easily. Here comes to steps of convert XLSX to HTML.
- Create a Workbook instance.
- Load an Excel file using Workbook.loadFromFile() method.
- Save the file to HTML using Workbook.saveToFile() method.
import com.spire.xls.*;
public class ExceltoHTML {
public static void main(String[] args) throws Exception {
//Load an Excel file
Workbook wb = new Workbook();
wb.loadFromFile("Sample.xlsx");
//Save the file to HTML
wb.saveToFile("WorkbookToHtml.html",FileFormat.HTML);
}
}
Convert Excel to a standalone HTML file
Spire.XLS for Java offers HTMLOptions instance to embed the image or convert Excel to a standalone HTML file. The following steps show how to save Excel worksheet as HTML file.
- Create a Workbook instance and load an Excel file using Workbook.loadFromFile() method.
- Get the first worksheet using Workbook.getWorksheets().get() method.
- Create a HTMLOptions instance.
- Enable image embedding using HTMLOptions.setImageEmbedded() method.
- Set the HTML as a standalone HTML file using HTMLOptions.isStandAloneHtmlFile() method.
- Save the worksheet to HTML with image embedded using Worksheet.saveToHtml(String, HTMLOptions) method.
import com.spire.xls.*;
import com.spire.xls.core.spreadsheet.HTMLOptions;
public class ExceltoHTML {
public static void main(String[] args) throws Exception {
//Load the sample document
Workbook wb = new Workbook();
wb.loadFromFile("Sample.xlsx");
//Get the first worksheet
Worksheet sheet = wb.getWorksheets().get(0);
//Initiate a HTMLOptions instance
HTMLOptions options = new HTMLOptions();
//Set embedded image as true
options.setImageEmbedded(true);
//Set is StandaloneHTML file as true
options.isStandAloneHtmlFile(true);
//Save the worksheet to HTML
sheet.saveToHtml("Result2.html", options);
}
}
Convert Excel to HTML stream
Spire.XLS for Java offers Workbook.saveToStream() method to save the Excel to HTML stream. The files can be saved directly to memory without having to save them to disk. When working with large numbers of files, it is very useful and these files can be easily sent to the network, to memory or to other devices.
- Create a Workbook instance and load an Excel file using Workbook.loadFromFile() method.
- Create a new FileOutputStream object named "fileStream" and specifies the file name "SaveStream.html" where the output will be saved.
- Calls the Workbook.saveToStream() method to save the content in HTML format to the file stream.
import com.spire.xls.*;
import java.io.FileOutputStream;
public class ExceltoHTMLStream {
public static void main(String[] args) throws Exception {
//Load the sample document
Workbook wb = new Workbook();
wb.loadFromFile("Sample.xlsx");
FileOutputStream fileStream = new FileOutputStream("SaveStream.html");
wb.saveToStream(fileStream, FileFormat.HTML);
fileStream.close();
}
}
In this article, you have learned how to convert an Excel file to HTML using Java. The code snippet provided demonstrates the use of Spire.XLS for Java to convert the Excel file to HTML format. Additionally, the article explores various options available to customize the conversion output, such as embedding images in the HTML file or saving them as standalone HTML files. By leveraging the flexibility of Spire.XLS, you can easily control the conversion process and tailor the output to your specific needs. Whether you require a simple conversion or a more complex one with advanced settings like page orientation, header and footer, or CSS styles, Spire.XLS makes it effortless. In conclusion, you have seen how Spire.XLS for Java provides an efficient solution to convert Excel files to HTML format. If you want to learn more about Excel file processing, you can explore further resources from Excel forums where you can find answers to common questions, share best practices, and connect with other developers.
Top comments (0)