功能特色
- 精簡(DLL 僅 20KB)且易於使用。
- 支援 .NET Standard 2.0/.NET 4.6/.NET 4.5/.NET 4.0
- 無需任何第三方函式庫
- 支援匿名型別、Dapper 動態查詢、List/Array/Set/Enumrable、DataTable、Dictionary
安裝方式
您可以使用 Visual Studio 套件管理器或 NuGet UI 從 NuGet 安裝套件:
PM> install-package HtmlTableHelper或使用 dotnet 指令列:
dotnet add package HtmlTableHelper線上 Fiddle 範例:
快速開始
##### List/Array/Set/Enumrable 非 Key/Value 型別範例
``C#
using HtmlTableHelper;
..
var sourceData = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } };
var tablehtml = sourceData.ToHtmlTable();
/*
結果:
*/
Name Age Gender ITWeiHan 25 M
C#
using (var cn = "Your Connection")
{
var sourceData = cn.Query(@"select 'ITWeiHan' Name,25 Age,'M' Gender");
var tablehtml = sourceData.ToHtmlTable();
}
##### Dapper 範例
C#
var sourceData = new[] {new Dictionary
##### Dictionary 範例
自訂 Table/TR/TD/TH 屬性(動態型別) C# var data = /List/Array/Set/Enumrable../; var html = data.ToHtmlTable( tableAttributes: new { @class = "SomeClass"} //這是動態型別,支援所有屬性 ,trAttributes: new { ID = "SomeID" },tdAttributes: new { width = "120 px" },thAttributes: new { @class = "dark-theme" } ); /* 結果:
| .. |
|---|
| .. |
##### 屬性註解###### 1. Display :
C#
public class ModelClassWithDisplayAttr
{
[TableColumn(DisplayName = "Column1")] //MyProperty1 屬性會將 thead-td 的 innertext 呈現為:"Column1"
public string MyProperty1 { get; set; }
[TableColumn(DisplayName = "Column2")] //MyProperty2 屬性會將 thead-td 的 innertext 呈現為:"Column2"
public string MyProperty2 { get; set; }
}
###### 2. 跳過 : C#
public class ModelClassWithSkipAttr
{
[TableColumn( Skip = true)]
public string MyProperty1 { get; set; } //MyProperty1 不會產生 html
public string MyProperty2 { get; set; }
}
##### HTMLTableBuilder###### HtmlCaption
C#
var soucreData = new []{ new {MyProperty1="test",MyProperty2=123} };
var html = soucreData.CreateBuilder()
.SetCaption("This is Caption", new { id = "CaptionId" })
.ToHtmlTable();
//結果 : | MyProperty1 | MyProperty2 |
|---|---|
| test | 123 |
##### HTMLTableSetting可配置的 InnerHtml 編碼(除非有特殊原因,建議不要關閉,因為 XSS 攻擊風險)
C#
var sourceData = new[] { new { Name = "ITWeiHan" } };//預設編碼 var encodinghtml = sourceData.ToHtmlTable(); //結果:
var htmltablesetting = new HTMLTableSetting() { IsHtmlEncodeMode = false }; var notEncodinghtml = sourceData.ToHtmlTable(HTMLTableSetting: htmltablesetting); //結果:
擴充功能
ASP.NET Core MVC:
建立 IHtmlHelperExtension.csC#
using System.Collections.Generic;
using HtmlTableHelper;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Html;public static class IHtmlHelperExtension
{
public static HtmlString ToHtmlTable
public static HtmlString ToHtmlTable
C#
@Html.ToHtmlTable(new[] { new { Name = "ITWeiHan", Age = "25", Gender = "M" } })
/*
結果:
razor.cshtml
*/
Name Age Gender ITWeiHan 25 M
C#
using System.Collections.Generic;
using HtmlTableHelper;
using System.Web;
using System.Web.Mvc;
ASP.NET MVC 5:
建立 HtmlHelperExtension.cs
public static class HtmlHelperExtension
{
public static HtmlString ToHtmlTable`csharp
var html = enums.ToHtmlTable(tableAttributes, trAttributes, tdAttributes, HTMLTableSetting);
return new HtmlString(html);
}
public static HtmlString ToHtmlTable
範例
ASP.NET MVC 5 JQuery DataTable 範例: `C#
using HtmlTableHelper;
//..
public class HomeController : Controller
{
public ActionResult Index()
{
var datas = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } };
ViewBag.Table = datas.ToHtmlTable();
return View();
}
}
C#
@{
Layout = null;
}
AspNetMvcDemo
@Html.Raw(ViewBag.Table)
ASP.NET Core 範例:
C#
public class Startup
{
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Run(async (context) =>
{
var sourceData = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } };
var tablehtml = sourceData.ToHtmlTable();
await context.Response.WriteAsync(tablehtml);
});
}
}
---
Tranlated By Open Ai Tx | Last indexed: 2025-06-30
---