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;