DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Merge HTML Documents in Java
  • How To Convert Common Documents to PNG Image Arrays in Java
  • How To Create and Edit Excel XLSX Documents in Java
  • How to Convert a PDF to Text (TXT) Using Java

Trending

  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Implementing API Design First in .NET for Efficient Development, Testing, and CI/CD
  • Supervised Fine-Tuning (SFT) on VLMs: From Pre-trained Checkpoints To Tuned Models
  1. DZone
  2. Coding
  3. Languages
  4. How to Convert Excel and CSV Documents to HTML in Java

How to Convert Excel and CSV Documents to HTML in Java

Converting documents to HTML makes them universally compatible and opens up opportunities to customize them with dynamic elements.

By 
Brian O'Neill user avatar
Brian O'Neill
DZone Core CORE ·
Jun. 27, 23 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
6.1K Views

Join the DZone community and get the full member experience.

Join For Free

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.

API Document HTML Convert (command) CSV Java (programming language) Microsoft Excel

Opinions expressed by DZone contributors are their own.

Related

  • How to Merge HTML Documents in Java
  • How To Convert Common Documents to PNG Image Arrays in Java
  • How To Create and Edit Excel XLSX Documents in Java
  • How to Convert a PDF to Text (TXT) Using Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: