借助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 表格。

相关推荐
用户8356290780514 小时前
Python 实现 PDF 文件加密与解密方法
后端·python
用户8356290780514 小时前
使用 Python 冻结与拆分 Excel 窗格教程
后端·python
karry_k4 小时前
MyBatis批量insert-select踩坑:useGeneratedKeys=true 可能让PostgreSQL返回大量插入结果
java·后端
karry_k4 小时前
PostgreSQL 在 MyBatis 中执行正常 SQL 失效:一次 DELETE USING 踩坑记录
java·后端
SamDeepThinking8 小时前
从源码到代码:MyBatis-Flex 与 MyBatis-Plus 的逐项对比
java·后端·程序员
她的男孩11 小时前
Spring Boot 接 Flowable 工作流:用 3 个注解搭一个请假审批流程
java·后端·架构
你好潘先生12 小时前
别再记命令了,用 yeero do 说句人话就能跑脚本,而且不烧 token
服务器·python·命令行
Agent_大师13 小时前
WebSocket 行情重连成功,K线缺口不会自动消失
python
荣码13 小时前
LLM结构化输出:让AI返回JSON而不是废话,我踩了4个坑
java·python