【一步一步学NPOI】3.设置单元格宽高和边框

1.宽度

excel中,一列的单元格宽度是一样的,所以宽度操作的对象是sheet

 var cellDate = row1.CreateCell(0);
            cellDate.SetCellValue("我是一个顽皮的单元格");
            sheet1.SetColumnWidth(0, 30 * 256);

SetColumnWidth()方法有两个参数,第一个是列的索引,从0开始。注意第二个参数,是以1/256为单位。

2.高度

每一行的高度都一样,所以高度操作的对象是row

row1.HeightInPoints = 20;//row1.Height = 20 * 20;

注意两者的区别,关于height的解释: Get the row's height measured in twips (1/20th of a point). If the height is

height始终是heightinpoints的1/20。

3.默认宽高

  sheet1.DefaultColumnWidth = 10 * 256;
  sheet1.DefaultRowHeightInPoints = 10;

4.边框

边框属性都是基于这两个类:HSSFCellStyle(样式)、HSSFColor(颜色)

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

  HSSFCellStyle style = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
            //设置边框格式
            style.BorderTop = NPOI.SS.UserModel.BorderStyle.Double;
            style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Double;
            style.BorderRight = NPOI.SS.UserModel.BorderStyle.Medium;
            style.BorderBottom = NPOI.SS.UserModel.BorderStyle.SlantedDashDot;
            //边框颜色
            style.LeftBorderColor = HSSFColor.Blue.Index;
            style.RightBorderColor = HSSFColor.Red.Index;
            style.BottomBorderColor = HSSFColor.Pink.Index;
            style.TopBorderColor = HSSFColor.Green.Index;
            cellDate.CellStyle = style;


  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值