借助Aspose.html控件,在 Java 中创建 HTML 表

HTML表格在网页上以网格格式显示数据。表格以行和列的形式组织表格数据,其中每个单元格可以包含文本、图像、链接或其他 HTML 元素。在这篇博文中,我们将学习如何用 Java 创建 HTML 表。

**Aspose.Html**是一种高级的HTML操作API,可让您直接在.NET应用程序中执行广泛的HTML操作任务,Aspose.Html for .NET允许创建,加载,编辑或转换(X)HTML文档,而无需额外的软件或工具。API还为固定布局格式(如PDF和XPS)以及许多光栅图像格式提供了高保真渲染引擎。

Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose.Html 最新下载(qun:666790229)https://www.evget.com/product/3983/download

用于创建 HTML 表的 Java API

我们将使用Aspose.HTML for Java以编程方式创建 HTML 表格。它使开发人员能够在 Java 应用程序中使用 HTML 文档。它允许 HTML 解析、渲染、编辑以及将 HTML 文档转换为其他支持的格式。

请下载API的JAR或在基于Maven的Java应用程序中添加以下pom.xml配置。

复制代码
<repositories>
<repository>
<id>snapshots</id>
<name>repo</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-html</artifactId>
<version>23.11</version>
<classifier>jdk17</classifier>
</dependency>
</dependencies>
在 Java 中创建 HTML 表

HTML 表格是使用<table>元素定义的,并且使用各种其他元素进一步指定其结构,例如<tr>行、<th>标题单元格和<td>数据单元格。

我们可以按照以下步骤轻松创建 HTML 表格:

  1. 创建HTMLDocument类的实例。
  2. (可选)创建一个样式元素并将其附加到 head 元素。
  3. **使用createElement()**方法创建<table>、<tbody>、<tr>、<th>和<td>元素。
  4. **使用appendChild()**方法将子元素追加到其父元素。
  5. 之后,将<table>元素附加到元素上<body>。
  6. 最后,调用**save()**方法将文档保存在给定的文件路径中。

以下代码示例展示了如何在 Java 中创建 HTML 表

复制代码
// 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);
在 Java 中创建带有样式属性的 HTML 表

我们可以使用SetAttribute(string name, string value) <style>方法指定HTML 元素的属性。我们将按照前面提到的步骤创建一个 HTML 表格。但是,我们需要使用**SetAttribute(string name, string value)**方法来设置属性。它为元素添加新属性或更新值(如果属性名称已存在)。我们可以为、、、和元素设置属性。<style><table><tbody><tr><th><td>

以下代码示例演示如何在 Java 中创建具有样式属性的 HTML 表

复制代码
// Prepare a path for edited file saving
String savePath = "C:\\Files\\TableWithStyle.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; border-collapse: collapse;}");

// 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();

// Create table element
Element table = document.createElement("table");
table.setAttribute("style", "background-color:#00FF00;");

// Create table body
Element tbody = document.createElement("tbody");
table.appendChild(tbody);

// Create table header row
Element tr = document.createElement("tr");
tbody.appendChild(tr);

// Set style attribute with properties for the selected element
tr.setAttribute("style", "border: 2px Black solid; background-color:Red; color:#FFFFFF");

// Create table header cell 1
Element th = document.createElement("th");
Text title = document.createTextNode("Name");
th.appendChild(title);
tr.appendChild(th);

// Create table header cell 2
th = document.createElement("th");
title = document.createTextNode("Email");
th.appendChild(title);
tr.appendChild(th);

// Create table header cell 3
th = document.createElement("th");
title = document.createTextNode("Phone");
th.appendChild(title);
tr.appendChild(th);

// Create table data row
Element dataTr = document.createElement("tr");
tbody.appendChild(dataTr);

// Create table data cell 1
Element td = document.createElement("td");
Text data = document.createTextNode("John Doe");
td.appendChild(data);
dataTr.appendChild(td);

// Create table data cell 2
td = document.createElement("td");
data = document.createTextNode("john.doe@example.com");
td.appendChild(data);
dataTr.appendChild(td);

