C# EXCEL NPOI

NPOI採用template檔方式

朱文吉 2019/11/14 17:14:27
5599

1.安裝NPOI

1.1.在該專案按右鍵選擇管理NuGet套件

/tpu/File/html/201911/20191114170826/images/20191114165251.png

1.2.在瀏覽裡的輸入框輸入NPOI來搜尋
/tpu/File/html/201911/20191114170826/images/20191114165520.png

1.3.點選第一個後來安裝

/tpu/File/html/201911/20191114170826/images/20191114165820.png

1.4.安裝完成後參考會増加dll

/tpu/File/html/201911/20191114170826/images/20191106184133.jpg

2.template的樣式

/tpu/File/html/201911/20191114170826/images/20191106184416.jpg

2.1.template的員編對齊方式

/tpu/File/html/201911/20191114170826/images/20191114170609.png

2.2.template的姓名對齊方式

2.3.template的薪資儲存格格式

3.template屬性設定

3.1.選取該檔案

3.2.按右鍵選擇屬性後在複製到輸出目錄選擇一律複製或有更新時才複製

4.程式碼

//讀template檔
                string basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin");
                using (FileStream templateFile = new FileStream("template.xlsx", FileMode.Open, FileAccess.Read))
                {
                    
                    IWorkbook workbook = new XSSFWorkbook(templateFile);
                    ISheet sheet = workbook.GetSheetAt(0);
                    int copyFormatRow = 2;
                    //設定各cell的style
                    Dictionary<int, ICellStyle> cellStyleDictionary = new Dictionary<int, ICellStyle>();
                    for (int i = 0; i < 3; i++)
                    {
                        cellStyleDictionary.Add(i, sheet.GetRow(copyFormatRow).GetCell(i).CellStyle);
                    }

                    sheet.RemoveRow(sheet.GetRow(copyFormatRow));

                    //資料來源
                    int[] number = { 1, 2, 3 };
                    string[] name = { "張三", "李四", "王五" };
                    int[] salary = { 30000, 40000, 50000 };

                    //寫入資料
                    for (int i = 0; i < number.Length; i++)
                    {
                        IRow row = sheet.CreateRow(i+ copyFormatRow);
                        int cellIndex = 0;

                        //number
                        ICell cell = row.CreateCell(cellIndex);
                        cell.SetCellValue(number[i]);
                        cell.CellStyle = cellStyleDictionary[cellIndex++];

                        //name
                        cell = row.CreateCell(cellIndex);
                        cell.SetCellValue(name[i]);
                        cell.CellStyle = cellStyleDictionary[cellIndex++];

                        //salary
                        cell = row.CreateCell(cellIndex);
                        cell.SetCellValue(salary[i]);
                        cell.CellStyle = cellStyleDictionary[cellIndex++];
                    }

                    //寫檔
                    using (FileStream file = new FileStream("templateRS.xlsx", FileMode.Create))//產生檔案
                    {
                        workbook.Write(file);
                    }
                }

5.程式碼說明

    第2~3行是讀取template的檔案

    第6行是建立XSSFWorkbook物件

    第7行是取得第1個工作表(sheet)

    第10~14行是複製儲存格格式到Dictionary

    第16行移除一列,因為有設定儲存格格式,若要給予值是要用GetRow的方式,移除後就用CreateRow方式,是為了方便後面做事

    第19~21行是資料來源

    第24~43行是在excel寫入值及設定儲存格格式

    第46~48行為寫檔

6.執行結果

/tpu/File/html/201911/20191114170826/images/20191111105220.png

朱文吉