1. Border settings
ICellStyle cellstyle= workbook.CreateCellStyle();
/*
* 边框样式(四周边框),如果文本框内本身有内容,设置边框样式会失效
*/
//下边框
cellstyle.BorderBottom = BorderStyle.Thin;
//上边框
cellstyle.BorderTop = BorderStyle.Hair;
//左边框
cellstyle.BorderLeft = BorderStyle.Medium;
//右边框
cellstyle.BorderRight = BorderStyle.Dotted;
//其它边框---实线类
cellstyle.BorderBottom = BorderStyle.Thin;//细实线(黑色)
cellstyle.BorderBottom = BorderStyle.Medium;//粗实线(黑色)
cellstyle.BorderBottom = BorderStyle.Thick;//更粗的实线
cellstyle.BorderBottom = BorderStyle.Double;//细的双实线
//其它边框---虚线类
cellstyle.BorderBottom = BorderStyle.Hair;//点状的虚线
cellstyle.BorderBottom = BorderStyle.Dashed;//细的,较短虚线
cellstyle.BorderBottom = BorderStyle.Dotted;//细的,短的的虚线(比dashed更短)
cellstyle.BorderBottom = BorderStyle.MediumDashed;//粗的,长的短横 虚线
cellstyle.BorderBottom = BorderStyle.DashDot;//细的,长短交替的虚线
cellstyle.BorderBottom = BorderStyle.MediumDashDot;//粗的,长短交替的虚线
cellstyle.BorderBottom = BorderStyle.DashDotDot;//细的 长短短交替的虚线
cellstyle.BorderBottom = BorderStyle.MediumDashDotDot;//粗的,长短短交替的虚线
cellstyle.BorderBottom = BorderStyle.SlantedDashDot;//连接较为紧密的虚线(有点花那种)
/*
* 边框颜色
*/
//下边框颜色
cellstyle.BottomBorderColor = 10;//红色
//左边框颜色
cellstyle.LeftBorderColor = 10;
//右边框颜色
cellstyle.RightBorderColor = 10;
//上边框颜色
cellstyle.TopBorderColor = 10;
2. Text position
//水平位置
cellstyle.Alignment = HorizontalAlignment.Center;//居中
cellstyle.Alignment = HorizontalAlignment.General;//常规
cellstyle.Alignment = HorizontalAlignment.Left;//靠左
cellstyle.Alignment = HorizontalAlignment.Right;//靠右
cellstyle.Alignment = HorizontalAlignment.Fill;//填充
cellstyle.Alignment = HorizontalAlignment.Justify;//两端对齐
cellstyle.Alignment = HorizontalAlignment.CenterSelection;//跨列居中
cellstyle.Alignment = HorizontalAlignment.Distributed;//分散对齐(缩进)
//垂直位置
cellstyle.VerticalAlignment = VerticalAlignment.Top;
cellstyle.VerticalAlignment = VerticalAlignment.Center;
cellstyle.VerticalAlignment = VerticalAlignment.Bottom;
cellstyle.VerticalAlignment = VerticalAlignment.Justify;//两端对齐
cellstyle.VerticalAlignment = VerticalAlignment.Distributed;//分散对齐
3. Font settings
/*
* 字体设置
* 行设置了字体后,excel中所有的行的字体样式都发生了改变,不仅仅是我设置了字体样式的行
* 列设置字体后,其他行的字体也发生了改变
* 单独获取单元格的cellstyle之后,对这个对象设置对应字体,仍然是全局的字体发生变化
*/
IFont font = cellstyle.GetFont(workbook);
font.FontName = "微软雅黑";//字体
font.Color = 10;//颜色
//font.FontHeight = 15;//字体高度---不要使用
font.FontHeightInPoints = 15;//字体高度(与excel中的字号一致)
font.IsBold = true;//是否加粗
font.IsItalic = true;//是否斜体
font.IsStrikeout = true;//是否加删除线
cellstyle.SetFont(font);
Note: Have questions about font settings?
1. Why does the global font style change when the font is set?
2. Why does FontName have no effect?
4. Color settings
/*
* 单元格背景色设置
*/
cellstyle.FillForegroundColor = 10;//10代表红色
cellstyle.FillPattern = FillPattern.SolidForeground;//必须设置这个,否则样式无效
Remarks: This way of setting colors is not very scientific. You need to know the corresponding values of various colors in advance, and then set the corresponding colors.
Here is a temporary color comparison table, a better way to summarize in the new blog post