- [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
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
[DEVEXPRESS] Hướng dẫn export data từ sqlserver vào file template excel có sẵn C# winform
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 sử dụng Spreadsheet để xuất dữ liệu từ DataTable Sqlserver vào mẫu template file excel có sẵn trong lập trình C# Winform.
[DEVEXPRESS] EXPORT DATA TO EXCEL TEMPLATE EXISTS C# WINFORM USING SPREADSHEET
Trong lập trình ứng dụng, khi các bạn muốn xuất dữ liệu từ Database ra file excel theo mẫu có sẵn của chúng ta đã thiết kế:
Trong mẫu đã có sẵn các thông tin:
- Tên công ty
- Logo công ty
- Định dạng format text theo yêu cầu của mình
Vậy khi xuất dữ liệu ra excel theo mẫu, sẽ làm cho người dùng ít phải thao tác chỉnh sửa hơn.
Các bạn nào có sử dụng phần mềm kế toán Misa thì cũng thấy người ta cũng xuất dữ liệu ra template excel giống vậy.
Ví dụ: mình có một mẫu template excel Danh sách sản phẩm như hình bên dưới đây:
và ta có dữ liệu từ SQL Server như hình bên dưới được select từ bảng table Products
trong database mẫu NORTHWND
.
Và kết quả mong muốn của chúng ta sau khi đổ dữ liệu vào template thành công thì sẽ được kết quả như hình ảnh bên dưới:
Full source code demo ứng dụng export data to template Excel C#:
using DevExpress.Export.Xl;
using DevExpress.Spreadsheet;
using DevExpress.XtraSpreadsheet;
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.Windows.Forms;
namespace FillDataToTemplateExcel
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
public Form1()
{
InitializeComponent();
configDatabase.ConnectString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btn_export_Click(object sender, EventArgs e)
{
SpreadsheetControl spreadsheetControl1 = new SpreadsheetControl();
var dt = SQLHelper.ExecQueryDataAsDataTable("SELECT ProductID, ProductName, UnitPrice, UnitsInStock FROM dbo.Products");
IWorkbook workbook = spreadsheetControl1.Document;
workbook.LoadDocument("template.xlsx", DocumentFormat.Xlsx);
Worksheet sheet = workbook.Worksheets[0];
workbook.BeginUpdate();
try
{
ExternalDataSourceOptions options = new ExternalDataSourceOptions() { ImportHeaders = true };
// Bắt đầu ghi từ column thứ 7
Table table = sheet.Tables.Add(dt, 7, 1, options);
TableStyleCollection tableStyles = workbook.TableStyles;
TableStyle tableStyle = tableStyles[BuiltInTableStyleId.TableStyleMedium2]; // Đổi style table ở đây
// Apply the table style to the existing table.
table.Style = tableStyle;
table.Columns[0].Name = "Mã sản phẩm";
table.Columns[1].Name = "Tên sản phểm";
table.Columns[2].Name = "Số lượng";
table.Columns[3].Name = "Đơn giá";
TableColumn subtotalColumn = table.Columns.Add();
subtotalColumn.Name = "Thành tiền";
subtotalColumn.Formula = "=[Số lượng] * [Đơn giá]";
subtotalColumn.TotalRowFunction = TotalRowFunction.Sum;
table.ShowTotals = true;
table.Columns[3].TotalRowLabel = "TỔNG CỘNG";
table.Columns[0].TotalRowLabel = "";
subtotalColumn.DataRange.NumberFormat = "$#,##0.00";
// sử dụng custom style
//TableStyle customTableStyle = workbook.TableStyles.Add("testTableStyle");
//TableStyleElement totalRowStyle = customTableStyle.TableStyleElements[TableStyleElementType.TotalRow];
//customTableStyle.BeginUpdate();
//totalRowStyle.Fill.BackgroundColor = Color.Green;
//totalRowStyle.Font.Color = Color.White;
//totalRowStyle.Font.Bold = true;
//customTableStyle.EndUpdate();
//table.Style = customTableStyle;
// sheet.MergeCells(sheet.Range["B2764:G2764"]);
}
finally
{
workbook.EndUpdate();
}
spreadsheetControl1.SaveDocument("test.xlsx", DocumentFormat.Xlsx);
Process.Start("test.xlsx");
}
}
}
Trong source code, trên mình sử dụng style ở đoạn code này, các bạn có thể thay đổi các kiểu style để đoạn code đó để test nhé:
tableStyles[BuiltInTableStyleId.TableStyleMedium2];
Các bạn có thể mở comment của mình bên dưới để có thể customize template theo yêu cầu của mình mong muốn.
Video demo ứng dụng:
Happy Coding :)