NPOI2.2.0.0实例详解(六)—设置EXCEL单元格边框

来源:互联网 发布:mac app store更新包 编辑:程序博客网 时间:2024/05/02 15:11
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using NPOI.HSSF.UserModel;using NPOI.SS.Formula.Eval;using NPOI.SS.Formula.Functions;using NPOI.SS.UserModel;using NPOI.XSSF.UserModel;using NPOI.POIFS.FileSystem;using NPOI.HPSF;using System.IO;using NPOI.SS.Util;using System.Drawing;using NPOI.HSSF.Util;namespace NPOI{    class Program5    {        static void Main(string[] args)        {            //说明:设置单元格边框            //1.创建EXCEL中的Workbook                     IWorkbook myworkbook = new XSSFWorkbook();            //2.创建Workbook中的Sheet                    ISheet mysheet = myworkbook.CreateSheet("sheet1");            //3.创建Row中的Cell并赋值            IRow row0 = mysheet.CreateRow(0); row0.CreateCell(0).SetCellValue(""); row0.CreateCell(1).SetCellValue("Thin");            IRow row1 = mysheet.CreateRow(1); row1.CreateCell(0).SetCellValue(""); row1.CreateCell(1).SetCellValue("Medium");            IRow row2 = mysheet.CreateRow(2); row2.CreateCell(0).SetCellValue(""); row2.CreateCell(1).SetCellValue("Dashed");            IRow row3 = mysheet.CreateRow(3); row3.CreateCell(0).SetCellValue(""); row3.CreateCell(1).SetCellValue("Dotted");            IRow row4 = mysheet.CreateRow(4); row4.CreateCell(0).SetCellValue(""); row4.CreateCell(1).SetCellValue("Thick");            IRow row5 = mysheet.CreateRow(5); row5.CreateCell(0).SetCellValue(""); row5.CreateCell(1).SetCellValue("Double");            IRow row6 = mysheet.CreateRow(6); row6.CreateCell(0).SetCellValue(""); row6.CreateCell(1).SetCellValue("Hair");            IRow row7 = mysheet.CreateRow(7); row7.CreateCell(0).SetCellValue(""); row7.CreateCell(1).SetCellValue("MediumDashed");            IRow row8 = mysheet.CreateRow(8); row8.CreateCell(0).SetCellValue(""); row8.CreateCell(1).SetCellValue("DashDot");            IRow row9 = mysheet.CreateRow(9); row9.CreateCell(0).SetCellValue(""); row9.CreateCell(1).SetCellValue("MediumDashDot");            IRow row10 = mysheet.CreateRow(10); row10.CreateCell(0).SetCellValue(""); row10.CreateCell(1).SetCellValue("DashDotDot");            IRow row11 = mysheet.CreateRow(11); row11.CreateCell(0).SetCellValue(""); row11.CreateCell(1).SetCellValue("MediumDashDotDot");            IRow row12 = mysheet.CreateRow(12); row12.CreateCell(0).SetCellValue(""); row12.CreateCell(1).SetCellValue("SlantedDashDot");            IRow row13 = mysheet.CreateRow(13); row13.CreateCell(0).SetCellValue(""); row13.CreateCell(1).SetCellValue("BorderDiagonal.Backward");            IRow row14 = mysheet.CreateRow(14); row14.CreateCell(0).SetCellValue(""); row14.CreateCell(1).SetCellValue("BorderDiagonal.Forward");            IRow row15 = mysheet.CreateRow(15); row15.CreateCell(0).SetCellValue(""); row15.CreateCell(1).SetCellValue("BorderDiagonal.Both");                       //4.创建CellStyle            ICellStyle style0 = myworkbook.CreateCellStyle();            style0.BorderBottom = BorderStyle.Thin;             ICellStyle style1 = myworkbook.CreateCellStyle();            style1.BorderBottom = BorderStyle.Medium;            ICellStyle style2 = myworkbook.CreateCellStyle();            style2.BorderBottom = BorderStyle.Dashed;            ICellStyle style3 = myworkbook.CreateCellStyle();            style3.BorderBottom = BorderStyle.Dotted;            ICellStyle style4 = myworkbook.CreateCellStyle();            style4.BorderBottom = BorderStyle.Thick;            ICellStyle style5 = myworkbook.CreateCellStyle();            style5.BorderBottom = BorderStyle.Double;            ICellStyle style6 = myworkbook.CreateCellStyle();            style6.BorderBottom = BorderStyle.Hair;            ICellStyle style7 = myworkbook.CreateCellStyle();            style7.BorderBottom = BorderStyle.MediumDashed;            ICellStyle style8 = myworkbook.CreateCellStyle();            style8.BorderBottom = BorderStyle.DashDot;            ICellStyle style9 = myworkbook.CreateCellStyle();            style9.BorderBottom = BorderStyle.MediumDashDot;            ICellStyle style10 = myworkbook.CreateCellStyle();            style10.BorderBottom = BorderStyle.DashDotDot;            ICellStyle style11 = myworkbook.CreateCellStyle();            style11.BorderBottom = BorderStyle.MediumDashDotDot;            ICellStyle style12 = myworkbook.CreateCellStyle();            style12.BorderBottom = BorderStyle.SlantedDashDot;            ICellStyle style13 = myworkbook.CreateCellStyle();            style13.BorderDiagonalLineStyle = BorderStyle.Thin;            style13.BorderDiagonal = BorderDiagonal.Backward;            style13.BorderDiagonalColor = IndexedColors.Red.Index;            ICellStyle style14 = myworkbook.CreateCellStyle();            style14.BorderDiagonalLineStyle = BorderStyle.Thin;            style14.BorderDiagonal = BorderDiagonal.Forward;            style14.BorderDiagonalColor = IndexedColors.Red.Index;            ICellStyle style15 = myworkbook.CreateCellStyle();            style15.BorderDiagonalLineStyle = BorderStyle.Thin;            style15.BorderDiagonal = BorderDiagonal.Both;            style15.BorderDiagonalColor = IndexedColors.Red.Index;                        //【Tips】            // 1.Border+方向 [边框类型] 例:BorderTop, BorderBottom,BorderLeft, BorderRight            // 2.方向+BorderColor [边框颜色] 例:TopBorderColor,BottomBorderColor, LeftBorderColor, RightBorderColor            // 3.绘制斜线首先要指定 BorderDiagonalLineStyle 然后 指定 BorderDiagonal             //5.将CellStyle应用于具体单元格            row0.GetCell(0).CellStyle = style0;            row1.GetCell(0).CellStyle = style1;            row2.GetCell(0).CellStyle = style2;            row3.GetCell(0).CellStyle = style3;            row4.GetCell(0).CellStyle = style4;            row5.GetCell(0).CellStyle = style5;            row6.GetCell(0).CellStyle = style6;            row7.GetCell(0).CellStyle = style7;            row8.GetCell(0).CellStyle = style8;            row9.GetCell(0).CellStyle = style9;            row10.GetCell(0).CellStyle = style10;            row11.GetCell(0).CellStyle = style11;            row12.GetCell(0).CellStyle = style12;            row13.GetCell(0).CellStyle = style13;            row14.GetCell(0).CellStyle = style14;            row15.GetCell(0).CellStyle = style15;            //6.保存                   FileStream file = new FileStream(@"E:\myworkbook5.xlsx", FileMode.Create);            myworkbook.Write(file);            file.Close();        }    }}

运行后,效果如下图所示【演示了不同边框样式】
1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 二年级的孩子浮躁上课不认真怎么办 三年级的小孩上课不认真听讲怎么办 大班孩子上课不专心听讲玩怎么办 老师跟家长说小孩上课讲话怎么办 别的家长动手打自己的孩子怎么办? 一岁3个月宝宝骨龄偏小怎么办 宝宝9个月了越来越粘人怎么办? 孩子突然说话结巴口吃了怎么办啊 很久不说话了不敢说话了怎么办 宝宝2岁了还不会说话怎么办 2岁的宝宝还不会说话怎么办 小宝宝有四个月了母乳不够吃怎么办 一个月的宝宝吐奶厉害怎么办 新生儿吐奶吐一次吐的特别多怎么办 把孩子打了一次现在说话结巴怎么办 宝宝五岁了口吃越来越严重了怎么办 幼儿把自己的舌头扣破皮了怎么办 6岁宝贝烧到39度怎么办 2岁半的宝宝说话结巴怎么办 2周3宝宝不会说话胆小怎么办 宝宝我2岁多了说话有点结巴怎么办 两岁宝宝说话突然结巴了怎么办 两岁3宝宝叫她名字不理人怎么办 九个月的宝宝身高不达标怎么办 3岁宝宝又吐又拉怎么办 宝宝发烧39度怎么办手脚很烫 两岁宝宝吃什么吐什么怎么办 7岁宝宝吃多了吐怎么办 7个月的宝宝大便干燥怎么办 10个月宝宝便秘大便干燥怎么办 一岁半宝宝老是拉糊糊状大便怎么办 外阴部长了一个疙瘩有点痒怎么办 小孩打架被另一个小孩家人告怎么办 德保豆浆机有电但不工作怎么办 刚买的笔记本c盘不足怎么办 qq糖粘在喉咙气管里怎么办 穿上旗袍后感觉后腰处不平整怎么办 机打票给客人给错联怎么办?急 ps修证件照感觉不太立体怎么办 手机百度上下载的文档打不开怎么办 5岁宝宝乘飞机没带证件怎么办