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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

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

  • How to Merge HTML Documents in Java
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • Event Driven Architecture (EDA) - Optimizer or Complicator
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 2
  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
Oops! Something Went Wrong

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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!