// Create table data cell 3
td = document.createElement("td");
data = document.createTextNode("123-456-789");
td.appendChild(data);
dataTr.appendChild(td);

// Append table to body
body.appendChild(table);

// Save the document to a file
document.save(savePath);
在 Java 中使用 Rowspan 和 Colspan 创建 HTML 表

<colspan>是HTML 中的属性,在和元素<rowspan>中使用,用于控制 HTML 表中多个列或多行的单元格跨度。我们可以使用**SetAttribute(string name, string value)**方法设置表格单元格的属性,如下所示:<td><th><colspan><rowspan>

复制代码
// Prepare a path for edited file saving
String savePath = "C:\\Files\\ColSpanRowSpan.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; border-collapse: collapse;}");

// 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();

// Create table element
Element table = document.createElement("table");

// Create table body
Element tbody = document.createElement("tbody");
table.appendChild(tbody);

// Create table header row
Element tr = document.createElement("tr");
tbody.appendChild(tr);

// Create table header cell 1
Element th = document.createElement("th");
Text title = document.createTextNode("Person Details");
th.appendChild(title);
tr.appendChild(th);

// Specify Colspan
th.setAttribute("colspan", "2");

// Create table data row
Element dataTr = document.createElement("tr");
tbody.appendChild(dataTr);

// Create table header cell 1
th = document.createElement("th");
title = document.createTextNode("Name");
th.appendChild(title);
dataTr.appendChild(th);

// Create table data cell 2
Element td = document.createElement("td");
Text data = document.createTextNode("John Doe");
td.appendChild(data);
dataTr.appendChild(td);

// Create table data row
dataTr = document.createElement("tr");
tbody.appendChild(dataTr);

// Create table header cell
th = document.createElement("th");
title = document.createTextNode("Phone");
th.appendChild(title);
dataTr.appendChild(th);

// Specify Colspan
th.setAttribute("rowspan", "2");

// Create table data cell
td = document.createElement("td");
data = document.createTextNode("123-456-780");
td.appendChild(data);
dataTr.appendChild(td);

// Create table data row
dataTr = document.createElement("tr");
tbody.appendChild(dataTr);

// Create table data cell
td = document.createElement("td");
data = document.createTextNode("123-456-789");
td.appendChild(data);
dataTr.appendChild(td);

// Append table to body
body.appendChild(table);

// Save the document to a file
document.save(savePath);
HTML 在线表格生成器

您还可以使用这个免费的HTML 表格生成器Web 应用程序在线创建 HTML 表格,该应用程序是使用此 API 开发的。

结论

在这篇博文中,我们学习了如何用 Java 创建 HTML 表。通过遵循本文中概述的步骤,您可以轻松开发自己的自定义解决方案来处理 HTML 表格。

相关推荐
独断万古他化3 分钟前
【Spring 原理】Bean 的作用域与生命周期
java·后端·spring
玄同7654 分钟前
Python 后端三剑客:FastAPI/Flask/Django 对比与 LLM 开发选型指南
人工智能·python·机器学习·自然语言处理·django·flask·fastapi
程序员猫哥_7 分钟前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
*小海豚*8 分钟前
在linux服务器上DNS正常,但是java应用调用第三方解析域名报错
java·linux·服务器
爱吃泡芙的小白白11 分钟前
环境数据多维关系探索利器:Pairs Plot 完全指南
python·信息可视化·数据分析·环境领域·pairs plot
杨超越luckly19 分钟前
HTML应用指南:利用GET请求获取中国500强企业名单,揭秘企业增长、分化与转型的新常态
前端·数据库·html·可视化·中国500强
派葛穆20 分钟前
Python-批量安装依赖
开发语言·python
pchaoda21 分钟前
RSI与布林带技术指标实战
python·matplotlib·量化
撩得Android一次心动24 分钟前
Android LiveData 全面解析:使用Java构建响应式UI【源码篇】
android·java·android jetpack·livedata