C# NPOI导出Excel和EPPlus导出Excel
转自:http://www.cnblogs.com/tanpeng/p/6155749.html
系统中经常会使用导出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导出:

1 private static MemoryStream ExportXlsx(DataTable dt)
2 {
3 XSSFWorkbook workbook = new XSSFWorkbook();
4 ISheet sheet = null;
5
6 int headRowIndex = 0;
7 string sheetName = "Sheet1";
8 if (!string.IsNullOrEmpty(dt.TableName))
9 {
10 sheetName = dt.TableName;
11 }
12 sheet = workbook.CreateSheet(sheetName);
13 int rowIndex = 0;
14
15 #region 列头及样式
16 {
17 XSSFRow headerRow = (XSSFRow)sheet.CreateRow(headRowIndex);
18
19 ICellStyle headStyle = workbook.CreateCellStyle();
20 headStyle.Alignment = HorizontalAlignment.Center;
21 IFont font = workbook.CreateFont();
22 font.FontHeightInPoints = 10;
23 font.Boldweight = 700;
24 headStyle.SetFont(font);
25
26 foreach (DataColumn column in dt.Columns)
27 {
28 headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
29 headerRow.GetCell(column.Ordinal).CellStyle = headStyle;
30 }
31 }
32 #endregion
33
34 #region 填充内容
35
36 foreach (DataRow row in dt.Rows)
37 {
38 rowIndex++;
39 XSSFRow dataRow = (XSSFRow)sheet.CreateRow(rowIndex);
40 foreach (DataColumn column in dt.Columns)
41 {
42 string drValue = row[column].ToString();
43 dataRow.CreateCell(column.Ordinal).SetCellValue(drValue);
44 }
45 }
46 #endregion
47
48
49 MemoryStream ms = new MemoryStream();
50
51 workbook.Write(ms);
52 ms.Flush();
53
54 return ms;
55 }
56
57 public static void ExportXlsxByWeb(DataTable dt, string strFileName)
58 {
59
60 HttpContext curContext = HttpContext.Current;
61
62 MemoryStream ms = ExportXlsx(dt);
63
64 curContext.Response.AppendHeader("Content-Disposition",
65 "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8) + ".xlsx");
66 curContext.Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
67 curContext.Response.ContentEncoding = Encoding.UTF8;
68
69 curContext.Response.BinaryWrite(ms.ToArray());
70 ms.Close();
71 ms.Dispose();
72 curContext.Response.End();
73
74 }

EPPlus导出:

1 /// <summary>
2 /// 使用EPPlus导出Excel(xlsx)
3 /// </summary>
4 /// <param name="sourceTable">数据源</param>
5 /// <param name="strFileName">xlsx文件名(不含后缀名)</param>
6 public static void ExportByEPPlus(DataTable sourceTable, string strFileName)
7 {
8 using (ExcelPackage pck = new ExcelPackage())
9 {
10 //Create the worksheet
11 string sheetName = string.IsNullOrEmpty(sourceTable.TableName) ? "Sheet1" : sourceTable.TableName;
12 ExcelWorksheet ws = pck.Workbook.Worksheets.Add(sheetName);
13
14 //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
15 ws.Cells["A1"].LoadFromDataTable(sourceTable, true);
16
17 //Format the row
18 ExcelBorderStyle borderStyle = ExcelBorderStyle.Thin;
19 Color borderColor = Color.FromArgb(155, 155, 155);
20
21 using (ExcelRange rng = ws.Cells[1, 1, sourceTable.Rows.Count + 1, sourceTable.Columns.Count])
22 {
23 rng.Style.Font.Name = "宋体";
24 rng.Style.Font.Size = 10;
25 rng.Style.Fill.PatternType = ExcelFillStyle.Solid; //Set Pattern for the background to Solid
26 rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(255, 255, 255));
27
28 rng.Style.Border.Top.Style = borderStyle;
29 rng.Style.Border.Top.Color.SetColor(borderColor);
30
31 rng.Style.Border.Bottom.Style = borderStyle;
32 rng.Style.Border.Bottom.Color.SetColor(borderColor);
33
34 rng.Style.Border.Right.Style = borderStyle;
35 rng.Style.Border.Right.Color.SetColor(borderColor);
36 }
37
38 //Format the header row
39 using (ExcelRange rng = ws.Cells[1, 1, 1, sourceTable.Columns.Count])
40 {
41 rng.Style.Font.Bold = true;
42 rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
43 rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(234, 241, 246)); //Set color to dark blue
44 rng.Style.Font.Color.SetColor(Color.FromArgb(51, 51, 51));
45 }
46
47 //Write it back to the client
48 HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
49 HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xlsx", HttpUtility.UrlEncode(strFileName, Encoding.UTF8)));
50 HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
51
52 HttpContext.Current.Response.BinaryWrite(pck.GetAsByteArray());
53 HttpContext.Current.Response.End();
54 }
55 }

