How to Convert Excel and CSV Documents to HTML in Java

When it comes to organizing, structuring, and formatting data for everyday report presentations and report-sharing scenarios, Excel is decisively the world’s go-to spreadsheet solution. And that’s for good reason – Excel’s extensive and endlessly updated features are perfect for most of our manual data manipulation needs, allowing us to quickly generate stylish rows and columns with slick graphs and other forms of visuals. The open-XML .XLSX format also transitions nicely into plain text formats like CSV, making it equally straightforward to export and upload Excel’s tabular data to a broader range of compatible applications.  Despite its myriad features and format conversion fluidity; however, document compatibility concerns are always lurking when we look to share Excel report content with the broadest possible audiences.  

Of course, we don’t have to look very far to find a global, standardized solution to this long-standing challenge. Where fully formatted and finished data reports are concerned, PDF represents the ideal export format, owing largely to its universal compatibility and secure design. Excel and CSV documents converted to PDF format are viewable on any browser, and these PDFs can be formed as vector or raster files depending on the document creator’s personal preference.

The limitations of PDF conversions become apparent only when we extend our document conversion criteria to include universally compatible formats that are also equipped to continue the document’s design. Documents converted to vector PDF files can only return to their original format (or to text format when using OCR conversions), and this fundamental lack of any meaningful editing/formatting capabilities in PDF makes the format a dead-end for ongoing design iterations.  

That’s where HTML conversions present a unique and exciting solution.  Not only is HTML universally accessible (compatible with every platform and browser in the world), but it’s also extremely dynamic, allowing for the inclusion of vast visual customizations with CSS and interactive features with JavaScript. Tabular data converted to HTML can be reformatted and iterated upon endlessly using popular, up-to-date, and extremely well-documented markup elements.   

Demonstration

In the remainder of this article, I’ll demonstrate two free API solutions with ready-to-run Java code examples that can be used to convert Excel and CSV documents to HTML format. Both allow form data input, so you can easily convert documents via absolute or relative file paths in your environment. These solutions will both return simple HTML text strings containing the converted document contents, making it straightforward to create and share accessible HTML files and further customize data visualizations in your resulting HTML code.

To take advantage of either API, start by installing the Java SDK.  First, add a reference to the repository in pom.xml:

XML
 
<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>


And then add a reference to the pom.xml dependency:

XML
 
<dependencies>
<dependency>
    <groupId>com.github.Cloudmersive</groupId>
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
    <version>v4.25</version>
</dependency>
</dependencies>


With the installation complete, you can begin structuring your document conversion requests.  To convert Excel (XLSX) spreadsheets to HTML format, copy and paste from the following code examples:

Java
 
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDocumentApi;

ApiClient defaultClient = Configuration.getDefaultApiClient();

// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");

ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
    byte[] result = apiInstance.convertDocumentXlsxToHtml(inputFile);
    System.out.println(result);
} catch (ApiException e) {
    System.err.println("Exception when calling ConvertDocumentApi#convertDocumentXlsxToHtml");
    e.printStackTrace();
}


And to convert from a CSV file instead, copy and paste the below code:

Java
 
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDocumentApi;

ApiClient defaultClient = Configuration.getDefaultApiClient();

// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");

ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
    byte[] result = apiInstance.convertDocumentCsvToHtml(inputFile);
    System.out.println(result);
} catch (ApiException e) {
    System.err.println("Exception when calling ConvertDocumentApi#convertDocumentCsvToHtml");
    e.printStackTrace();
}


As outlined above, each request will return your document as a string of plain-text HTML code – simple as that.

 

 

 

 

Top