国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[C#]利用NPOI以模板形式生成EXCEL

2019-11-14 16:45:50
字體:
來源:轉載
供稿:網友

在開發中,有時候需要生成的Excel樣子比較復雜,如果用代碼形式去控制EXCEL比較繁瑣費時,所以事先做好需要生成EXCEL的模板,再生成的時候往里面填充數據這樣子比較簡便。

這里利用NPOI(鏈接:https://npoi.codeplex.com/)第三方東西來實現;

代碼如下:

                if (File.Exists(templateXlsPath))                {                    int i = 4, j = 4;                    using (FileStream file = new FileStream(templateXlsPath, FileMode.Open, Fileaccess.Read))                    {                        HSSFWorkbook _excel = new HSSFWorkbook(file);                        ISheet _sheetBasic = _excel.GetSheet(sheet_basicInfo.Replace("$", ""));                        ISheet _sheetStreatLamp = _excel.GetSheet(sheet_LampMoreLess.Replace("$", ""));                        file.Close();                        foreach (ExcelBasicInfo excelBasic in basicInfoList)                        {                            IRow _row = _sheetBasic.CreateRow(i);                            _row.CreateCell(0).SetCellValue(excelBasic.Number);                            _row.CreateCell(1).SetCellValue(excelBasic.TeamName);                            _row.CreateCell(2).SetCellValue(excelBasic.CtrlCabNumber);                            _row.CreateCell(3).SetCellValue(excelBasic.CurrentStatus);                            _row.CreateCell(4).SetCellValue(excelBasic.DevicesUseTime);                            _row.CreateCell(5).SetCellValue(excelBasic.ConfirmLampCount);                            _row.CreateCell(6).SetCellValue(excelBasic.CalculateKW);                            _row.CreateCell(7).SetCellValue(excelBasic.OldDeviceNumber);                            _row.CreateCell(8).SetCellValue(excelBasic.CtrlCabType);                            _row.CreateCell(9).SetCellValue(excelBasic.InstallationSites);                            _row.CreateCell(10).SetCellValue(excelBasic.Section);                            _row.CreateCell(11).SetCellValue(excelBasic.SuperiorPower);                            _row.CreateCell(12).SetCellValue(excelBasic.TableName);                            _row.CreateCell(13).SetCellValue(excelBasic.LampCount);                            _row.CreateCell(14).SetCellValue(excelBasic.LampTypeA);                            _row.CreateCell(15).SetCellValue(excelBasic.LamppowerA);                            _row.CreateCell(16).SetCellValue(excelBasic.LampCountA);                            _row.CreateCell(17).SetCellValue(excelBasic.RunModeA);                            _row.CreateCell(18).SetCellValue(excelBasic.LampTypeB);                            _row.CreateCell(19).SetCellValue(excelBasic.LamppowerB);                            _row.CreateCell(20).SetCellValue(excelBasic.LampCountB);                            _row.CreateCell(21).SetCellValue(excelBasic.RunModeB);                            _row.CreateCell(22).SetCellValue(excelBasic.LampTypeC);                            _row.CreateCell(23).SetCellValue(excelBasic.LamppowerC);                            _row.CreateCell(24).SetCellValue(excelBasic.LampCountC);                            _row.CreateCell(25).SetCellValue(excelBasic.RunModeC);                            _row.CreateCell(26).SetCellValue(excelBasic.LampTypeD);                            _row.CreateCell(27).SetCellValue(excelBasic.LamppowerD);                            _row.CreateCell(28).SetCellValue(excelBasic.LampCountD);                            _row.CreateCell(29).SetCellValue(excelBasic.RunModeD);                            _row.CreateCell(30).SetCellValue(excelBasic.Remark);                            i++;                        }                        foreach (ExcelStreetLamp streatLamp in streatLamps)                        {                            IRow _row = _sheetStreatLamp.CreateRow(j);                            _row.CreateCell(0).SetCellValue(streatLamp.Number);                            _row.CreateCell(1).SetCellValue(streatLamp.TeamName);                            _row.CreateCell(2).SetCellValue(streatLamp.PRojectName);                            _row.CreateCell(3).SetCellValue(streatLamp.CtrlCabNumber);                            _row.CreateCell(4).SetCellValue(streatLamp.ChangeDate);                            _row.CreateCell(5).SetCellValue(streatLamp.MoreLess);                            _row.CreateCell(6).SetCellValue(streatLamp.MoreLessTotalCount);                            _row.CreateCell(7).SetCellValue(streatLamp.LampTypeA);                            _row.CreateCell(8).SetCellValue(streatLamp.LamppowerA);                            _row.CreateCell(9).SetCellValue(streatLamp.LampCountA);                            _row.CreateCell(10).SetCellValue(streatLamp.RunModeA);                            _row.CreateCell(11).SetCellValue(streatLamp.LampTypeB);                            _row.CreateCell(12).SetCellValue(streatLamp.LamppowerB);                            _row.CreateCell(13).SetCellValue(streatLamp.LampCountB);                            _row.CreateCell(14).SetCellValue(streatLamp.RunModeB);                            _row.CreateCell(15).SetCellValue(streatLamp.LampTypeC);                            _row.CreateCell(16).SetCellValue(streatLamp.LamppowerC);                            _row.CreateCell(17).SetCellValue(streatLamp.LampCountC);                            _row.CreateCell(18).SetCellValue(streatLamp.RunModeC);                            _row.CreateCell(19).SetCellValue(streatLamp.LampTypeD);                            _row.CreateCell(20).SetCellValue(streatLamp.LamppowerD);                            _row.CreateCell(21).SetCellValue(streatLamp.LampCountD);                            _row.CreateCell(22).SetCellValue(streatLamp.RunModeD);                            _row.CreateCell(23).SetCellValue(streatLamp.Remark);                            j++;                        }                        SaveFileDialog _savefileDialog = new SaveFileDialog();                        _savefileDialog.Filter = " excel files(*.xls)|*.xls|All files(*.*)|*.*";                        _savefileDialog.RestoreDirectory = true;                        _savefileDialog.FileName = string.Format("上海路燈每月增減表_{0}", DateTime.Now.ToString("yyyyMMddHHmm"));                        if (_savefileDialog.ShowDialog() == DialogResult.OK)                        {                            using (FileStream f = new FileStream(_savefileDialog.FileName.ToString().Trim(), FileMode.Create, FileAccess.ReadWrite))                            {                                _excel.Write(f);                            }                            MessageToolV2.ShowInfo("合并EXCEL成功!");                        }                    }                }

代碼效果

image


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宣化县| 五台县| 探索| 运城市| 祁阳县| 民县| 伊宁市| 海淀区| 新化县| 利辛县| 安溪县| 循化| 桑日县| 永嘉县| 泾阳县| 新绛县| 洪洞县| 乐安县| 崇州市| 达孜县| 武威市| 岱山县| 安达市| 铁力市| 东丽区| 深水埗区| 临夏县| 泰兴市| 乐清市| 宣恩县| 克东县| 安塞县| 德阳市| 外汇| 龙江县| 锡林郭勒盟| 时尚| 昭苏县| 罗田县| 桂东县| 婺源县|