[C#] Export Data to Microsoft Word Template Winform
Ví dụ: Chúng ta sẽ có một mẫu phiếu xuất kho dạng File Word như hình dưới đây.

Dữ liệu cần xuất ra sẽ được bọc trong dấu ngoặc nhọn.
Kết quả khi các bạn xuất dữ liệu ra, chúng ta sẽ được kết quả như hình bên dưới:

Đầu tiên, các bạn cài đặt cho mình thư viện MiniWord từ nuget:
PM> NuGet\Install-Package MiniWord -Version 0.7.0
Source code C# đầy đủ:
using MiniExcelLibs;
using MiniExcelLibs.OpenXml;
using MiniSoftware;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ExportDataToExcelTemplate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public List<Product> GetListProductSample()
{
var listProduct = new List<Product>();
var product1 = new Product();
product1.stt = 1;
product1.nhomsanpham = "Thuốc";
product1.tensanpham = "Paradol";
product1.masanpham = "1";
product1.donvitinh = "Viên";
product1.slyeucau = 0;
product1.slthucnhap = 100;
product1.dongia = 1080;
product1.thanhtien = 108000;
product1.ghichu = "Nhập từ nhà cung cấp";
var product = new Product();
product.stt = 2;
product.nhomsanpham = "Thuốc";
product.tensanpham = "Paradol Extra";
product.masanpham = "5";
product.donvitinh = "Viên";
product.slyeucau = 0;
product.slthucnhap = 100;
product.dongia = 1080;
product.thanhtien = 108000;
product.ghichu = "Nhập từ nhà cung cấp";
var product2 = new Product();
product2.stt = 3;
product2.nhomsanpham = "Thuốc";
product2.tensanpham = "Acetylcystein";
product2.masanpham = "2";
product2.donvitinh = "Viên";
product2.slyeucau = 0;
product2.slthucnhap = 250;
product2.dongia = 800;
product2.thanhtien = 162500;
product2.ghichu = "Nhập từ nhà cung cấp";
var product3 = new Product();
product3.stt = 4;
product3.nhomsanpham = "Vật tư";
product3.tensanpham = "Bao tay";
product3.masanpham = "1";
product3.donvitinh = "Túi";
product3.slyeucau = 0;
product3.slthucnhap = 20;
product3.dongia = 750;
product3.thanhtien = 35620;
product3.ghichu = "Nhập từ nhà cung cấp";
var product4 = new Product();
product4.stt = 5;
product4.nhomsanpham = "Vật tư";
product4.tensanpham = "Kim tiêm";
product4.masanpham = "1";
product4.donvitinh = "Bộ";
product4.slyeucau = 0;
product4.slthucnhap = 50;
product4.dongia = 2500;
product4.thanhtien = 458200;
product4.ghichu = "Nhập từ nhà cung cấp";
listProduct.Add(product1);
listProduct.Add(product);
listProduct.Add(product2);
listProduct.Add(product3);
listProduct.Add(product4);
return listProduct;
}
public string PATH_TEMPLATE = Application.StartupPath + "\\mauword.docx";
public string PATH_EXPORT = Application.StartupPath + "\\export.docx";
static string FormatVietnameseDate(DateTime date)
{
string day = date.Day.ToString();
string month = date.Month.ToString();
string year = date.Year.ToString();
return $"Ngày {day} tháng {month} năm {year}";
}
private void btnExport_Click(object sender, EventArgs e)
{
var data = GetListProductSample().ToArray();
var config = new OpenXmlConfiguration()
{
IgnoreTemplateParameterMissing = false,
};
var total = data.Sum(x => x.thanhtien);
var value = new
{
product = data,
tienchu = TienChu.DocTienBangChu(total, " đồng."),
ngay_thang_nam = FormatVietnameseDate(new DateTime(2023, 11, 4)),
ma_phieu = "11230802001",
ten_nha_cung_cap = "Công ty TNHH Công Nghệ LẬP TRÌNH VB.NET",
nguoi_giao = "Thảo Meo TV",
ly_do = "nhập mua hàng nhà cung cấp",
ten_kho = "Kho nhà thuốc",
TEN_CONGTY = "CÔNG TY TNHH CÔNG NGHỆ SÀI GÒN",
dia_chi = "290 Nơ Trang Long, Phường 12, Quận Bình Thạnh, TP.HCM",
mst = "08123458631",
dien_thoai = "0933.913.122",
tong = total,
};
MiniWord.SaveAsByTemplate(PATH_EXPORT, PATH_TEMPLATE, value);
Process.Start(PATH_EXPORT);
}
}
}