NPOI2.2.0.0实例详解(四)—设置EXCEL单元格对齐方式

来源:互联网 发布:数据采集与处理官网 编辑:程序博客网 时间:2024/05/18 09:03
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();        }    }}
运行后,效果如下图所示
1 0