C# Excel设置单元格边框 之 NPOI


很多表格中都要使用边框,本节将为你重点讲解NPOI中边框的设置和使用。

边框和其他单元格设置一样也是调用ICellStyle接口,ICellStyle有2种和边框相关的属性,分别是:

边框相关属性 说明 范例
Border+方向 边框类型 BorderTop, BorderBottom,BorderLeft, BorderRight
方向+BorderColor 边框颜色 TopBorderColor,BottomBorderColor, LeftBorderColor, RightBorderColor

其中边框类型分为以下几种:

边框范例图 对应的静态值
image CellBorderType.DOTTED
image CellBorderType.HAIR
image CellBorderType.DASH_DOT_DOT
image CellBorderType.DASH_DOT
image CellBorderType.DASHED
image CellBorderType.THIN
image CellBorderType.MEDIUM_DASH_DOT_DOT
image CellBorderType.SLANTED_DASH_DOT
image CellBorderType.MEDIUM_DASH_DOT
image CellBorderType.MEDIUM_DASHED
image CellBorderType.MEDIUM
image CellBorderType.THICK
image CellBorderType.DOUBLE

 

至于颜色那就很多了,全部在HSSFColor下面,如HSSFColor.GREEN, HSSFColor.RED,都是静态实例,可以直接引用。

下面我们假设我们要把一个单元格的四周边框都设置上,可以用下面的代码:

    //创建单元格样式
    ICellStyle cellStyle = workbook.CreateCellStyle();
   //设置为文本格式,也可以为 text,即 dataFormat.GetFormat("text");
    cellStyle.DataFormat = dataFormat.GetFormat("@");
    cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;    //下边框线
    cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;      //左边框线
    cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;     //右边框线
    cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;       //上边框线

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM