第八章——ICellStyle单元格样式操作

1.边框设置

			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.文字位置

 			//水平位置
            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.字体设置

            /*
             * 字体设置
             * 行设置了字体后,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);

备注:字体设置尚有疑问?
1.为什么设置了字体,全局的字体样式会发生变化?
2.为什么设置了FontName没有效果?

4.颜色设置

			/*
             * 单元格背景色设置
             */
            cellstyle.FillForegroundColor = 10;//10代表红色
            cellstyle.FillPattern = FillPattern.SolidForeground;//必须设置这个,否则样式无效

备注:这种设置颜色的方式不太科学,需要提前知道各种颜色对应的数值,然后设置对应的颜色。
这里临时先提供一个颜色的对照表,更好的方法在新的博文中做总结
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_39541254/article/details/107935095