NPOI导出Excel封装
直接上代码
public class ExcelUtils
{
public static ICellStyle CreateStyle(IWorkbook workbook,
string fontName = "宋体",
int fontSize = ,
bool isBold = false,
HorizontalAlignment horizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment verticalAlignment = VerticalAlignment.Center,
bool wrapText = false,
BorderStyle left = BorderStyle.Thin,
BorderStyle right = BorderStyle.Thin,
BorderStyle top = BorderStyle.Thin,
BorderStyle bottom = BorderStyle.Thin
)
{
IFont font = workbook.CreateFont();
font.FontName = fontName;
font.FontHeightInPoints = (short)fontSize;
font.IsBold = isBold;
ICellStyle style = workbook.CreateCellStyle();
style.SetFont(font);
style.Alignment = horizontalAlignment;
style.VerticalAlignment = verticalAlignment;
style.WrapText = wrapText;
style.BorderLeft = left;
style.BorderRight = right;
style.BorderTop = top;
style.BorderBottom = bottom;
return style;
}
} public class ExcelRow
{
/// <summary>
///
/// </summary>
public ISheet Sheet { get; set; }
/// <summary>
///
/// </summary>
public IRow Row { get; set; }
/// <summary>
///
/// </summary>
public int RowIndex { get; private set; } public int ColumnIndex { get; set; } public const int HeightConstant = ; public ExcelRow(ISheet sheet, int height = )
{
if (sheet.PhysicalNumberOfRows == )
{
this.RowIndex = ;
}
else
{
this.RowIndex = sheet.PhysicalNumberOfRows ;
}
this.ColumnIndex = ;
this.Sheet = sheet;
this.Row = this.Sheet.CreateRow(this.RowIndex);
this.Row.Height = (short)(HeightConstant * height);
} } public class ExcelCell
{
/// <summary>
///
/// </summary>
public ExcelRow Row { get; set; }
/// <summary>
///
/// </summary>
public ICell Cell { get; set; }
/// <summary>
///
/// </summary>
public int ColumnIndex { get; set; } public const int WidthConstant = ;
public ExcelCell(ExcelRow row, ICellStyle style, int width = )
{
this.Row = row;
this.ColumnIndex = this.Row.ColumnIndex++; this.Cell = this.Row.Row.CreateCell(this.ColumnIndex, CellType.Blank);
this.Row.Sheet.SetColumnWidth(this.ColumnIndex, width * WidthConstant);
this.Cell.CellStyle = style; } public ExcelCell SetValue(string value)
{
this.Cell.SetCellValue(value);
this.Cell.SetCellType(CellType.String);
return this;
} public ExcelCell SetValue(bool value)
{
this.Cell.SetCellValue(value);
this.Cell.SetCellType(CellType.Boolean);
return this;
} public ExcelCell SetValue(double value)
{
this.Cell.SetCellValue(value);
this.Cell.SetCellType(CellType.Numeric);
return this;
} public ExcelCell SetValue(DateTime value)
{
this.Cell.SetCellValue(value);
return this;
} public ExcelCell SetRowSpan(int rowspan)
{
this.Row.Sheet.AddMergedRegion(new CellRangeAddress(this.Row.RowIndex, this.Row.RowIndex + rowspan - , this.ColumnIndex, this.ColumnIndex));
return this;
} public ExcelCell SetColSpan(int colspan)
{
this.Row.Sheet.AddMergedRegion(new CellRangeAddress(this.Row.RowIndex, this.Row.RowIndex, this.ColumnIndex, this.ColumnIndex + colspan - ));
return this;
} }
调用方法:
MemoryStream stream = new MemoryStream();
IWorkbook workbook = new HSSFWorkbook();
ICellStyle headStyle = ExcelUtils.CreateStyle(workbook, "宋体", , true);
ICellStyle bodyStyle = ExcelUtils.CreateStyle(workbook, "宋体", , false); ExcelRow tr = null;
ExcelCell td = null; ISheet sheet = workbook.CreateSheet("Sheet0"); tr = new ExcelRow(sheet);
td = new ExcelCell(tr, headStyle);
td.SetValue("序号"); td = new ExcelCell(tr, headStyle);
td.SetValue("姓名"); td = new ExcelCell(tr, headStyle);
td.SetValue("金额"); for (int i = ; i < ; i++)
{
tr = new ExcelRow(sheet);
td = new ExcelCell(tr, bodyStyle);
td.SetValue(i + ); td = new ExcelCell(tr, bodyStyle);
td.SetValue("姓名"); td = new ExcelCell(tr, bodyStyle);
td.SetValue(new Random().Next(, ));
} td.SetColSpan(); //fs.Flush();
// fs.Close();
workbook.Write(stream);//将Excel写入流
stream.Flush();
stream.Position = ; File.WriteAllBytes("a.xls", stream.GetBuffer()); Console.Read();
NPOI导出Excel封装的更多相关文章
- NPOI导出Excel (C#) 踩坑 之--The maximum column width for an individual cell is 255 charaters
/******************************************************************* * 版权所有: * 类 名 称:ExcelHelper * 作 ...
- Asp.Net 使用Npoi导出Excel
引言 使用Npoi导出Excel 服务器可以不装任何office组件,昨天在做一个导出时用到Npoi导出Excel,而且所导Excel也符合规范,打开时不会有任何文件损坏之类的提示.但是在做导入时还是 ...
- NPOI导出EXCEL 打印设置分页及打印标题
在用NPOI导出EXCEL的时候设置分页,在网上有查到用sheet1.SetRowBreak(i)方法,但一直都没有起到作用.经过研究是要设置 sheet1.FitToPage = false; 而 ...
- .NET NPOI导出Excel详解
NPOI,顾名思义,就是POI的.NET版本.那POI又是什么呢?POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office的文件. 支持的文件格式包括xls, ...
- NPOI导出Excel(含有超过65335的处理情况)
NPOI导出Excel的网上有很多,正好自己遇到就学习并总结了一下: 首先说明几点: 1.Excel2003及一下:后缀xls,单个sheet最大行数为65335 Excel2007 单个sheet ...
- [转]NPOI导出EXCEL 打印设置分页及打印标题
本文转自:http://www.cnblogs.com/Gyoung/p/4483475.html 在用NPOI导出EXCEL的时候设置分页,在网上有查到用sheet1.SetRowBreak(i)方 ...
- 分享使用NPOI导出Excel树状结构的数据,如部门用户菜单权限
大家都知道使用NPOI导出Excel格式数据 很简单,网上一搜,到处都有示例代码. 因为工作的关系,经常会有处理各种数据库数据的场景,其中处理Excel 数据导出,以备客户人员确认数据,场景很常见. ...
- 用NPOI导出Excel
用NPOI导出Excel public void ProcessRequest(HttpContext context) { context.Response.ContentType = " ...
- NPOI导出Excel示例
摘要:使用开源程序NPOI导出Excel示例.NPOI首页地址:http://npoi.codeplex.com/,NPOI示例博客:http://tonyqus.sinaapp.com/. 示例编写 ...
随机推荐
- Java内存模型的基础
Java内存模型的基础 并发编程模型的两个关键问题 在并发编程中,需要处理两个关键问题:线程之间如何通信及线程之间如何同步(这里的线程是指并发执行的活动实体).通信是指线程之间以何种机制来交换信息.在 ...
- Java线程池的增长过程
通过ThreadPoolExecutor的方式创建线程池 ThreadPoolExecutor 构造方法: public ThreadPoolExecutor(int corePoolSize, in ...
- Spring浅入浅出——不吹牛逼不装逼
Spring浅入浅出——不吹牛逼不装逼 前言: 今天决定要开始总结框架了,虽然以前总结过两篇,但是思维是变化的,而且也没有什么规定说总结过的东西就不能再总结了,是吧.这次总结我命名为浅入浅出,主要在于 ...
- java根据经纬度查询门店地理位置-完美解决附近门店问题
1.首先我们需要创建一个门店表如下: CREATE TABLE `app_store` ( `store_id` ) NOT NULL AUTO_INCREMENT COMMENT '发布id', ` ...
- 安装yarn实况
[**前情提要**]最近在gayhub上面得到一个开源项目,遂准备研究一下源码,当然第一步就是要把项目运行起来.然后看了一下技术栈,发现包管理工具是使用yarn,以前也听说过yarn但是也没有具体使用 ...
- Docker部署网站之后映射域名
Docker中部署tomcat相信大家也都知道,不知道的可以google 或者bing 一下.这里主要是为了记录在我们启动容器之后,tomcat需要直接定位到网站信息,而不是打开域名之后,还得加个bl ...
- 洛谷 P1631 序列合并
题意简述 有两个长度都是N的序列A和B,在A和B中各取一个数相加可以得到N^2个和,求这N^2个和中最小的N个. 题解思路 大根堆,先存入n个和,再比较大小,改变堆中元素. 代码 #include & ...
- 使用 php 内部web服务器
使用 php 内部web服务器如网站目录 d:\web\index.php1.打开命令窗口,输入下列3条命令cd d:cd d:\web\index.phpphp -S localhost:80802 ...
- File Compression and Archiving in linux (linux 中文件的归档)
1. Compressing Files at the Shell Prompt Red Hat Enterprise Linux provides the bzip2, gzip, and zip ...
- Redis缓存,持久化,高可用
一,Redis作缓存服务器 本篇博客是接着上一篇博客未分享完的技术点. redis作为缓存服务器是众多企业中的选择之一,虽然该技术很成熟但也是存在一定的问题.就是缓存带来的缓存穿透,缓存击穿, ...