NPOI2.2.0.0实例详解(七)—设置EXCEL字体

来源:互联网 发布:过滤器里面返回json 编辑:程序博客网 时间:2024/05/22 05:05
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using NPOI.HSSF.UserModel;using NPOI.SS.Formula.Eval;using NPOI.SS.Formula.Functions;using NPOI.SS.UserModel;using NPOI.XSSF.UserModel;using NPOI.POIFS.FileSystem;using NPOI.HPSF;using System.IO;using NPOI.SS.Util;using System.Drawing;using NPOI.HSSF.Util;namespace NPOI{    class Program6    {        static void Main(string[] args)        {            //说明:设置字体            //1.创建EXCEL中的Workbook                     IWorkbook myworkbook = new XSSFWorkbook();            //2.创建Workbook中的Sheet                    ISheet mysheet = myworkbook.CreateSheet("sheet1");            mysheet.DefaultRowHeight = 20 * 20;            //3.创建Row中的Cell并赋值            IRow row0 = mysheet.CreateRow(0); row0.CreateCell(0).SetCellValue("中文"); row0.CreateCell(1).SetCellValue("原始字体");            IRow row1 = mysheet.CreateRow(1); row1.CreateCell(0).SetCellValue("中文"); row1.CreateCell(1).SetCellValue("Boldweight");            IRow row2 = mysheet.CreateRow(2); row2.CreateCell(0).SetCellValue("中文"); row2.CreateCell(1).SetCellValue("Color");            IRow row3 = mysheet.CreateRow(3); row3.CreateCell(0).SetCellValue("中文"); row3.CreateCell(1).SetCellValue("FontHeight");            IRow row4 = mysheet.CreateRow(4); row4.CreateCell(0).SetCellValue("中文"); row4.CreateCell(1).SetCellValue("FontHeightInPoints");            IRow row5 = mysheet.CreateRow(5); row5.CreateCell(0).SetCellValue("中文"); row5.CreateCell(1).SetCellValue("FontName");            IRow row6 = mysheet.CreateRow(6); row6.CreateCell(0).SetCellValue("中文"); row6.CreateCell(1).SetCellValue("IsBold");            IRow row7 = mysheet.CreateRow(7); row7.CreateCell(0).SetCellValue("中文"); row7.CreateCell(1).SetCellValue("IsItalic");            IRow row8 = mysheet.CreateRow(8); row8.CreateCell(0).SetCellValue("中文"); row8.CreateCell(1).SetCellValue("IsStrikeout");            IRow row9 = mysheet.CreateRow(9); row9.CreateCell(0).SetCellValue("中文"); row9.CreateCell(1).SetCellValue("TypeOffset");            IRow row10 = mysheet.CreateRow(10); row10.CreateCell(0).SetCellValue("中文"); row10.CreateCell(1).SetCellValue("Underline");            //4.设置字体样式            IFont font1 = myworkbook.CreateFont();            font1.Boldweight = (Int16)FontBoldWeight.Bold;            //【Tips】            // 1.Boldweight 要使用(Int16)FontBoldWeight 对应的数值 否则无效            IFont font2 = myworkbook.CreateFont();            font2.Color = IndexedColors.Red.Index;            IFont font3 = myworkbook.CreateFont();            font3.FontHeight = 17;            IFont font4 = myworkbook.CreateFont();            font4.FontHeightInPoints = 17;            IFont font5 = myworkbook.CreateFont();            font5.FontName = "黑体";            IFont font6 = myworkbook.CreateFont();            font6.IsBold = true;            IFont font7 = myworkbook.CreateFont();            font7.IsItalic = true;            IFont font8 = myworkbook.CreateFont();            font8.IsStrikeout = true;            IFont font9 = myworkbook.CreateFont();            font9.TypeOffset = FontSuperScript.Sub;            IFont font10 = myworkbook.CreateFont();            font10.Underline = FontUnderlineType.Single;            //5.创建CellStyle并加载字体            ICellStyle style1 = myworkbook.CreateCellStyle();            style1.SetFont(font1);            ICellStyle style2 = myworkbook.CreateCellStyle();            style2.SetFont(font2);            ICellStyle style3 = myworkbook.CreateCellStyle();            style3.SetFont(font3);            ICellStyle style4 = myworkbook.CreateCellStyle();            style4.SetFont(font4);            ICellStyle style5 = myworkbook.CreateCellStyle();            style5.SetFont(font5);            ICellStyle style6 = myworkbook.CreateCellStyle();            style6.SetFont(font6);            ICellStyle style7 = myworkbook.CreateCellStyle();            style7.SetFont(font7);            ICellStyle style8 = myworkbook.CreateCellStyle();            style8.SetFont(font8);            ICellStyle style9 = myworkbook.CreateCellStyle();            style9.SetFont(font9);            ICellStyle style10 = myworkbook.CreateCellStyle();            style10.SetFont(font10);            //6.将CellStyle应用于具体单元格            row1.GetCell(0).CellStyle = style1;            row2.GetCell(0).CellStyle = style2;            row3.GetCell(0).CellStyle = style3;            row4.GetCell(0).CellStyle = style4;            row5.GetCell(0).CellStyle = style5;            row6.GetCell(0).CellStyle = style6;            row7.GetCell(0).CellStyle = style7;            row8.GetCell(0).CellStyle = style8;            row9.GetCell(0).CellStyle = style9;            row10.GetCell(0).CellStyle = style10;            //7.保存                   FileStream file = new FileStream(@"E:\myworkbook6.xlsx", FileMode.Create);            myworkbook.Write(file);            file.Close();        }    }}


运行后,效果如下图所示【演示了字体不同属性的设置】
1 0
原创粉丝点击