C# NPOI导出Excel

创建工作簿

XSSFWorkbook workbook = new XSSFWorkbook();

一、创建工作表 Sheet

1.名称、列宽、行高

//创建工作表,定义工作表名称
ISheet INFO = workbook.CreateSheet("summary");

//设置该工作表全部单元格的默认列宽
INFO.DefaultColumnWidth = 20;

//对某一列设置宽度  10000 - 38.44
INFO.SetColumnWidth(0, 2145);//第一列
INFO.SetColumnWidth(1, 2145);
INFO.SetColumnWidth(2, 3200);

//对某一行设置行高(不常用,一般在创建IRow的时候设置)
INFO.GetRow(1).Height = 10;//第二行  //Height的单位是:缇(Twips) (缇:计量单位,等于“磅”的 1/20)

2.合并单元格

//合并单元格从a+1行到b+1行 c+1列到d+1列 索引从0开始
INFO.AddMergedRegion(new CellRangeAddress(a, b, c, d));

//eg.
CellRangeAddress rregion = new CellRangeAddress(0, 1, 2, 3);
//这里设置合并单元格的框线格式
summarySheet.AddMergedRegion(rregion);

*设置合并单元格框线

RegionUtil.SetBorderLeft(1, region, sheet);//左
RegionUtil.SetBorderRight(1, region, sheet);//右
RegionUtil.SetBorderTop(1, region, sheet);//顶
RegionUtil.SetBorderBottom(1, region, sheet);//底

二、格式设置

1.创建Font字体格式

字号/字体类型/字体加粗

IFont font = workbook.CreateFont();//创建字体样式
font.FontHeightInPoints = 10;//字号
titleFont.FontName = "华文楷体";//字体类型
font3.IsBold = true;//字体加粗

* 绑定Font字体格式至单元格格式

单元格格式对象.SetFont(font);//设置Font格式

2.创建单元格格式

文本自动换行

ICellStyle txt_style = workbook.CreateCellStyle();
txt_style.SetFont(font);//给单元格格式设置字体格式 上面提到的字体格式使用

txt_style.WrapText = true;//文本自动换行

2.1 对齐方式

txt_style.Alignment = 
HorizontalAlignment.Center; //水平方向:居中
HorizontalAlignment.Left; //水平方向:左对齐
HorizontalAlignment.Right;//水平方向:右对齐

txt_style.VerticalAlignment =
VerticalAlignment.Center;//垂直方向:居中
VerticalAlignment.Bottom;//垂直方向:底部对齐
VerticalAlignment.Top;//垂直方向:顶部对齐

2.2 填充背景颜色

using System.Drawing; //Color

cell_style.FillPattern = FillPattern.SolidForeground;//见图 一定要写否则不会填充颜色
cell_style.FillForegroundColor = 10;//自定义的话就随便填

//填充背景颜色
Color color_name = Color.FromArgb(150, 151, 150);//方法一 用RGB定义颜色
Color color_name = Color.Yellow; //方法二 用一些已经设定好了的颜色值
((XSSFColor)cell_style.FillForegroundColorColor).SetRgb(new byte[] { color_name.R, color_name.G, color_name.B }); 
*如何用16进制的字符串获取颜色填充给单元格的背景上
string a = "#" + a; //十六进制的只能走FromHtml这个方法拿到Color对象 一定要加#
Color colorH = ColorTranslator.FromHtml(a); //拿到Color对象
((XSSFColor)Color_style.FillForegroundColorColor).SetRgb(new byte[] { colorH.R, colorH.G, colorH.B });
cell.CellStyle = Color_style;//设置样式

图片来源见右下角水印

2.3 框线格式

//单元格框线 见图
cell_style.BorderBottom = BorderStyle.Thin;//下
cell_style.BorderRight = BorderStyle.Thin;//右
cell_style.BorderTop = BorderStyle.Thin;//上
cell_style.BorderLeft = BorderStyle.Thin;//左

图片来源见右下角水印

三、创建行Row

//创建Row行
IRow roww = null;//可以设null,可以重复使用


 //对该行设置行高
roww = INFO.CreateRow(1);    //创建第二行对象
roww.Height = 559;            //Height的单位是:缇(Twips) (缇:计量单位,等于“磅”的 1/20)

*创建后注意不要再次创建了,不然会发生覆盖的情况

四、创建单元格Cell

1.文本、绑定格式

ICell cell = null;//创建Cell单元格

cell = roww.CreateCell(0);    //创建第二行的第一列单元格对象
cell.SetCellValue("test2");    //设置单元格文本值
cell.CellStyle = cell_style;    //设置单元格格式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值