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 ...
随机推荐
- C# 获取系统硬件可用信息
如何获取系统可用磁盘和可用内存? 1 获取磁盘信息 这个有自带的 DriveInfo 可以用. DriveInfo 类 (System.IO) | Microsoft Docs 2 获取内存信息 参考 ...
- PHP中设计模式以及魔术方法
1.设计模式 1.1单例模式 口诀:三私一公 1.私有的静态属性用来保存对象的单例 2.私有的构造方法用来阻止在类的外部实例化 3.私有的__clone阻止在类的外部clo ...
- 2019年上半年收集到的人工智能AutoML干货文章
2019年上半年收集到的人工智能AutoML干货文章 自动机器学习简述(AutoML) 谷歌将AutoML应用于Transformer架构,翻译结果飙升,已开源 IBM推出AutoAI,让企业人工智能 ...
- 关于UIScollView中的contentOffset的理解
大家对UIScollView 中的contentOffset 一直有疑问. 当时我也有好多疑问,后来在网上找了一下资料,发现没有找到合理的解释,因此自己就查看了一下官方文档,自己好好的研究了一番. 现 ...
- Python 字符串用法总结
一.将某个对象转换为字符串,有str()和repr()两种方法 区别:repr() 转化为供解释器读取的形式str() 转化为适于人阅读的形式 a = 123456 print('repr输出:', ...
- MySQL 部署分布式架构 MyCAT (四)
分片(水平拆分) 2.取模分片(mod-long) cd /data/mycat/conf cp schema.xml schema.xml.rang-long vi schema.xml <? ...
- hisi mmz模块驱动讲解
一.概述 如图所示,在海思平台上将内存分为两个部分:os内存和mmz内存.os内存指:由linux操作系统管理的内存:mmz内存:由mmz驱动模块进行管理供媒体业务单独使用的内存,在驱动加载时可以指定 ...
- 抓包工具Fiddler的简单使用
HTTP代理 http代理,就是代理客户机的http访问,主要代理浏览器访问页面 代理服务器是介于浏览器和web服务器之间的一台服务器,有了它之后,浏览器不是直接到Web服务器去取回网页而是向代理服务 ...
- Debian创建.desktop文件(Create .desktop file in Debian/Gnome)
在Debian系Linux中,用于标识应用的启动文件.desktop file是位于/usr/share/applications目录下的,Gnome会将这些文件在菜单中展示为启动图标,也可以固定在d ...
- 无序数组中用 快速排序的分治思想 寻找第k大元素
#include <stdio.h> int *ga; int galen; void print_a(){ ; i < galen; i++){ printf("%d & ...