Hướng dẫn export excel c# - xuất excel c #

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 vào file Excel Template sử dụng thư viện Mini Excelxuất dữ liệu vào file Excel Template sử dụng thư viện Mini Excel

[C#] Export Data to Excel Template

Mini Excel là thư viện C#, hỗ trợ cho chúng ta dễ dàng: import, export, template trong Excel. là thư viện C#, hỗ trợ cho chúng ta dễ dàng: import, export, template trong Excel.

Ở bài này, mình sẽ xuất dữ liệu demo của một hóa đơn vào Template Excel có sẵn.Template Excel có sẵn.

Giao diện hình ảnh ứng dụng:

Để thực hiện, các bạn có tham khảo trực tiếp trên video dưới đây:

 

Full source code C#:

using Microsoft.Extensions.Configuration.UserSecrets;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExportExcelToTemplate
{
   
    public partial class Form1 : Form
    {
        public class Product
        {
            public string Name { get; set; }
            public int Price { get; set; }

            public string PriceAsString
            {
                get
                {
                    return this.Price.ToString["#,###"];
                }

            }

        }
        public Form1[]
        {
            InitializeComponent[];
        }
        public List GetProducts[]
        {

            var listProduct = new List[];

            var product5 = new Product[]
            {
                Name = "Bí kiếp tán gái họ Nguyễn",
                Price = 1000
            };
            var product1 = new Product[]
            {
                Name = "Tự học C# trong 24h với Thảo Meo",
                Price = 158000
            };
            var product2 = new Product[]
            {
                Name = "Lập trình Mobile với Flutter 3.3 ",
                Price = 255000
            };

            var product3 = new Product[]
            {
                Name = "Cấu trúc dữ liệu & Thuật toán",
                Price = 99000
            };
            var product4 = new Product[]
            {
                Name = "Tự học OPP trong Java",
                Price = 18000
            };

            listProduct.Add[product1];
            listProduct.Add[product2];
            listProduct.Add[product3];
            listProduct.Add[product4];
            listProduct.Add[product5];


            return listProduct;

        }

        public dynamic GenerateDataDemo[]
        {
            var company = new
            {
                Name = "CÔNG TY TNHH LẬP TRÌNH VB",
                Phone = "0933.913.122",
                Email = "",
                Location = "Biên Hòa, Đồng Nai, Việt Nam"
            };

            var customer = new
            {
                Name = "Trịnh Quốc Khang",
                Mobile = "0932452123",
                Email = "",
                Address = "Bà Rịa Vũng Tàu"
            };


            var fee = new
            {
                Total = GetProducts[].Sum[x => x.Price].ToString["#,###"],
                Tax = "10 %",
                Transit = "35,000"
            };

            var data = new
            {
              
                    Title = "Hóa đơn mua hàng",
                    Company_Name = "CÔNG TY TNHH HÒA BÌNH",
                    Company_Phone = "0933.913.122",
                    Company_Email = "",
                    Company_Location = "Biên Hòa, Đồng Nai, Việt Nam",                 
                    Company_Website = "www.laptrinhvb.net",
                    Customer = customer,
                    NumInvoice = "11100002",
                    Products = GetProducts[],
                    Fee = fee,
                    Total = "2,585,000",
                    Date = DateTime.Now.ToString["dd/MM/yyyy HH:mm:ss"],
                    Description = "Thanks for Shopping!",
                    Tenant_Name = "NGUYỄN THẢO",
                    PropertyAddress = "Bà Rịa - Vũng Tàu",
                    Email = "",
                    Phone = "0933913122",                    
                  //Logo = File.ReadAllBytes["images/logo.png"] 
                  

            };

            return data;
        }

        private void btnExport_Click[object sender, EventArgs e]
        {
            var data = GenerateDataDemo[];
            MiniExcel.SaveAsByTemplate["exported.xlsx", "invoice.xlsx", data];
            Process.Start["exported.xlsx"];
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

Founder 1156 bài viết 9,864,194

NGUYỄN THẢO

Founder at LaptrinhVB.net at LaptrinhVB.net

★★★★★

♥ Tình yêu thương chẳng hề hư mất bao giờ. [Cr 13,4]

=========================================================================

My skills includes .NET[C#, VB.NET], DevExpress, Java, Android, PHP,

Python, Sqlserver, Mysql, Reactjs, Dart, Flutter, API services and lot more... Mysql, Reactjs, Dart, Flutter, API services and lot more...

Phone/Zalo/Telegram/WhatsApp: +84.933.913.122+84.933.913.122

Email: 

Facebook: //fb.com/Lewandowski28031988//fb.com/Lewandowski28031988

Youtube Channel: //www.youtube.com/c/CTutorialsTips

 

Buy me a cup of beer

       

=========================================================================

My skills includes .NET[C#, VB.NET], DevExpress, Java, Android, PHP,

Python, Sqlserver, Mysql, Reactjs, Dart, Flutter, API services and lot more...Thảo Meo - Lượt xem: 318 07:50:47, 12/10/2022EBOOK

Bài Viết Liên Quan

Chủ Đề