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
- NPOI2.2.0.0实例详解(四)—设置EXCEL单元格对齐方式
- NPOI2.2.0.0实例详解(三)—设置EXCEL列宽、行高与合并单元格
- NPOI2.2.0.0实例详解(五)—设置EXCEL单元格背景与图案
- NPOI2.2.0.0实例详解(六)—设置EXCEL单元格边框
- NPOI2.2.0.0实例详解(八)—设置EXCEL单元格【数字格式】
- NPOI2.2.0.0实例详解(九)—设置EXCEL单元格【时间格式】
- NPOI2.2.0.0实例详解(十)—设置EXCEL单元格【文本格式】
- NPOI2.2.0.0实例详解(七)—设置EXCEL字体
- NPOI2.2.0.0实例详解(二)—使用NPOI创建EXCEL
- NPOI2.2.0.0实例详解(十一)—向EXCEL插入图片
- 【rzxt】系统设置Excel单元格数据的对齐方式技巧
- NPOI2.2.0.0实例详解(一)—NPOI的下载与引用
- Excel操作——单元格的字体和对齐方式
- Excel操作——单元格的字体和对齐方式
- Java中JTbale使用—设置单元格对齐方式
- cxGrid动态设置单元格对齐方式
- NPOI2.0操作导出excel
- JXL导出EXCEL合并单元格、文字对齐方式
- 配色速成指导
- opencv 从内存加载图像
- 赛马问题
- RMAN实战1:RMAN备份数据库、控制文件、参数文件、FRA、归档日志
- android 通过Build获取手机硬件信息
- NPOI2.2.0.0实例详解(四)—设置EXCEL单元格对齐方式
- Zookeeper工作流程
- java高并发框架 SSM框架 详细整合教程(Spring+SpringMVC+MyBatis)
- log4j日志之xml形式
- androidstudio的Rendering Problems Missing styles错误解决
- Nginx应用案例分享:压力测试
- Qt遍历ui窗口控件
- js获取事件对象
- 数据结构与算法------线性查找