C#的NPOI按照规定模板导出excel设置单元格样式,并在单元格中动态存放数据

导入excel步骤:
1、引用相关组件
2、创建一个工作簿,创建工作表并命名;
3、设置表头及样式;
4、填充数据;
5、保存数据到指定位置;

 public FileDto ExportExcel(List<DeviceItem> list)
        {
            HSSFWorkbook excelBook = new HSSFWorkbook();

            ICellStyle style = excelBook.CreateCellStyle();
            style.VerticalAlignment = VerticalAlignment.Center;
            style.Alignment = HorizontalAlignment.Center;
            style.BorderBottom = BorderStyle.Thin;    //下边框线(给表格加入背景颜色后,就会覆盖原来的边框,直接设置边框样式可以解决这一问题)
            style.BorderLeft = BorderStyle.Thin;      //左边框线
            style.BorderRight = BorderStyle.Thin;     //右边框线
            style.BorderTop = BorderStyle.Thin;       //上边框线
            IFont font = excelBook.CreateFont();
            font.FontName = "宋体";
            font.FontHeight = 15 * 15;
            style.SetFont(font);

            ICellStyle style1 = excelBook.CreateCellStyle();
            style1.CloneStyleFrom(style);//复制之前的样式
            //style1.FillForegroundColor = LightYellow.Index;//设置背景颜色(使用NPOI已经有的颜色创建)
            //style1.FillPattern = FillPattern.SolidForeground;
            HSSFPalette palette = excelBook.GetCustomPalette();//自定义颜色
            palette.SetColorAtIndex(48, 255, 242, 204);
            style1.FillForegroundColor = palette.FindColor(255, 242, 204).Indexed;
            style1.FillPattern = FillPattern.SolidForeground;

            ICellStyle style2 = excelBook.CreateCellStyle();
            style2.CloneStyleFrom(style);
            IFont font2 = excelBook.CreateFont();//创建字体
            font2.FontName = "宋体";
            font2.FontHeight = 15 * 15;
            font2.IsBold = true;//字体加粗
            style2.SetFont(font2);

            foreach (var item in list)
            {
                ISheet sheet1 = excelBook.CreateSheet(item.SheetName);
                //设置每个单元格的宽度
                for (int i = 0; i < DetailTitle.Count; i++)
                {
                    sheet1.SetColumnWidth(i, 20 * 256);
                }

                #region 第一行:包括主控plc的IP和端口号
                IRow rowHeader1 = sheet1.CreateRow(0);
                rowHeader1.CreateCell(0).SetCellValue("IP地址:");
                rowHeader1.CreateCell(1).SetCellValue(item.Host);
                rowHeader1.CreateCell(2).SetCellValue("端口号:");
                rowHeader1.CreateCell(3).SetCellValue(item.Port);
                //sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 3, 4));//合并第四列和第五列
                var region1 = new CellRangeAddress(0, 0, 3, 4);//合并第四列和第五列
                ((HSSFSheet)sheet1).SetEnclosedBorderOfRegion(region1, BorderStyle.Thin, Black.Index);//解决合并单元格后无法加边框的问题
                sheet1.AddMergedRegion(region1);
                
                rowHeader1.GetCell(0).CellStyle = style1;
                rowHeader1.GetCell(1).CellStyle = style1;
                rowHeader1.GetCell(2).CellStyle = style1;
                rowHeader1.GetCell(3).CellStyle = style1;
                #endregion

                #region 第二行:包括设备负责人和设备通讯类型
                IRow rowHeader2 = sheet1.CreateRow(1);
                rowHeader2.CreateCell(0).SetCellValue("设备负责人:");
                rowHeader2.CreateCell(1).SetCellValue(" ");
                rowHeader2.CreateCell(2).SetCellValue("设备通讯类型:");
                rowHeader2.CreateCell(3).SetCellValue(" ");
                var region2 = new CellRangeAddress(1, 1, 3, 4); //合并第四列和第五列
                ((HSSFSheet)sheet1).SetEnclosedBorderOfRegion(region2, BorderStyle.Thin, Black.Index);
                sheet1.AddMergedRegion(region2);

                rowHeader2.GetCell(0).CellStyle = style1;
                rowHeader2.GetCell(1).CellStyle = style1;
                rowHeader2.GetCell(2).CellStyle = style1;
                rowHeader2.GetCell(3).CellStyle = style1;
                #endregion

                IRow rowHeader3 = sheet1.CreateRow(2);
                for (int i = 0; i < DetailTitle.Count; i++)
                {
                    ICell cell = rowHeader3.CreateCell(i);
                    cell.SetCellValue(DetailTitle[i]);
                    cell.CellStyle = style2;
                }
                //除了前面的三行,剩余的数据通过前面写的逻辑在单元格填入数据
                for (int i = 0; i < item.ItemList.Count; i++)
                {
                    IRow rowTemp = sheet1.CreateRow(i + 3);

                    #region 数据内容
                    rowTemp.CreateCell(0).SetCellValue(item.ItemList[i].CategoryName);
                    rowTemp.CreateCell(1).SetCellValue(item.ItemList[i].Name);
                    rowTemp.CreateCell(2).SetCellValue(item.ItemList[i].Address);
                    rowTemp.CreateCell(3).SetCellValue(item.ItemList[i].DataTypeName);
                    rowTemp.CreateCell(4).SetCellValue(item.ItemList[i].ReadAndWrite);
                    
                    rowTemp.GetCell(0).CellStyle = style;
                    rowTemp.GetCell(1).CellStyle = style;
                    rowTemp.GetCell(2).CellStyle = style;
                    rowTemp.GetCell(3).CellStyle = style;
                    rowTemp.GetCell(4).CellStyle = style;
                    #endregion
                }
            }

            string fileName = "IO点" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff") + ".xls";

            MemoryStream bookstream = new MemoryStream();

            excelBook.Write(bookstream);

            var buf = bookstream.ToArray();

            var file = new FileDto(fileName, MimeTypeNames.ApplicationVndMsExcel);

            var filePath = Path.Combine(AppFolders.TempFileDownloadFolder, file.FileToken);

            using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                fs.Write(buf, 0, buf.Length);
                fs.Flush();
                fs.Close();
            }

            return file;
        }


//设置xlsx背景颜色
ICellStyle style = workbook.CreateCellStyle();//使用NPOI已经有的颜色创建(这里跟xls一样,不知道为什么可以通用)
style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Yellow.Index;
style.FillPattern = FillPattern.SolidForeground;//使用没有的颜色,这里和xls不一样,不需要覆盖掉原来的色板
style.FillForegroundColor = 0;
style.FillPattern = FillPattern.SolidForeground;
((XSSFColor)style.FillForegroundColorColor).SetRgb(new byte[] { 0, 176, 240 });

你可能感兴趣的:(excel,c#,开发语言)