直接上代码

    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. 【Java例题】2.2 分数类

    2.定义分数类,包括分子和分母变量.构造方法. 加减乘除方法.化简方法.值计算方法和显示分子和分母的方法. 然后编写一个主类,在其主方法中通过定义两个分数对象来 显示每一个分数的分子值.分母值.化简和 ...

  2. C#使用WebClient调用接口

    用于上传图片base64位 private void upLoadCunzai() { errorstring += " upLoadCunzai方法执行成功:用于上传已经存在人员摄像头照片 ...

  3. 逆向破解之160个CrackMe —— 004-005

    CrackMe —— 004 160 CrackMe 是比较适合新手学习逆向破解的CrackMe的一个集合一共160个待逆向破解的程序 CrackMe:它们都是一些公开给别人尝试破解的小程序,制作 c ...

  4. 建立第一个G2图表

    Step1:引进G2脚本 方法一:引入在线脚本 <script src="https://gw.alipayobjects.com/os/lib/antv/g2/3.4.10/dist ...

  5. Spark 系列(十一)—— Spark SQL 聚合函数 Aggregations

    一.简单聚合 1.1 数据准备 // 需要导入 spark sql 内置的函数包 import org.apache.spark.sql.functions._ val spark = SparkSe ...

  6. 云上RDS架构

    概述 越来越多的企业选择上云,最基础的云服务就是IaaS(Infrastructure as a Service)服务,直观理解就是虚拟主机,用户不用再自建机房,自己购买服务器,而是直接向云厂商购买虚 ...

  7. (17)ASP.NET Core EF基于数据模型创建数据库

    1.简介 使用Entity Framework Core构建执行基本数据访问的ASP.NET Core MVC应用程序.使用迁移(Migrations)基于数据模型创建数据库,你可以在Windows上 ...

  8. Hive 系列(一)—— Hive 简介及核心概念

    一.简介 Hive 是一个构建在 Hadoop 之上的数据仓库,它可以将结构化的数据文件映射成表,并提供类 SQL 查询功能,用于查询的 SQL 语句会被转化为 MapReduce 作业,然后提交到 ...

  9. 重读《学习JavaScript数据结构与算法-第三版》- 第5章 队列

    定场诗 马瘦毛长蹄子肥,儿子偷爹不算贼,瞎大爷娶个瞎大奶奶,老两口过了多半辈,谁也没看见谁! 前言 本章为重读<学习JavaScript数据结构与算法-第三版>的系列文章,主要讲述队列数据 ...

  10. 聊聊我在这家公司设计的SSO

    最近小明遇到一个需求:需要将几个独立的系统(子系统)汇总到一个集中的系统(父系统)当中,当用户在父系统登录过后,再点击这几个子系统,就可以免登录跳转到任意一个系统.当时一听,duang~duang~就 ...