C# NPOI导出Excel和EPPlus导出Excel比较
系统中经常会使用导出Excel的功能。
之前使用的是NPOI,但是导出数据行数多就报内存溢出。
最近看到EPPlus可以用来导出Excel,就自己测了下两者导出上的差异。
NPIO官网地址:http://npoi.codeplex.com/
EPPlus官网地址:http://epplus.codeplex.com/
添加NPOI、EPPlus类库dll使用的是NuGet添加。
在类库References右键Manage NuGet Packages...,之后选择添加对应的dll。
测试结果显示,相同数据结构的数据,EPPlus的导出能力比NPOI强。
20列,NPOI能导出4万数据,导出5万数据时报内存溢出。
EPPlus能导出20万以上数据,导出23万测试时内存溢出。
NPOI导出:
private static MemoryStream ExportXlsx(DataTable dt)
{
XSSFWorkbook workbook = new XSSFWorkbook();
ISheet sheet = null; int headRowIndex = ;
string sheetName = "Sheet1";
if (!string.IsNullOrEmpty(dt.TableName))
{
sheetName = dt.TableName;
}
sheet = workbook.CreateSheet(sheetName);
int rowIndex = ; #region 列头及样式
{
XSSFRow headerRow = (XSSFRow)sheet.CreateRow(headRowIndex); ICellStyle headStyle = workbook.CreateCellStyle();
headStyle.Alignment = HorizontalAlignment.Center;
IFont font = workbook.CreateFont();
font.FontHeightInPoints = ;
font.Boldweight = ;
headStyle.SetFont(font); foreach (DataColumn column in dt.Columns)
{
headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
headerRow.GetCell(column.Ordinal).CellStyle = headStyle;
}
}
#endregion #region 填充内容 foreach (DataRow row in dt.Rows)
{
rowIndex++;
XSSFRow dataRow = (XSSFRow)sheet.CreateRow(rowIndex);
foreach (DataColumn column in dt.Columns)
{
string drValue = row[column].ToString();
dataRow.CreateCell(column.Ordinal).SetCellValue(drValue);
}
}
#endregion MemoryStream ms = new MemoryStream(); workbook.Write(ms);
ms.Flush(); return ms;
} public static void ExportXlsxByWeb(DataTable dt, string strFileName)
{ HttpContext curContext = HttpContext.Current; MemoryStream ms = ExportXlsx(dt); curContext.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8) + ".xlsx");
curContext.Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
curContext.Response.ContentEncoding = Encoding.UTF8; curContext.Response.BinaryWrite(ms.ToArray());
ms.Close();
ms.Dispose();
curContext.Response.End(); }
EPPlus导出:
/// <summary>
/// 使用EPPlus导出Excel(xlsx)
/// </summary>
/// <param name="sourceTable">数据源</param>
/// <param name="strFileName">xlsx文件名(不含后缀名)</param>
public static void ExportByEPPlus(DataTable sourceTable, string strFileName)
{
using (ExcelPackage pck = new ExcelPackage())
{
//Create the worksheet
string sheetName = string.IsNullOrEmpty(sourceTable.TableName) ? "Sheet1" : sourceTable.TableName;
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(sheetName); //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
ws.Cells["A1"].LoadFromDataTable(sourceTable, true); //Format the row
ExcelBorderStyle borderStyle = ExcelBorderStyle.Thin;
Color borderColor = Color.FromArgb(, , ); using (ExcelRange rng = ws.Cells[, , sourceTable.Rows.Count + , sourceTable.Columns.Count])
{
rng.Style.Font.Name = "宋体";
rng.Style.Font.Size = ;
rng.Style.Fill.PatternType = ExcelFillStyle.Solid; //Set Pattern for the background to Solid
rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(, , )); rng.Style.Border.Top.Style = borderStyle;
rng.Style.Border.Top.Color.SetColor(borderColor); rng.Style.Border.Bottom.Style = borderStyle;
rng.Style.Border.Bottom.Color.SetColor(borderColor); rng.Style.Border.Right.Style = borderStyle;
rng.Style.Border.Right.Color.SetColor(borderColor);
} //Format the header row
using (ExcelRange rng = ws.Cells[, , , sourceTable.Columns.Count])
{
rng.Style.Font.Bold = true;
rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(, , )); //Set color to dark blue
rng.Style.Font.Color.SetColor(Color.FromArgb(, , ));
} //Write it back to the client
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xlsx", HttpUtility.UrlEncode(strFileName, Encoding.UTF8)));
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; HttpContext.Current.Response.BinaryWrite(pck.GetAsByteArray());
HttpContext.Current.Response.End();
}
}
程序生成DataTable,20列,内容如下图
电脑配置:

