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

相关推荐
小小编程路19 分钟前
字符串转数字时,可能会遇到哪些问题?
java·开发语言·算法
许彰午23 分钟前
责任链模式实战——同一个框架里的两种链
java·开发语言·责任链模式
寻道码路26 分钟前
LangChain4j Java AI 应用开发实战(十四):手写 RAG 全流程 - 深入理解每个环节
java·开发语言·人工智能·ai
云烟成雨TD31 分钟前
Agent Scope Java 2.x 系列【1】核心架构
java·人工智能·agent
金銀銅鐵34 分钟前
用 Tkinter 实现简单的论语第一章阅读器
后端·python
小玮看世界34 分钟前
【技术成长实录】北京地铁12号线数据分析系统:从一个观察到一个完整项目的演进之路
python·人机交互·学习方法·cicd·项目交付
极光代码工作室36 分钟前
基于机器学习的金融风险预测系统
python·深度学习·机器学习·ai·系统设计
愛~杦辷个訾36 分钟前
Java Springboot使用阿里云oss对图片进行等质量压缩,转换成webp格式的压缩图。
java·spring boot·阿里云·oss
吴阿福|一人公司37 分钟前
Python 类变量修改的压力测试:高并发场景
开发语言·python
hikktn38 分钟前
Excel 日期格式统一治理:从“显示不全“到“自动兼容“的完整方案
windows·python·excel