程序生成DataTable,20列,内容如下图
电脑配置:

测试结果:
| 条数 | NPOI | EPPlus |
| 10000 | 成功生成 | 成功生成 |
| 20000 | 成功生成 | 成功生成 |
| 30000 | 成功生成 | 成功生成 |
| 40000 | 成功生成 | 成功生成 |
| 50000 | 失败 | 成功生成 |
| 100000 | 失败 | 成功生成 |
| 200000 | 失败 | 成功生成 |
| 230000 | 失败 | 失败 |
C# NPOI导出Excel和EPPlus导出Excel的更多相关文章
- C# NPOI导出Excel和EPPlus导出Excel比较
系统中经常会使用导出Excel的功能. 之前使用的是NPOI,但是导出数据行数多就报内存溢出. 最近看到EPPlus可以用来导出Excel,就自己测了下两者导出上的差异. NPIO官网地址:http: ...
- 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 ...
随机推荐
- ubuntu server 无线网卡的处理
1) iwconfig 确定一下接口的名称 2) 编辑 /etc/network/interfaces 加入下面的代码 auto wlan0 iface wlan0 inet dhcp wpa-ssi ...
- 16S 基础知识、分析工具和分析流程详解
工作中有个真理:如果你连自己所做的工作的来龙去脉都讲不清楚,那你是绝对不可能把这份工作做好的. 这适用于任何行业.如果你支支吾吾,讲不清楚,那么说难听点,你在混日子,没有静下心来工作. 检验标准:随时 ...
- ubuntu mysql主从库的搭建
1,首先我们要确定一个从库一个主库,紧记从库只能读取不能有其他的操作,如果操作写那主从就失效了,那就看看我们这么搭建主从吧! 2. 环境:Ubuntu,Mysql (主从的数据库版本必须保持一致) 主 ...
- OAF页面集成条形码或者二维码
OAF页面集成条形码 OAF生成条形码 OAF页面集成二维码跟这个类似,生成二维码需要以下jar包,zxing-core.jar, zxing-javase.jar,可自行去maven下载. 代码如下 ...
- quartz---springmvc的配置文件正合
quartz---springmvc的配置文件正合 XML <beans xmlns="http://www.springframework.org/schema/beans" ...
- 阿里云ECS服务器自定义端口无法访问问题记录
记住阿里云ECS服务器有个安全组!!! 购买了阿里云服务器的时候,购买界面那里是可以勾选默认的几个端口是否开启的,服务器默认勾了22端口,使用户能登录服务器. 当我们在服务器里面配置nginx,开启自 ...
- spring cloud学习(七)Spring Cloud Config(续)
Spring Cloud Config(续) 个人参考项目 个人博客 : https://zggdczfr.cn/ 个人参考项目 : (整合到上一个案例中)https://github.com/Fun ...
- jsp jsp的基本语法
jsp模板元素 jsp页面中的HTML 内容称为JSP模板元素 jsp模板元素定义了网页的基本骨架,即定义了页面结构和外观 jsp表达式 jsp脚本表达式用于将程序数据输出到客户端 语法& ...
- o(1)取b > a,且b的二进制中1的个数等于a二进制中1的个数,且使b最小
给你一个uint32 a,让你找到另一个uint32 b,使b > a,且b的二进制中1的个数等于a二进制中1的个数.且使b最小.(数据保证可出) 1 因为1的个数不变,所以必然大于n+lowb ...
- linux目录管理、时钟管理、文件查看命令
inux的两种时钟: 系统时钟:由Linux内核通过CPU的工作频率进行的计时: 硬件时钟: hwclock: 显示硬件时钟 -s, --hctosys -w, --systohc cal:日历 目录 ...