首页 > 其他分享 >.NET6 给单元格设置背景色并导出Excel

.NET6 给单元格设置背景色并导出Excel

时间:2023-03-17 11:13:22浏览次数:35  
标签:CellStyle 单元格 Excel bodyStyle rowtemp CreateCell sheet1 GetCell NET6

        /// <summary>
        /// 给单元格设置背景色
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> CollectExport()
        {
            #region 表头样式
            IWorkbook book = new HSSFWorkbook();
            ISheet sheet1 = book.CreateSheet("Sheet1");
            IRow row1 = sheet1.CreateRow(0);
            row1.HeightInPoints = 20;
            #endregion

            #region 表头样式及字体
            ICellStyle headStyle = book.CreateCellStyle();
            headStyle.Alignment = HorizontalAlignment.Center;
            IFont font = book.CreateFont();
            font.FontHeightInPoints = 12;
            font.Boldweight = 500;
            headStyle.SetFont(font);
            #endregion

            #region 表体样式及字体
            ICellStyle bodyStyle = book.CreateCellStyle();
            bodyStyle.Alignment = HorizontalAlignment.Center;
            //headStyle.Alignment = CellHorizontalAlignment;
            IFont fontbody = book.CreateFont();
            fontbody.FontHeightInPoints = 10;
            fontbody.Boldweight = 500;
            bodyStyle.SetFont(fontbody);
            #endregion

            #region 添加背景颜色
            ICellStyle cellStyle = book.CreateCellStyle();
            cellStyle.FillPattern = FillPattern.SolidForeground;
            cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
            cellStyle.Alignment = HorizontalAlignment.Center;
            #endregion

            #region 把数据给npoi
            DataTable dt = new DataTable();
            if (10 > 0)
            {
                string strs = "序号,客户,项目名称,编号,中标日期,报价人,审核人,中标金额,成本价,备注";
                string[] strArry = strs.Split(',');
                for (int i = 0; i < strArry.Length; i++)
                {
                    row1.CreateCell(i).SetCellValue(strArry[i]);
                    row1.GetCell(i).CellStyle = headStyle;
                }
                for (int i = 0; i < 10; i++)
                {
                    NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
                    rowtemp.HeightInPoints = 15;
                    rowtemp.CreateCell(0).SetCellValue(i + 1); //序号
                    rowtemp.GetCell(0).CellStyle = bodyStyle;
                    sheet1.SetColumnWidth(0, 5 * 256);

                    rowtemp.CreateCell(1).SetCellValue("客户"); //客户
                    rowtemp.GetCell(1).CellStyle = cellStyle;
                    sheet1.SetColumnWidth(1, 20 * 256);
                    rowtemp.CreateCell(2).SetCellValue("客户"); //项目名称
                    rowtemp.GetCell(2).CellStyle = bodyStyle;
                    sheet1.SetColumnWidth(2, 25 * 256);
                    rowtemp.CreateCell(3).SetCellValue("客户"); //编号
                    rowtemp.GetCell(3).CellStyle = bodyStyle;
                    sheet1.SetColumnWidth(3, 25 * 256);
                    rowtemp.CreateCell(4).SetCellValue("客户"); //中标日期
                    if (i % 2 == 0)
                    {
                        rowtemp.GetCell(4).CellStyle = cellStyle;
                    }
                    sheet1.SetColumnWidth(4, 10 * 256);

                    rowtemp.CreateCell(5).SetCellValue("客户"); //报价人
                    rowtemp.GetCell(5).CellStyle = bodyStyle;
                    sheet1.SetColumnWidth(5, 10 * 256);
                    rowtemp.CreateCell(6).SetCellValue("客户"); //审核人
                    rowtemp.GetCell(6).CellStyle = bodyStyle;
                    sheet1.SetColumnWidth(6, 10 * 256);

                    rowtemp.CreateCell(7).SetCellValue("客户"); //中标金额
                    rowtemp.GetCell(7).CellStyle = bodyStyle;
                    sheet1.SetColumnWidth(7, 10 * 256);
                    rowtemp.CreateCell(8).SetCellValue("客户"); //成本价
                    rowtemp.GetCell(8).CellStyle = bodyStyle;
                    sheet1.SetColumnWidth(8, 10 * 256);
                    rowtemp.CreateCell(9).SetCellValue("客户"); //备注
                    rowtemp.GetCell(9).CellStyle = bodyStyle;
                    sheet1.SetColumnWidth(9, 25 * 256);
                }
            }
            #endregion

            #region 返回工作流
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            book.Write(ms);
            ms.Seek(0, SeekOrigin.Begin);
            //string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls";
            book = null;
            #endregion
            return await Task.FromResult(new FileStreamResult(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = $"{DateTimeOffset.Now:yyyyMMdd_HHmmss}_CollectMaterialPlan.xlsx"
            });
        }

 

标签:CellStyle,单元格,Excel,bodyStyle,rowtemp,CreateCell,sheet1,GetCell,NET6
From: https://www.cnblogs.com/fyssl/p/17225963.html

相关文章

  • 盘点一个Excel多条件求和的问题
    大家好,我是皮皮。一、前言前几天在Python最强白银交流群【维哥】问了一个Excel处理的问题,这里拿出来给大家分享下。下图是他的数据:二、实现过程这个问题确看上去是E......
  • Excel批量加粗关键词(宏)
    代码如下:Sub批量加粗()DimaAsRangeDimbAsStringb=InputBox("请输入要加粗的关键字")ForEachaInActiveSheet.UsedRangeIfInStr(a.Value,b)>0Then......
  • 读取Excel文件数据java方法
    /***读取Excel数据*@paramfilePath文件路径*@paramsheetName工作表名称*@return以List<List<String>>形式返回Excel中的数据*@throwsIOException*......
  • EasyExcel
    模板{{$fe:listt.xxx    }}函数空格分割三目运算{{test?obj:obj2}}n:表示这个cell是数值类型{{n:}}le:代表长度{{le:()}}在if/else运用{{le:()>8......
  • 关于EasyExcel的数据导入和单sheet和多sheet导出
    读写Excel基本代码直接复制不一定能用实体类@ExcelIgnore在导出操作中不会被导出@ExcelProperty在导入过程中可以根据导入模板自动匹配字段,在导出过程中可用于设置......
  • aspose.cell 把一个Excel的Sheet拷贝到另一个Excel中,并插入在第一个位置
    1、aspose.cell把一个Excel的Sheet拷贝到另一个Excel中,并插入在第一个位置。什么?难道放在最后不行嘛,还要这么变态要求。先说一下,Copy之后放在最后的位置怎么操作。......
  • 自从用了 EasyExcel,导入导出 Excel 更简单了!
    作者:风雨兼程来源:jianshu.com/p/8f3defdc76d4EasyExcel在做excel导入导出的时候,发现项目中封装的工具类及其难用,于是去gitHub上找了一些相关的框架,最终选定了EasyEx......
  • ABAP 基于Function ALV 实现单元格自定义搜索帮助
    1.实现ALV报表自定义搜索帮助效果如下:点击搜索帮助按钮,弹出框 双击值并填入单元格内 2.实现思路 2.1定义变量以及F4搜索帮助自定义类,并实现 "定义ALV变量......
  • 表格单元格合并
    表格单元格合并 单元格合并属性水平合并:colspan垂直合并:rowspan<tableborder="1"width=500pxheight="500px">  <tr>    <td>单元1</td>   ......
  • gRPC之.Net6中的客户端和服务端共用proto协议文件
    1、说明在上一篇文章gRPC之.Net6中的初步使用介绍中,我们简单的介绍了gRPC在服务端、客户端以及Web项目中的使用。有一个问题,不知道大家发现没有,就是不管在服务端项目还是......