翻譯|行業(yè)資訊|編輯:胡濤|2023-11-20 11:16:03.140|閱讀 224 次
概述:在這篇博文中,我們將逐步學(xué)習(xí)如何在 C# 中創(chuàng)建 HTML 表格。本指南將為您提供在 C# 中有效創(chuàng)建 HTML 表格所需的知識和技能。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
HTML表格是一種在網(wǎng)頁上顯示數(shù)據(jù)的通用且強大的方式。它們可用于創(chuàng)建簡單的表(例如日歷)或更復(fù)雜的表(例如數(shù)據(jù)網(wǎng)格)。在這篇博文中,我們將逐步學(xué)習(xí)如何在 C# 中創(chuàng)建 HTML 表格。本指南將為您提供在 C# 中有效創(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 .NET在 C# 中創(chuàng)建 HTML 表格。它允許開發(fā)人員以編程方式操作和使用 HTML 文檔。它提供了廣泛的特性和功能,用于在 .NET 應(yīng)用程序中解析、轉(zhuǎn)換、編輯和呈現(xiàn) HTML 文檔。
請下載 API 的 DLL或使用NuGet安裝它。
PM> Install-Package Aspose.Html
我們可以按照以下步驟創(chuàng)建 HTML 表格:
以下代碼示例演示如何在 C# 中創(chuàng)建 HTML 表格。
// Prepare a path for edited file saving string savePath = "C:\\Files\\Table.html"; // Initialize an empty HTML document var document = new HTMLDocument(); // Create a style element and assign the color border-style and border-color values for table element var style = document.CreateElement("style"); style.TextContent = "table, th, td { border: 1px solid #0000ff; }"; // Find the document head element and append style element to the head var head = document.GetElementsByTagName("head").First(); head.AppendChild(style); // Declare a variable body that references the <body> element var body = document.Body; // Specify cols and rows var cols = 3; var rows = 2; var isFirstRowHeader = false; // Create table element var table = document.CreateElement("table"); // Create a table body var tbody = document.CreateElement("tbody"); table.AppendChild(tbody); // Create a table header row if (isFirstRowHeader) { var tr = document.CreateElement("tr"); tbody.AppendChild(tr); // Create table header columns for (int j = 1; j < cols + 1; j++) { var th = document.CreateElement("th"); var title = document.CreateTextNode("Column-" + j); th.AppendChild(title); tr.AppendChild(th); } for (int i = 0; i < rows - 1; i++) { // Create a table row var dataTr = document.CreateElement("tr"); tbody.AppendChild(dataTr); // Create table header cells for (int j = 1; j < cols + 1; j++) { var td = document.CreateElement("td"); var title = document.CreateTextNode("Data-" + j); td.AppendChild(title); dataTr.AppendChild(td); } } } else { for (int i = 0; i < rows; i++) { // Create a table row var dataTr = document.CreateElement("tr"); tbody.AppendChild(dataTr); // Create table cells for (int j = 1; j < cols + 1; j++) { var td = document.CreateElement("td"); var 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);
我們可以按照前面提到的步驟創(chuàng)建一個 HTML 表格。但是,我們需要使用SetAttribute(string name, string value)<style>方法設(shè)置元素的屬性。它為元素添加新屬性或更新值(如果屬性名稱已存在)。我們可以為、、、和元素設(shè)置屬性。<table><tbody><tr><th><td>
以下代碼示例演示如何在 C# 中創(chuàng)建具有樣式屬性的 HTML 表。
// Prepare a path for edited file saving string savePath = "C:\\Files\\TableWithStyle.html"; // Initialize an empty HTML document using var document = new HTMLDocument(); // Create a style element and assign the color border-style and border-color values for table element var style = document.CreateElement("style"); style.TextContent = "table, th, td { border: 1px solid #0000ff; border-collapse: collapse;}"; // Find the document head element and append style element to the head var head = document.GetElementsByTagName("head").First(); head.AppendChild(style); // Declare a variable body that references the <body> element var body = document.Body; // Create table element var table = document.CreateElement("table"); table.SetAttribute("style", "background-color:#00FF00;"); // Create table body var tbody = document.CreateElement("tbody"); table.AppendChild(tbody); // Create table header row var 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 var th = document.CreateElement("th"); var 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 var dataTr = document.CreateElement("tr"); tbody.AppendChild(dataTr); // Create table data cell 1 var td = document.CreateElement("td"); var 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);
同樣,我們也可以使用SetAttribute(string name, string value)<colspan>方法設(shè)置<rowspan>表格單元格的屬性,如下所示:
// Prepare a path for edited file saving string savePath = "C:\\Files\\ColSpanRowSpan.html"; // Initialize an empty HTML document using var document = new HTMLDocument(); // Create a style element and assign the color border-style and border-color values for table element var style = document.CreateElement("style"); style.TextContent = "table, th, td { border: 1px solid #0000ff; border-collapse: collapse;}"; // Find the document head element and append style element to the head var head = document.GetElementsByTagName("head").First(); head.AppendChild(style); // Declare a variable body that references the <body> element var body = document.Body; // Create table element var table = document.CreateElement("table"); // Create table body var tbody = document.CreateElement("tbody"); table.AppendChild(tbody); // Create table header row var tr = document.CreateElement("tr"); tbody.AppendChild(tr); // Create table header cell 1 var th = document.CreateElement("th"); var title = document.CreateTextNode("Person Details"); th.AppendChild(title); tr.AppendChild(th); // Specify Colspan th.SetAttribute("colspan", "2"); // Create table data row var 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 var td = document.CreateElement("td"); var 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)用程序,它是使用此 API 開發(fā)的。
在這篇博文中,我們學(xué)習(xí)了如何在 C# 中創(chuàng)建 HTML 表格。我們已經(jīng)介紹了使用 Aspose.HTML for .NET 以編程方式創(chuàng)建表的基礎(chǔ)知識。通過遵循本文中提供的步驟和代碼示例,您可以輕松開發(fā)自己的自定義解決方案來使用 HTML 表格。要是您還有其他關(guān)于產(chǎn)品方面的問題,歡迎咨詢我們,或者加入我們官方技術(shù)交流群。
歡迎下載|體驗更多Aspose產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn