C# EXCEL表格生成 NPOI.dll应用2(表格样式)

using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;


            XSSFWorkbook workbook = new XSSFWorkbook();
            ISheet sheet = workbook.CreateSheet();
            
            sheet.SetColumnWidth(0, (int)((15 + 0.72) * 256)); //*设置宽度(列号,宽度)
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 2, 0, 3));//*合并单元格(起始行号,终止行号, 起始列号,终止列号)

            IRow row0 = sheet.CreateRow(0);
            row0.Height = 20 * 20;//*行高
            
            ICell t0 = (ICell)row0.CreateCell(0); //创建单元格

            {
                //1.定义属性
                ICellStyle cellStyle = workbook.CreateCellStyle();
                //2.文字
                IFont font = workbook.CreateFont();//文字属性
                font.Color = HSSFColor.OliveGreen.Blue.Index;//颜色
                font.IsItalic = true;//下划线  
                font.FontHeightInPoints = 10;
                font.FontName = "微软雅黑";
                cellStyle.SetFont(font);
                //3.边框
                cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Dotted;//上
                cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Dotted;//下
                cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Hair;//左
                cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Hair;//右
                cellStyle.TopBorderColor = HSSFColor.OliveGreen.Blue.Index;//上边框颜色
                cellStyle.BottomBorderColor = HSSFColor.OliveGreen.Blue.Index;//下边框颜色  
                cellStyle.LeftBorderColor = HSSFColor.OliveGreen.Blue.Index;//左边框颜色  
                cellStyle.RightBorderColor = HSSFColor.OliveGreen.Blue.Index;//右边框颜色

                cellStyle.FillForegroundColor = HSSFColor.Blue.Index;//选中后文字背景
                cellStyle.FillBackgroundColor = HSSFColor.Red.Index;//选中后单元格背景
                cellStyle.FillPattern = FillPattern.AltBars;

                cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;//水平对齐
                cellStyle.VerticalAlignment = VerticalAlignment.Center;//垂直对齐
                cellStyle.WrapText = true; //自动换行
                cellStyle.Indention = 0;//缩进

                t0.CellStyle = cellStyle;//*单元格属性
            }
            t0.SetCellValue("内容");
            string FilePath = "Documents/xxx.xlsx";
            FilePath = Path.GetFullPath(FilePath);
            using (FileStream file = new FileStream(FilePath, FileMode.Create))
            {
                workbook.Write(file);
            }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欧阳大虾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值