
Creating structured data tables in HTML is a common requirement for developers dealing with reports, UI components, or web-based documents. Rather than manually building these tables in static files, you can automate the process using Java. With the help of the Conholdate.Total for Java SDK, you can dynamically create and customize HTML tables, applying styles, borders, and structured rows and columns, all through code. This not only saves time but also ensures that your documents are consistently formatted and easy to maintain or update.
In this blog post, we will walk through the complete process of generating an HTML table in Java. The example we’ll cover demonstrates how to initialize a blank HTML document, define table structure, populate it with data, and apply CSS styling, all within your Java application.
Why Create HTML Table in Java?
Automate Document Generation: Automatically generate structured reports or tables within web pages without manually coding HTML.
Maintain Consistency: Ensure uniform formatting across multiple HTML files using consistent styles and logic.
Dynamic Content Handling: Populate HTML tables dynamically from data sources like databases, APIs, or user input.
Streamline Workflows: Integrate HTML generation into your document management systems or server-side applications.
Generate HTML Tables in Java - SDK Configuration
You need to install Conholdate.Total for Java SDK in your system. You can download its JAR file from the New Releases section or use the following Maven configurations:
<dependency>
<groupId>com.conholdate</groupId>
<artifactId>conholdate-total</artifactId>
<version>25.4</version>
<type>pom</type>
</dependency>
Create HTML Table in Java
Below is the Java code that demonstrates how to programmatically build an HTML table and apply border styling using Conholdate.Total for Java SDK:
// Prepare a path for edited file saving
String savePath = "C:\\Files\\Table.html";
// Initialize an empty HTML document
HTMLDocument document = new HTMLDocument();
// Create a style element and assign the color border-style and border-color values for table element
Element style = document.createElement("style");
style.setTextContent("table, th, td { border: 1px solid #0000ff; }");
// Find the document head element and append style element to the head
Element head = document.getElementsByTagName("head").get_Item(0);
head.appendChild(style);
// Declare a variable body that references the <body> element
Element body = document.getBody();
// Specify cols and rows
int cols = 3;
int rows = 2;
boolean isFirstRowHeader = false;
// Create table element
Element table = document.createElement("table");
// Create a table body
Element tbody = document.createElement("tbody");
table.appendChild(tbody);
// Create a table header row
if (isFirstRowHeader)
{
Element tr = document.createElement("tr");
tbody.appendChild(tr);
// Create table header columns
for (int j = 1; j < cols + 1; j++)
{
Element th = document.createElement("th");
Text title = document.createTextNode("Column-" + j);
th.appendChild(title);
tr.appendChild(th);
}
for (int i = 0; i < rows - 1; i++)
{
// Create a table row
Element dataTr = document.createElement("tr");
tbody.appendChild(dataTr);
// Create table header cells
for (int j = 1; j < cols + 1; j++)
{
Element td = document.createElement("td");
Text title = document.createTextNode("Data-" + j);
td.appendChild(title);
dataTr.appendChild(td);
}
}
}
else
{
for (int i = 0; i < rows; i++)
{
// Create a table row
Element dataTr = document.createElement("tr");
tbody.appendChild(dataTr);
// Create table cells
for (int j = 1; j < cols + 1; j++)
{
Element td = document.createElement("td");
Text title = document.createTextNode("Data-" + j);
td.appendChild(title);
dataTr.appendChild(td);
}
}
}
// Append table to body
body.appendChild(table);
// Save the document to a file
document.save(savePath);
This example covers how to construct a 3x2 HTML table with optional headers and border styling. You can adjust the number of columns, rows, or isFirstRowHeader values to meet your specific needs.
Conclusion
Generating HTML tables programmatically in Java can significantly simplify the way structured content is managed within your applications. By leveraging the features offered by Conholdate.Total for Java, you gain the ability to automate HTML document creation with full control over content and design. This not only speeds up the development process but also reduces the risk of human error and maintains a high standard of document formatting. Whether you are working on reporting systems, content management tools, or exporting features, mastering HTML table creation in Java opens the door to more dynamic and scalable solutions.