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/. 示例编写 ...
随机推荐
- win10教育版激活错误:在运行 Microsoft Windows 非核心版本的计算机上,运行"slui.exe ...”
折腾了一天,最终轻松解决,先启用Software Protection服务,在激活(密钥或者工具都行). PS:但是这样还是无法解决Software Protection自动停止的问题,这个可以参考网 ...
- Computing Jobs
docker&k8shadoopsparkhbasemesosrediskafkazookeeper SCSI.NVMe.PCIe devops
- Tomcat源码分析 (三)----- 生命周期机制 Lifecycle
Tomcat里面有各种各样的组件,每个组件各司其职,组件之间又相互协作共同完成web服务器这样的工程.在这些组件之上,Lifecycle(生命周期机制)至关重要!在学习各个组件之前,我们需要看看Lif ...
- Python3 反射
反射 python面向对象中的反射:通过字符串的形式操作对象相关的属性 hasattr(obj,name) # hasattr(obj, name) # 判断一个对象是否有指定的属性name,返回Tr ...
- Python.append()与Python.extend()的区别
lst=[1,2] >>>[1,2] lst.append([3,4]) >>>[1, 2, [3, 4]] lst.extend([3,4]) >>& ...
- h5微信浏览器复制粘贴--ios兼容问题的解决方法(clipboard.js插件)
前段时间在做微信h5的时候,遇到了ios兼容,使用clipboard.js插件完美解决 下载地址:下载地址: https://github.com/zenorocha/clipboard.js cnd ...
- .netcore持续集成测试篇之测试方法改造
系列目录 通过前面两节讲解,我们的测试类中已经有两个测试方法了,总体上如下 public class mvc20 { private readonly HttpClient _client; publ ...
- 约会安排 HDU - 4553(线段树区间查询,区间修改,区间合并)
题目: 寒假来了,又到了小明和女神们约会的季节. 小明虽为屌丝级码农,但非常活跃,女神们常常在小明网上的大段发言后热情回复“呵呵”,所以,小明的最爱就是和女神们约会.与此同时,也有很多基友找他开黑, ...
- 深入研究BufferedInputStream内幕
目录 1 概述 2 BufferedInputStream源码分析 3 BufferedInputStream在实际场景中,没有太多用处 4 BufferedInputStream唯一使用场景 1 概 ...
- Flutter学习笔记(20)--FloatingActionButton、PopupMenuButton、SimpleDialog、AlertDialog、SnackBar
如需转载,请注明出处:Flutter学习笔记(20)--FloatingActionButton.PopupMenuButton.SimpleDialog.AlertDialog.SnackBar F ...