สร้างไฟล์ Excel ด้วย C#, NPOI และคลาส NpoiExport

Program.cs

using NPOI.HSSF.UserModel;
using System;
using System.Data;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            HSSFWorkbook workbook = new HSSFWorkbook();
            var sheet1 = workbook.CreateSheet("Sheet1");
            var sheet2 = workbook.CreateSheet("Sheet2");

            DataTable dt = new DataTable();
            dt.Columns.Add("ชื่อ");
            dt.Columns.Add("Email");
            DataRow row1 = dt.NewRow();
            row1["ชื่อ"] = "Jack";
            row1["Email"] = "mr.phaisarn@gmail.com";
            dt.Rows.Add(row1);
            DataRow row2 = dt.NewRow();
            row2["ชื่อ"] = "Example";
            row2["Email"] = "example@gmail.com";
            dt.Rows.Add(row2);

            string fixedWidthColumn = "0-4000,1-4000,2-6000";
            var exporter = new NpoiExport(fixedWidthColumn);
            exporter.ExportDataTableToWorkbook(dt, "Export_Data");
            exporter.ExportDataTableToWorkbook(dt, "Export_Data2");

            string localFullPath = AppDomain.CurrentDomain.BaseDirectory;
            FileStream FileStreamObject = new System.IO.FileStream(localFullPath + @"Export_Excel.xls", FileMode.Create, FileAccess.Write);
            FileStreamObject.Write(exporter.GetBytes(), 0, exporter.GetBytes().Length);
            FileStreamObject.Close();
        }
    }
}
Continue reading