- [DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
[C#] Hướng dẫn chuyển đổi DataTable sang Table HTML
Xin chào các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn cách xuất dữ liệu từ DataTable C# sang Table HTML và hiển thị lên Webbrowser.
[C#] Export DataTable to Table HTML
Thường trong lập trình ứng dụng, các bạn thường sử dụng Report để xuất báo cáo thông tin.
Tuy nhiên, trong bài này mình sẽ xuất dữ liệu từ DataTable sang table HTML nhanh chóng và sử dụng thư viện Bootstrap Framework, để hiển thị dữ liệu.
Sau khi view dữ liệu ở dạng Website, các bạn có thể dễ dàng xuất dữ liệu này dưới dạng File PDF, hoặc có thể in bảng dữ liệu này ra.
Dưới đây là giao diện demo ứng dụng Table HTML:
Full source code C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DataTable2HTML
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_export_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("STT", typeof(int));
table.Columns.Add("Mã số", typeof(string));
table.Columns.Add("Tên học sinh", typeof(string));
table.Columns.Add("Ngày sinh", typeof(string));
table.Columns.Add("Toán", typeof(double));
table.Columns.Add("Văn", typeof(double));
table.Columns.Add("Anh Văn", typeof(double));
table.Columns.Add("Vật Lý", typeof(double));
table.Columns.Add("Hóa Học", typeof(double));
table.Columns.Add("Sinh Học", typeof(double));
table.Rows.Add(1, "SV001", "Nguyễn Thảo", "28/03/2000", 7,8,8,6,7,10);
table.Rows.Add(2, "SV002", "Hoàng Dược Sư", "15/01/1980", 7,8,8,6,7,10);
table.Rows.Add(3, "SV003", "Trương Vô Kỵ", "24/05/2004", 7,8,8,6,7,10);
table.Rows.Add(4, "SV004", "Dương Quá", "23/03/1978", 7,8,8,6,7,10);
table.Rows.Add(5, "SV005", "Âu Dương Phong", "21/10/1968", 7,8,8,6,7,10);
table.Rows.Add(6, "SV006", "Chu Chỉ Nhược", "24/03/1963", 7,8,8,6,7,10);
table.Rows.Add(7, "SV007", "Hồng Thất Công", "05/03/1998", 7,8,8,6,7,10);
table.Rows.Add(8, "SV008", "Dương Trùng Dương", "02/03/1970", 7,8,8,6,7,10);
table.Rows.Add(9, "SV009", "Tiểu Long Nữ", "10/02/1988", 7,8,8,6,7,10);
table.Rows.Add(10, "SV010", "Lý Mạc Sầu", "05/07/1991", 7,8,8,6,7,10);
string HtmlBody = ExportDatatableToHtml(table);
System.IO.File.WriteAllText(@"E:abc.HTML", HtmlBody);
webBrowser1.Url = new Uri(@"E:abc.HTML");
}
protected string ExportDatatableToHtml(DataTable dt)
{
StringBuilder strHTMLBuilder = new StringBuilder();
strHTMLBuilder.Append("<html >");
strHTMLBuilder.Append("<head>");
strHTMLBuilder.Append("<meta charset='utf-8'>");
strHTMLBuilder.Append("</head>");
strHTMLBuilder.Append(@"<link rel='stylesheet' href='https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css' integrity='sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk' crossorigin='anonymous'>");
strHTMLBuilder.Append("<script src='https://code.jquery.com/jquery-3.3.1.slim.min.js' integrity='sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo' crossorigin='anonymous'></script>");
strHTMLBuilder.Append(" <script src='https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js' integrity='sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49' crossorigin='anonymous'></script>");
strHTMLBuilder.Append("<script src='https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js' integrity='sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy' crossorigin='anonymous'></script>");
strHTMLBuilder.Append("<body>");
strHTMLBuilder.Append("<h3 style='margin: 20px; color: #bd2130; font-weight: bold; text-align: center;'> DANH SÁCH ANH HÙNG THẦN ĐIÊU ĐẠI HIỆP</h3>");
strHTMLBuilder.Append("<table class='table table-sm table-hover' style='margin: 20px;'>");
strHTMLBuilder.Append("<thead>");
strHTMLBuilder.Append("<tr class='bg-success' style='color: white; text-align: left;'>");
foreach (DataColumn myColumn in dt.Columns)
{
strHTMLBuilder.Append("<th class='border border-secondary'>");
strHTMLBuilder.Append(myColumn.ColumnName);
strHTMLBuilder.Append("</th>");
}
strHTMLBuilder.Append("</tr>");
strHTMLBuilder.Append("</thead>");
foreach (DataRow myRow in dt.Rows)
{
strHTMLBuilder.Append("<tr>");
foreach (DataColumn myColumn in dt.Columns)
{
strHTMLBuilder.Append("<td class='border border-secondary'>");
strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString());
strHTMLBuilder.Append("</td>");
}
strHTMLBuilder.Append("</tr>");
}
strHTMLBuilder.Append("</table>");
strHTMLBuilder.Append("</body>");
strHTMLBuilder.Append("</html>");
string Htmltext = strHTMLBuilder.ToString();
return Htmltext;
}
}
}
Thanks for watching!