C#,NPOI,Export Generic T Data
1.Nuget 下载NPOI;
Install-package NPOI -version 2.4.1
2.下载EF
install-package entityframework -version 6.2.0
3.添加数据,ef model.edmx
4.建议使用NPOI.XSSF.UserModel;应为XSSF最大行数为1048575,而HSSFWorkBook 最大行数为65535行
5.数据量不能太大,要不然会FileStream写入时会OutOfMemoryException(多写几次),建议100万行以内
当内存溢出时,弹出如下的消息,将break前面的勾选框去掉.
70万数据大概需时160s;
Managed Debugging Assistant 'ContextSwitchDeadlock' : 'The CLR has been unable to transition from COM context 0x142a240 to COM context 0x142a2f8 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.'
代码如下,亲测有效
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.XSSF.UserModel;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace ConsoleApp315
{
class Program
{
static Stopwatch watch = new Stopwatch();
static int exportNum = 0;
static string logPath = Directory.GetCurrentDirectory() + "\\ExportLog.txt";
static string exportMsg = "";
[STAThread]
static void Main(string[] args)
{
Thread exportThread = new Thread(() =>
{
using (AdventureWorks2017Entities db = new AdventureWorks2017Entities())
{
List<SalesOrderDetail> orderList = db.SalesOrderDetails.ToList();
orderList.AddRange(orderList);
orderList.AddRange(orderList);
ExportTEntity<SalesOrderDetail>(orderList.ToArray());
}
});
exportThread.SetApartmentState(ApartmentState.STA);
exportThread.Start();
exportThread.Join();
System.Windows.Forms.MessageBox.Show(exportMsg);
Console.ReadLine();
}
[STAThread]
static void ExportTEntity<T>(T[] arr)
{
if (arr != null && !arr.Any())
{
return;
}
exportNum = arr.Length;
using (SaveFileDialog sfd = new SaveFileDialog())
{
watch.Start();
XSSFWorkbook book;
sfd.Filter = "Excel Files(.xls)|*.xls|Excel Files(.xlsx)| *.xlsx | All Files | *.*";
sfd.FileName = DateTime.Now.ToString("yyyyMMddHHmmssffffff") + Guid.NewGuid().ToString() + ".xlsx";
sfd.InitialDirectory = Directory.GetCurrentDirectory();
if (sfd.ShowDialog() == DialogResult.OK)
{
var firstRowData = arr.FirstOrDefault();
book = new XSSFWorkbook();
var sheet = book.CreateSheet("Sheet1");
var firstRow = sheet.CreateRow(0);
var propertiesArr = firstRowData.GetType().GetProperties().Where(x => !x.GetMethod.IsVirtual).ToArray();
for (int i = 0; i < propertiesArr.Length; i++)
{
var column = firstRow.CreateCell(i);
column.SetCellValue(propertiesArr[i].Name);
}
for (int i = 1; i <= arr.Length; i++)
{
var indexRow = sheet.CreateRow(i);
for (int j = 0; j < propertiesArr.Length; j++)
{
var indexColumn = indexRow.CreateCell(j);
var indexColumnName = propertiesArr[j];
var columnValue = indexColumnName.GetValue(arr[i - 1]);
if (columnValue != null)
{
indexColumn.SetCellValue(columnValue.ToString());
}
}
}
using (FileStream stream = File.OpenWrite(sfd.FileName))
{
book.Write(stream);
stream.Close();
}
watch.Stop();
exportMsg = string.Format($"Saved in {sfd.FileName}.\nThere are totally {exportNum} salesorderdetails and cost {watch.ElapsedMilliseconds} millseconds and now is {DateTime.Now.ToString("yyyyMMddHHmmssfff")} \n\n");
File.AppendAllText(logPath, exportMsg);
}
arr = null;
}
}
}
}
C#,NPOI,Export Generic T Data的更多相关文章
- maatwebsite lost precision when export long integer data
Maatwebsite would lost precision when export long integer data, no matter string or int storaged in ...
- 获取对象属性值=NPOI EXPORT
使用dll ==== NPOI.dll 获取属性,设置属性=参考:http://blog.csdn.net/cestarme/article/details/6548126 额外的: 导出的时候碰到一 ...
- C# NPOI Export DataTable C# NPOI导出DataTable 单元格自适应大小
1.Install-Package NPOI -v 2.4.0 2. using NPOI.XSSF; using NPOI.XSSF.UserModel; using NPOI.SS.UserMod ...
- vooya --- a YUV player and a generic raw data player
vooya是一个raw数据播放器,可播放yuv数据,兼容win.linex以及mac平台. 下载地址:https://www.offminor.de/(见最下面) ubuntu需要安装依赖: apt ...
- NetSuite SuiteScript 2.0 export data to Excel file(xls)
In NetSuite SuiteScript, We usually do/implement export data to CSV, that's straight forward: Collec ...
- Export SQLite data to Excel in iOS programmatically(OC)
//For the app I have that did this, the SQLite data was fairly large. Therefore, I used a background ...
- 基于NPOI导出和导入Excel
概述 NPOI,顾名思义,就是POI的.NET版本.NPOI就是用.NET语言编写的一套数据导出Excel的开源项目,支持XML.xls.xlsx.ppt等格式..NET不仅实现Excel导出还可以实 ...
- PE Header and Export Table for Delphi
Malware Analysis Tutorial 8: PE Header and Export Table 2. Background Information of PE HeaderAny bi ...
- ASP.NET基于NPOI导出数据
using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...
随机推荐
- Vue中计算属性、侦听、过滤、自定义指令、ref的操作
1.计算属性 <div id="app"> <input type="text" v-model="x"> < ...
- katalon studio升级到6.3.3版本后如何生成测试报告
背景: katalon studio 6.3.0版本开始,默认不会生成测试报告,因此,原先自动化运行结果的邮件也就不会包含测试报告附件. 解决方法如下: 通过插件[basic reports]生成测试 ...
- 使用Fiddler进行HTTP流量分析
- 安装 Fiddler是一款免费软件,可以到其官网下载,地址是https://www.telerik.com/fiddler,也可以从我的网盘中下载,发送"fiddler"获取下 ...
- mssql sqlserver if exists 用法大汇总
摘要: 下文讲述sqlserver中,更新脚本中常用if exists关键字的用法说明,如下所示: 实验环境:sql server 2008 R2 一.检测数据库是否存在于当前数据库引擎下 if ex ...
- mysql 实现全连接
mysql不支持全连接,但可以通过左外连接+ union+右外连接实现 SELECT * FROM t1 LEFT JOIN t2 ON t1.id = t2.id UNION SELECT * FR ...
- Debian创建.desktop文件(Create .desktop file in Debian/Gnome)
在Debian系Linux中,用于标识应用的启动文件.desktop file是位于/usr/share/applications目录下的,Gnome会将这些文件在菜单中展示为启动图标,也可以固定在d ...
- 常见的Dos命令大全
打开cmd: Win键+R 输入cmd; 常用的Dos命令: 1.盘符切换: 2.打开文件目录: dir 3.清理屏幕: cls 4.退出: exit 5.查看本机IP地址:ipconfig ...
- Centos 7 自动安装系统-pxe
一.简介 PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过网络从远端服 ...
- Java:String,int相互转化
int转String int a: a + “” String.valueOf(a) Interger.toString(a) 一般使用以上几种方法进行转化 第一种方法效率不好,ja ...
- SpringMVC拦截器(四)
拦截器,本质类似于AOP,主要的应用场景: 日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV等. 权限检查:如登录检测,进入处理器检测是否登录,没有登录返回登录页面. 性能监控:记录 ...