测试结果:
| 条数 | NPOI | EPPlus |
| 10000 | 成功生成 | 成功生成 |
| 20000 | 成功生成 | 成功生成 |
| 30000 | 成功生成 | 成功生成 |
| 40000 | 成功生成 | 成功生成 |
| 50000 | 失败 | 成功生成 |
| 100000 | 失败 | 成功生成 |
| 200000 | 失败 | 成功生成 |
| 230000 | 失败 | 失败 |
C# NPOI导出Excel和EPPlus导出Excel比较的更多相关文章
- C# NPOI导出Excel和EPPlus导出Excel
转自:http://www.cnblogs.com/tanpeng/p/6155749.html 系统中经常会使用导出Excel的功能.之前使用的是NPOI,但是导出数据行数多就报内存溢出. 最近看到 ...
- c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出
c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出 using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using S ...
- c# .Net :Excel NPOI导入导出操作教程之List集合的数据写到一个Excel文件并导出
将List集合的数据写到一个Excel文件并导出示例: using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using System;using Sys ...
- ASP.NET使用NPOI加载Excel模板并导出下载
1.为什么要使用NPOI导出Excel? 一.解决传统操作Excel遇到的问题: 如果是.NET,需要在服务器端装Office,且及时更新它,以防漏洞,还需要设定权限允许.NET访问COM+,如果在导 ...
- 导出Excel之Epplus使用教程1(基本介绍)
1.前言 目前Epplus的介绍中文资料很少,我也一直在摸索中使用它,以下是我在使用过程中得到的经验,写出来供大家参考.本系列共4章: 导出Excel之Epplus使用教程1(基本介绍) 导出Exce ...
- 导出Excel之Epplus使用教程2(样式设置)
导出Excel之Epplus使用教程1(基本介绍) 导出Excel之Epplus使用教程2(样式设置) 导出Excel之Epplus使用教程3(图表设置) 导出Excel之Epplus使用教程4(其他 ...
- 导出Excel之Epplus使用教程3(图表设置)
导出Excel之Epplus使用教程1(基本介绍) 导出Excel之Epplus使用教程2(样式设置) 导出Excel之Epplus使用教程3(图表设置) 导出Excel之Epplus使用教程4(其他 ...
- 导出Excel之Epplus使用教程4(其他设置)
导出Excel之Epplus使用教程1(基本介绍) 导出Excel之Epplus使用教程2(样式设置) 导出Excel之Epplus使用教程3(图表设置) 导出Excel之Epplus使用教程4(其他 ...
- [转].net 使用NPOI或MyXls把DataTable导出到Excel
本文转自:http://www.cnblogs.com/yongfa365/archive/2010/05/10/NPOI-MyXls-DataTable-To-Excel-From-Excel.ht ...
随机推荐
- CYQ.Data 支持WPF相关的数据控件绑定(2013-08-09)
事件的结果 经过多天的思考及忙碌的开发及测试,CYQ.Data 终于在UI上全面支持WPF,至此,CYQ.Data 已经可以方便支持wpf的开发,同时,框架仍保留最低.net framework2.0 ...
- 走向面试之数据库基础:三、SQL进阶之变量、事务、存储过程与触发器
一.变量那点事儿 1.1 局部变量 (1)声明局部变量 DECLARE @变量名 数据类型 ) DECLARE @id int (2)为变量赋值 SET @变量名 =值 --set用于普通的赋值 SE ...
- Chrome开发者工具不完全指南(二、进阶篇)
上篇向大家介绍完了基础功能篇,这次分享的是Chrome开发工具中最有用的面板Sources. Sources面板几乎是我最常用到的Chrome功能面板,也是在我看来决解一般问题的主要功能面板.通常只 ...
- 数据库中GETDATE()函数格式化时间
SELECT CONVERT(varchar(100), GETDATE(), 0): 05 16 2016 10:57AM SELECT CONVERT(varchar(100), GETDATE( ...
- C#设计模式系列:简单工厂模式(Simple Factory)
1. 简单工厂模式简介 1.1 定义 简单工厂模式定义一个Factory类,可以根据参数的不同返回不同类的实例,被创建的实例通常有共同的父类. 简单工厂模式只需要一个Factory类. 简单工厂模式又 ...
- WPF 子窗体关闭时显示父窗体
这个问题纠结了两天,今天在一个朋友的帮助下,解决了,其实很简单,但是可能作为新手,接触WPF时间还是短,因此作为一个问题困扰了我. 父窗体部分代码 private void EditInformati ...
- windows下实现Git在局域网使用
1.首先在主机A上创建一个文件夹用于存放你要公开的版本库.然后进入这个文件夹,右键->Git create repository here,弹出的窗口中勾选Make it Bare!之后将这个文 ...
- Objective-C中把URL请求的参数转换为字典
上一篇博客中是把URL转换为字典,那么我们如何把URL请求中的参数封装成字典,然后再封装成数组呢?对OC中字符串操作熟练的小伙伴们应该觉得这是一个a+b的问题,没错把URL中的参数转换为字典主要是对字 ...
- 用html5的视频元素所遇到的第一个坑
html5 有一个video标签,这个是被大家所熟知的事情.按照w3c的规范,我认真的写出如下代码: <video preload="auto" controls=" ...
- 【HBase】HBase Getting Started(HBase 入门指南)
入门指南 1. 简介 Quickstart 会让你启动和运行一个单节点单机HBase. 2. 快速启动 – 单点HBase 这部分描述单节点单机HBase的配置.一个单例拥有所有的HBase守护线程- ...