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 ...
随机推荐
- 微信网站登录doem
直接上代码 namespace CloudPrj.WeiXin { public partial class index : System.Web.UI.Page { ...
- python基础(2):python的安装、第一个python程序
1. 第一个python程序 1.1 python的安装 自己百度,这是自学最基本的,安装一路确定即可,记得path下打钩. 1.2 python的编写 python程序有两种编写方式: 1.进入cm ...
- PHP+Ajax点击加载更多列表数据实例
一款简单实用的PHP+Ajax点击加载更多列表数据实例,实现原理:通过“更多”按钮向服务端发送Ajax请求,PHP根据分页参数查询将最新的几条记录,数据以JSON形式返回,前台Query解析JSON数 ...
- 为什么要学 Python? python该怎么学
很多童鞋对为什么学习Python感到迷茫,小编来跟大家说说学习Python的10个理由,希望可以帮助到大家!!! 摘要: 看完这十个理由,我决定买本python从入门到精通! 如果你定期关注现今的科技 ...
- Oracle 中Number的长度定义
Number可以通过如下格式来指定:Field_NAME Number(precision ,scale),其中precision指Number可以存储的最大数字长度(不包括左右两边的0),scale ...
- 证券secuerity英语secuerity安全
中文名:证券 外文名:security.secuerity 类别:经济权益凭证统称 组成:资本证券.货币证券和商品证券 作用:用来证明持者权益的法律凭证 图集 目录 1 发展历程 ? 世界 ? 中国 ...
- 微信语音短消息amr文件转WAV
- amr说明 - 转WAV程序 我对SILK编码库稍作修改,编译了一个Windows下可直接将SILK V3编码转换为WAV格式,并支持原生的微信语音短消息amr文件的版本,提供给大家使用,压缩包( ...
- CVE-2019-0708-BlueKeep漏洞复现
环境 攻击机:Kali Linux IP:192.168.0.108 靶机:Windows Sever 7 SP1 旗舰版 IP:192.168.0.109 Exploit: https://gith ...
- Android native进程间通信实例-binder篇之——解决实际问题inputreader内建类清楚缓存
我在实际开发中,遇到一个问题,在电容屏驱动中没有发送input_sync 给上层,导致电容屏有的数据缓存在inputreader 中,会导致系统一系列奇怪问题发生, 至于为什么驱动不发送input_s ...
- Scala开发问题汇总
1.JDK版本问题 Error:java.lang.VerifyError: Uninitialized Exception Details: Location: scala/collection/i ...