1. 程式人生 > >NPOI2.2.0.0例項詳解(七)—設定EXCEL字型

NPOI2.2.0.0例項詳解(七)—設定EXCEL字型

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();
        }
    }
}


執行後,效果如下圖所示【演示了字型不同屬性的設定】