直接上代码

    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. 快速字符串匹配一: 看毛片算法(KMP)

    前言 由于需要做一个快速匹配敏感关键词的服务,为了提供一个高效,准确,低能耗的关键词匹配服务,我进行了漫长的探索.这里把过程记录成系列博客,供大家参考. 在一开始,接收到快速敏感词匹配时,我就想到了 ...

  2. 解决报错:类型“System.Object”在未被引用的程序集中定义。必须添加对程序集“System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”的引用

    Razor视图引擎中,使用部分视图编译报错 类型“System.Object”在未被引用的程序集中定义.必须添加对程序集“System.Runtime, Version=4.0.0.0, Cultur ...

  3. 使用Arthas 获取Spring ApplicationContext还原问题现场

    ## 背景 最近来了个实习僧小弟,安排他实现对目标网站 连通性检测的小功能,简单讲就是将下边的shell 脚本换成Java 代码来实现 ``` 1#!/bin/bash 2URL="http ...

  4. RBF神经网络

    RBF神经网络 RBF神经网络通常只有三层,即输入层.中间层和输出层.其中中间层主要计算输入x和样本矢量c(记忆样本)之间的欧式距离的Radial Basis Function (RBF)的值,输出层 ...

  5. linux 配置ssh无密码登录不起作用的解决方案

    1.安装ssh 直接 sudo apt-get install openssh-server 2.查看ssh运行状态 ps -e | grep ssh 如果发现 sshd 和 ssh-agent 即表 ...

  6. 2019牛客多校训练第三场H.Magic Line(思维)

    题目传送门 大致题意: 输入测试用例个数T,输入点的个数n(n为偶数),再分别输入n个不同的点的坐标,要求输出四个整数x1,y1,x2,y2,表示有一条经过点(x1,y1),(x2,y2)的直线将该二 ...

  7. Redis集群与spring的整合

    上一篇详细的赘述了Redis的curd操作及集群的搭建.下面我们开始将他整合到我们实际的项目中去.我的项目采用的是标准的ssm框架,ssm框架这里不说,直接开始整合. 首先在maven管理中将我们的j ...

  8. 使用ArrayPool池化大型数组(翻译)

    原文链接:https://adamsitnik.com/Array-Pool/ 使用ArrayPool 简介 .NET的垃圾收集器(GC)实现了许多性能优化,其中之一就是,设定年轻的对象很快消亡,然而 ...

  9. 大白话5分钟带你走进人工智能-第32节集成学习之最通俗理解XGBoost原理和过程

    目录 1.回顾: 1.1 有监督学习中的相关概念 1.2  回归树概念 1.3 树的优点 2.怎么训练模型: 2.1 案例引入 2.2 XGBoost目标函数求解 3.XGBoost中正则项的显式表达 ...

  10. CPU中的cache结构以及cache一致性

    一. 引子 在多线程环境中,经常会有一些计数操作,用来统计线上服务的一些qps.平均延时.error等.为了完成这些统计,可以实现一个多线程环境下的计数器类库,方便记录和查看用户程序中的各类数值.在实 ...