翻譯|行業(yè)資訊|編輯:胡濤|2024-04-02 09:43:00.597|閱讀 96 次
概述:在這篇博文中,我們將學(xué)習(xí)如何用 Java 創(chuàng)建 HTML 表。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
HTML表格在網(wǎng)頁上以網(wǎng)格格式顯示數(shù)據(jù)。表格以行和列的形式組織表格數(shù)據(jù),其中每個單元格可以包含文本、圖像、鏈接或其他 HTML 元素。在這篇博文中,我們將學(xué)習(xí)如何用 Java 創(chuàng)建 HTML 表。
Aspose.Html 是一種高級的HTML操作API,可讓您直接在.NET應(yīng)用程序中執(zhí)行廣泛的HTML操作任務(wù),Aspose.Html for .NET允許創(chuàng)建,加載,編輯或轉(zhuǎn)換(X)HTML文檔,而無需額外的軟件或工具。API還為固定布局格式(如PDF和XPS)以及許多光柵圖像格式提供了高保真渲染引擎。
Aspose API支持流行文件格式處理,并允許將各類文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
我們將使用Aspose.HTML for Java以編程方式創(chuàng)建 HTML 表格。它使開發(fā)人員能夠在 Java 應(yīng)用程序中使用 HTML 文檔。它允許 HTML 解析、渲染、編輯以及將 HTML 文檔轉(zhuǎn)換為其他支持的格式。
請下載API的JAR或在基于Maven的Java應(yīng)用程序中添加以下pom.xml配置。
<repositories> <repository> <id>snapshots</id> <name>repo</name> <url>//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>
HTML 表格是使用<table>元素定義的,并且使用各種其他元素進一步指定其結(jié)構(gòu),例如<tr>行、<th>標(biāo)題單元格和<td>數(shù)據(jù)單元格。
我們可以按照以下步驟輕松創(chuàng)建 HTML 表格:
以下代碼示例展示了如何在 Java 中創(chuàng)建 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);
我們可以使用SetAttribute(string name, string value)<style>方法指定HTML 元素的屬性。我們將按照前面提到的步驟創(chuàng)建一個 HTML 表格。但是,我們需要使用SetAttribute(string name, string value)方法來設(shè)置屬性。它為元素添加新屬性或更新值(如果屬性名稱已存在)。我們可以為、、、和元素設(shè)置屬性。<style><table><tbody><tr><th><td>
以下代碼示例演示如何在 Java 中創(chuàng)建具有樣式屬性的 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);
<colspan>是HTML 中的屬性,在和元素<rowspan>中使用,用于控制 HTML 表中多個列或多行的單元格跨度。我們可以使用SetAttribute(string name, string value)方法設(shè)置表格單元格的屬性,如下所示:<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);
您還可以使用這個免費的Web 應(yīng)用程序在線創(chuàng)建 HTML 表格,該應(yīng)用程序是使用此 API 開發(fā)的。
在這篇博文中,我們學(xué)習(xí)了如何用 Java 創(chuàng)建 HTML 表。通過遵循本文中概述的步驟,您可以輕松開發(fā)自己的自定義解決方案來處理 HTML 表格。要是您還有其他關(guān)于產(chǎn)品方面的問題,歡迎咨詢我們,或者加入我們官方技術(shù)交流群。
歡迎下載|體驗更多Aspose產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn