//创建样式
CellStyle cellStyle = workbook.createCellStyle();
文字位置: //水平方向 cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中cellStyle.setAlignment(HorizontalAlignment.LEFT);//靠左 cellStyle.setAlignment(HorizontalAlignment.RIGHT);//靠右 cellStyle.setAlignment(HorizontalAlignment.GENERAL);//常规 cellStyle.setAlignment(HorizontalAlignment.FILL);//填充 cellStyle.setAlignment(HorizontalAlignment.JUSTIFY);//两端对齐 cellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION);//跨列居中 cellStyle.setAlignment(HorizontalAlignment.DISTRIBUTED);//分散对齐(缩进) //上下居中 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); cellStyle.setVerticalAlignment(VerticalAlignment.TOP); cellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM); cellStyle.setVerticalAlignment(VerticalAlignment.DISTRIBUTED);//分散对齐(缩进) cellStyle.setVerticalAlignment(VerticalAlignment.JUSTIFY);//两端对齐
字体设置:
/*⾏设置了字体后,excel中所有的⾏的字体样式都发⽣了改变 */
Font font = cellstyle.GetFont(workbook);
font.FontName ="微软雅⿊";//字体
font.Color =10;//颜⾊
font.FontHeightInPoints =15;//字体⾼度(与excel中的字号⼀致)
font.IsBold =true;//是否加粗
font.IsItalic =true;//是否斜体
font.IsStrikeout =true;//是否加删除线
cellstyle.SetFont(font);
边框设置:
实线类
BorderStyle.Thin;//细实线(⿊⾊)
BorderStyle.Medium;//粗实线(⿊⾊)
BorderStyle.Thick;//更粗的实线
BorderStyle.Double;//细的双实线
虚线类
BorderStyle.Hair;//点状的虚线
BorderStyle.Dashed;//细的,较短虚线
BorderStyle.Dotted;//细的,短的的虚线(⽐dashed更短)
BorderStyle.MediumDashed;//粗的,长的短横虚线
BorderStyle.DashDot;//细的,长短交替的虚线
BorderStyle.MediumDashDot;//粗的,长短交替的虚线
BorderStyle.DashDotDot;//细的长短短交替的虚线
BorderStyle.MediumDashDotDot;//粗的,长短短交替的虚线
BorderStyle.SlantedDashDot;//连接较为紧密的虚线(有点花那种)
上下左右四种边框:
cellStyle.setBorderBottom();
cellStyle.setBorderTop();
cellStyle.setBorderLeft();
cellStyle.setBorderRight();
边框颜色设置:
cellStyle.setBottomBorderColor(); cellStyle.setLeftBorderColor(); cellStyle.setRightBorderColor(); cellStyle.setTopBorderColor();
单元格背景颜色设置
cellStyle.setFillForegroundColor((short)10);//设置前景色 10为红色 cellStyle.setFillBackgroundColor((short)10);//设置背景色 cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//必须设置 否则无效