直接上代码

    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封装的更多相关文章

  1. NPOI导出Excel (C#) 踩坑 之--The maximum column width for an individual cell is 255 charaters

    /******************************************************************* * 版权所有: * 类 名 称:ExcelHelper * 作 ...

  2. Asp.Net 使用Npoi导出Excel

    引言 使用Npoi导出Excel 服务器可以不装任何office组件,昨天在做一个导出时用到Npoi导出Excel,而且所导Excel也符合规范,打开时不会有任何文件损坏之类的提示.但是在做导入时还是 ...

  3. NPOI导出EXCEL 打印设置分页及打印标题

    在用NPOI导出EXCEL的时候设置分页,在网上有查到用sheet1.SetRowBreak(i)方法,但一直都没有起到作用.经过研究是要设置  sheet1.FitToPage = false; 而 ...

  4. .NET NPOI导出Excel详解

    NPOI,顾名思义,就是POI的.NET版本.那POI又是什么呢?POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office的文件. 支持的文件格式包括xls, ...

  5. NPOI导出Excel(含有超过65335的处理情况)

    NPOI导出Excel的网上有很多,正好自己遇到就学习并总结了一下: 首先说明几点: 1.Excel2003及一下:后缀xls,单个sheet最大行数为65335 Excel2007 单个sheet ...

  6. [转]NPOI导出EXCEL 打印设置分页及打印标题

    本文转自:http://www.cnblogs.com/Gyoung/p/4483475.html 在用NPOI导出EXCEL的时候设置分页,在网上有查到用sheet1.SetRowBreak(i)方 ...

  7. 分享使用NPOI导出Excel树状结构的数据,如部门用户菜单权限

    大家都知道使用NPOI导出Excel格式数据 很简单,网上一搜,到处都有示例代码. 因为工作的关系,经常会有处理各种数据库数据的场景,其中处理Excel 数据导出,以备客户人员确认数据,场景很常见. ...

  8. 用NPOI导出Excel

    用NPOI导出Excel public void ProcessRequest(HttpContext context) { context.Response.ContentType = " ...

  9. NPOI导出Excel示例

    摘要:使用开源程序NPOI导出Excel示例.NPOI首页地址:http://npoi.codeplex.com/,NPOI示例博客:http://tonyqus.sinaapp.com/. 示例编写 ...

随机推荐

  1. 深入理解JVM-类加载器深入解析(1)

    类加载 在java代码中,类型的加载,连接与初始化过程都是在程序运行期间完成的 类型:表示的Object本身,并不是指一个对象,也就是class. 运行期间:表示的是一种runtime的概念,在运行期 ...

  2. Java Lambda表达式forEach无法跳出循环的解决思路

    Java Lambda表达式forEach无法跳出循环的解决思路 如果你使用过forEach方法来遍历集合,你会发现在lambda表达式中的return并不会终止循环,这是由于lambda的底层实现导 ...

  3. sed流编辑器

    一.前言 (一).sed 工作流程 sed 是一种在线的.非交互式的流编辑器,它一次处理一行内容.处理时,把当做前处理的行存储在临时缓存区中,成为“模式空间”(pattern space),接着用se ...

  4. SpringMVC项目案例之---数据的获取与显示

    数据的获取与显示 (一)功能 1.对用户输入的数据进行获取 2.将获取的数据显示到页面 3.使用了SpringMVC技术的注解方式 4.使用了过滤器,处理中文乱码问题 5.在web.xml中设置了访问 ...

  5. JVM系列(3)- Java VisualVM使用

    前言 Java VisualVM是jdk自带一款工具,可以十分友好的监控java进程相关的应用服务及中间件. 工具位置 jdk的bin目录下,找到jvisualvm.exe,双击打开即可. 功能介绍 ...

  6. python+unittest框架第二天unittest之简单认识Test Suite:测试套件

    今天了解下测试套件Test Suite,什么是测试套件,测试套件是由多个Test Case测试用例组成的,当然也可以由多个子测试套件组成. 接下来看下如果构建测试套件,构建测试套件的方法: 1.用un ...

  7. Oracle中的日期函数

    (一)查询系统的当前日期用sysdate,用法如下: select sysdate from dual 日期操作的三个格式: 日期-数字=日期 日期+=日期 日期-日期=数字(天数) (二)常用的日期 ...

  8. mybatis 源码分析(三)Executor 详解

    本文将主要介绍 Executor 的整体结构和各子类的功能,并对比效率: 一.Executor 主体结构 1. 类结构 executor 的类结构如图所示: 其各自的功能: BaseExecutor: ...

  9. 第一章 .NET基础-1.1.学前入门

    一.1.1. 概念:.NET和C# l .NET/DOTNET:一般指.Net Framework框架.一种平台,一种技术.它提供了一个稳定的运行环境:来保障我们.Net平台正常的运转. l C#(C ...

  10. springmvc集成swaggerui

    这里先写下需要的pom.xml配置(我引用的2.4.0,相对稳定) 在集成springfox-swagger2之前,我也尝试着集成了swagger-springmvc,方式差不多,但是swagger- ...