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 Program3
    {
        static void Main(string[] args)
        {
            //說明:設定單元格對齊方式

            //1.建立EXCEL中的Workbook         
            IWorkbook myworkbook = new XSSFWorkbook();

            //2.建立Workbook中的Sheet        
            ISheet mysheet = myworkbook.CreateSheet("sheet1");

            mysheet.SetColumnWidth(0, 24 * 256);
            mysheet.SetColumnWidth(1, 24 * 256);
            mysheet.SetColumnWidth(2, 24 * 256);
            mysheet.SetColumnWidth(3, 24 * 256);

            //3.建立Row中的Cell並賦值
            IRow row0 = mysheet.CreateRow(0);
            row0.Height = 50 * 20;
            row0.CreateCell(0).SetCellValue("對齊方式");
            row0.CreateCell(1).SetCellValue("對齊方式");
            row0.CreateCell(2).SetCellValue("對齊方式");
            row0.CreateCell(3).SetCellValue("對齊方式");

            IRow row1 = mysheet.CreateRow(1);
            row1.Height = 50 * 20;
            row1.CreateCell(0).SetCellValue("對齊方式");
            row1.CreateCell(1).SetCellValue("Shanghai is the largest city by population in ");
            row1.CreateCell(2).SetCellValue("對齊方式");
            row1.CreateCell(3).SetCellValue("對齊方式");

            //4.建立CellStyle
            ICellStyle style0 = myworkbook.CreateCellStyle();
            style0.Alignment = HorizontalAlignment.General;//【General】數字、時間預設:右對齊;BOOL:預設居中;字串:預設左對齊

            ICellStyle style1 = myworkbook.CreateCellStyle();
            style1.Alignment = HorizontalAlignment.Left;//【Left】左對齊

            ICellStyle style2 = myworkbook.CreateCellStyle();
            style2.Alignment = HorizontalAlignment.Center;//【Center】居中

            ICellStyle style3 = myworkbook.CreateCellStyle();
            style3.Alignment = HorizontalAlignment.Right;//【Right】右對齊

            ICellStyle style4 = myworkbook.CreateCellStyle();
            style4.Alignment = HorizontalAlignment.Fill;//【Fill】填充

            ICellStyle style5 = myworkbook.CreateCellStyle();
            style5.Alignment = HorizontalAlignment.Justify;//【Justify】兩端對齊[會自動換行](主要針對英文)

            ICellStyle style6 = myworkbook.CreateCellStyle();
            style6.Alignment = HorizontalAlignment.CenterSelection;//【CenterSelection】跨列居中

            ICellStyle style7 = myworkbook.CreateCellStyle();
            style7.Alignment = HorizontalAlignment.Distributed;//【Distributed】分散對齊[會自動換行]

            //【Tips】
            // 1.通過ICellStyle的VerticalAlignment屬性可以設定垂直對齊模式與水平對齊無異 不再演示
            // 2.通過ISheet的SetDefaultColumnStyle(int column, ICellStyle style)方法可以設定整列的預設單元格樣式;
            
            //5.將CellStyle應用於具體單元格
            row0.GetCell(0).CellStyle = style0;
            row0.GetCell(1).CellStyle = style1;
            row0.GetCell(2).CellStyle = style2;
            row0.GetCell(3).CellStyle = style3;

            row1.GetCell(0).CellStyle = style4;
            row1.GetCell(1).CellStyle = style5;
            row1.GetCell(2).CellStyle = style6;
            row1.GetCell(3).CellStyle = style7;

            //6.儲存       
            FileStream file = new FileStream(@"E:\myworkbook3.xlsx", FileMode.Create);
            myworkbook.Write(file);
            file.Close();
        }
    }
}
執行後,效果如下圖所示