借助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 分钟前
部署在线GBA游戏,并通过docker安装启动
游戏·html·gba
小鹿( ﹡ˆoˆ﹡ )4 分钟前
Python中的树与图:构建复杂数据结构的艺术
开发语言·python
阡之尘埃10 分钟前
Python数据分析案例59——基于图神经网络的反欺诈交易检测(GCN,GAT,GIN)
python·神经网络·数据挖掘·数据分析·图神经网络·反欺诈·风控大数据
xiaojiesec14 分钟前
第157天: 安全开发-Python 自动化挖掘项目&SRC 目标&FOFA 资产&Web 爬虫解析库
python·安全
27划流星雨_18 分钟前
from tqdm.auto import tqdm用法详细介绍
python
掐指一算乀缺钱18 分钟前
SpringBoot 数据库表结构文档生成
java·数据库·spring boot·后端·spring
爱里承欢。23 分钟前
【Python语言初识(二)】
python
晚睡早起₍˄·͈༝·͈˄*₎◞ ̑̑23 分钟前
苍穹外卖学习笔记(七)
java·windows·笔记·学习·mybatis
就这个java爽!29 分钟前
JAVA网络编程【基于TCP和UDP协议】超详细!!!
java·开发语言·网络·tcp/ip·udp·eclipse·idea
hzw051030 分钟前
Jupyter的使用
ide·python·jupyter