This tutorial covers how to create HTML table in Java. It discusses the system environment configurations, the algorithm, and a working sample code to build HTML table in Java. Furthermore, it compiles the information to improve the HTML table generation per your requirements.
Steps to Create a Basic HTML Table Generator in Java
- Set up your IDE to use Aspose.HTML for Java to create HTML tables
- Generate a blank HTML document using the HTMLDocument class object to insert a table
- Declare a table element by invoking the createElement() method
- Set different table styles and append HTML columns and rows
- Create table header, rows and columns and fill with sample data
- Save the output file as an HTML file
These steps summarize the workflow for creating an HTML table generator in Java. As a first step, create an empty HTML file and then append some rows and columns. Finally, export the HTML table contents to a file to conclude the conversion.
Code for Creating Table in HTML using Java
import com.aspose.html.*; | |
public class Main | |
{ | |
public static void main(String[] args) throws Exception // Create HTML table in Java | |
{ | |
// Set the licenses | |
new License().setLicense("License.lic"); | |
// Prepare a path | |
String savePath = "Table.html"; | |
// Initialize an empty HTML document | |
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument(); | |
// Create a style element | |
com.aspose.html.dom.Element style = document.createElement("style"); | |
style.setTextContent("table, th, td { border: 1px dotted #90EE90; }"); | |
// Access the head and set its style | |
com.aspose.html.dom.Element head = document.getElementsByTagName("head").get_Item(0); | |
head.appendChild(style); | |
// Declare a variable body | |
com.aspose.html.dom.Element body = document.getBody(); | |
// Specify cols and rows | |
int cols = 3; | |
int rows = 2; | |
boolean isFirstRowHeader = false; | |
// Create table element | |
com.aspose.html.dom.Element table = document.createElement("table"); | |
// Create a table body | |
com.aspose.html.dom.Element tbody = document.createElement("tbody"); | |
table.appendChild(tbody); | |
// Create a table header row | |
if (isFirstRowHeader) | |
{ | |
com.aspose.html.dom.Element tr = document.createElement("tr"); | |
tbody.appendChild(tr); | |
// Create table header columns | |
for (int j = 1; j < cols + 1; j++) | |
{ | |
com.aspose.html.dom.Element th = document.createElement("th"); | |
com.aspose.html.dom.Text title = document.createTextNode("Column-" + j); | |
th.appendChild(title); | |
tr.appendChild(th); | |
} | |
for (int i = 0; i < rows - 1; i++) | |
{ | |
// Create a table row | |
com.aspose.html.dom.Element dataTr = document.createElement("tr"); | |
tbody.appendChild(dataTr); | |
// Create table header cells | |
for (int j = 1; j < cols + 1; j++) | |
{ | |
com.aspose.html.dom.Element td = document.createElement("td"); | |
com.aspose.html.dom.Text title = document.createTextNode("Data-" + j); | |
td.appendChild(title); | |
dataTr.appendChild(td); | |
} | |
} | |
} | |
else | |
{ | |
for (int i = 0; i < rows; i++) | |
{ | |
// Create a table row | |
com.aspose.html.dom.Element dataTr = document.createElement("tr"); | |
tbody.appendChild(dataTr); | |
// Create table cells | |
for (int j = 1; j < cols + 1; j++) | |
{ | |
com.aspose.html.dom.Element td = document.createElement("td"); | |
com.aspose.html.dom.Text title = document.createTextNode("Data-" + j); | |
td.appendChild(title); | |
dataTr.appendChild(td); | |
} | |
} | |
} | |
// Append table to body | |
body.appendChild(table); | |
// Save the output | |
document.save(savePath); | |
System.out.println("Table created in HTML successfully"); | |
} | |
} |
Above is the quick version of the code for creating table in HTML using Java. It uses the HTMLDocument class to initiate the blank document and then format visual properties like the border color, borderline style, etc. Subsequently, you may choose to add as many rows and columns as you may need by invoking the insertCell and insertRow methods.
This article has covered the details of creating an HTML table builder in Java. To render Markdown to an HTML file, read the article on Convert Markdown to HTML in Java.