NPOI 设置单元格边框

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

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

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

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

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

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

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

ISheet sheet = hssfworkbook.CreateSheet("new sheet");

// Create a row and put some cells in it. Rows are 0 based.
IRow row = sheet.CreateRow(1);
// Create a cell and put a value in it.
ICell cell = row.CreateCell(1);
// Style the cell with borders all around.
ICellStyle style = hssfworkbook.CreateCellStyle();
style.BorderBottom= CellBorderType.THIN;
style.BorderLeft= CellBorderType.THIN;
style.BorderRight= CellBorderType.THIN;
style.BorderTop = CellBorderType.THIN;
cell.CellStyle= style;


这段代码使用了最普通的细边框,使得这个单元格看上去像块空心砖头。

image

注意:这里我们没有设置边框的颜色,但这不会影响最终的效果,因为Excel会用默认的黑色给边框上色。

如果要设置颜色的话,也很简单,如下:

 
style.BottomBorderColor= HSSFColor.GREEN.index;

以上代码将底部边框设置为绿色,要注意,不是直接把HSSFColor.GREEN赋给XXXXBorderColor属性,而是把index的值赋给它。

转载于:https://www.cnblogs.com/kingangWang/archive/2012/02/06/2339502.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值