提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|行业资讯|编辑:胡涛|2023-11-20 11:16:03.140|阅读 241 次
概述:在这篇博文中,我们将逐步学习如何在 C# 中创建 HTML 表格。本指南将为您提供在 C# 中有效创建 HTML 表格所需的知识和技能。
#慧都22周年庆大促·界面/图表报表/文档/IDE/IOT/测试等千款热门软控件火热促销中>>
HTML表格是一种在网页上显示数据的通用且强大的方式。它们可用于创建简单的表(例如日历)或更复杂的表(例如数据网格)。在这篇博文中,我们将逐步学习如何在 C# 中创建 HTML 表格。本指南将为您提供在 C# 中有效创建 HTML 表格所需的知识和技能。
Aspose.Html 是一种高级的HTML操作API,可让您直接在.NET应用程序中执行广泛的HTML操作任务,Aspose.Html for .NET允许创建,加载,编辑或转换(X)HTML文档,而无需额外的软件或工具。API还为固定布局格式(如PDF和XPS)以及许多光栅图像格式提供了高保真渲染引擎。
Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
我们将使用Aspose.HTML for .NET在 C# 中创建 HTML 表格。它允许开发人员以编程方式操作和使用 HTML 文档。它提供了广泛的特性和功能,用于在 .NET 应用程序中解析、转换、编辑和呈现 HTML 文档。
请下载 API 的 DLL或使用NuGet安装它。
PM> Install-Package Aspose.Html
我们可以按照以下步骤创建 HTML 表格:
以下代码示例演示如何在 C# 中创建 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);
我们可以按照前面提到的步骤创建一个 HTML 表格。但是,我们需要使用SetAttribute(string name, string value)<style>方法设置元素的属性。它为元素添加新属性或更新值(如果属性名称已存在)。我们可以为、、、和元素设置属性。<table><tbody><tr><th><td>
以下代码示例演示如何在 C# 中创建具有样式属性的 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>方法设置<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 应用程序,它是使用此 API 开发的。
在这篇博文中,我们学习了如何在 C# 中创建 HTML 表格。我们已经介绍了使用 Aspose.HTML for .NET 以编程方式创建表的基础知识。通过遵循本文中提供的步骤和代码示例,您可以轻松开发自己的自定义解决方案来使用 HTML 表格。要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。
欢迎下载|体验更多Aspose产品
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@hmdbvip.cn




在现代复杂系统开发中,大型项目面临着需求频繁变更、技术复杂性高、团队协作困难、系统可追溯性差等多重挑战。Sparx Systems Enterprise Architect作为一款领先的UML全生命周期建模平台,通过提供统一的建模环境和贯穿从概念到退役的完整工具链,为大型项目提供了至关重要的战略支撑。
HOOPS SDK系列(包括HOOPS Visualize、HOOPS Exchange、HOOPS Communicator等)为开发者提供从图形渲染、3D数据解析到可视化交互的完整工具链,帮助机器人制造商在有限周期内构建出媲美行业顶级标准的专业应用,实现软硬件的最佳融合。
在接到客户投诉的那一刻,工厂最怕的不是质量问题本身,而是查不清来路:同一批次的成品到底是谁做的?用了哪批原料?哪道工序出了问题?
HOOPS Exchange是Tech Soft 3D提供的行业领先CAD数据访问引擎,支持超过30种主流CAD文件格式的导入与导出,完整保留几何结构、装配层级、PMI注释与工程属性
相关产品
创建,阅读,编辑HTML文档,包括CSS样式,并呈现为PDF和光栅图像格式。
Html To PdfExpertPDF HTML to PDF Converter提供对 HTML/CSS 转换的充分支持。Html2pdf 转换器很容易使用,你可以在数分钟时间内把它整合到你的应用程序中。
HTML Add-on富文本编辑器TE Edit Control的插件,为它添加解析HTML文本等功能。
最新文章 MORE
永利最大(官方)网站相关的文章 MORE
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@hmdbvip.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
永利最大(官方)网站