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 ...
随机推荐
- wpf datetime format
<Style TargetType="{x:Type DatePickerTextBox}"> <Setter Property="Control.Te ...
- python基础(23):面向过程与面向对象的优劣、初识面向对象
1. 面向过程与面向对象的优劣 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. 优点是:极大的降低了写程 ...
- 如何访问到静态的文件,如jpg,js,css.
如果你的DispatcherServlet拦截"*.do"这样的有后缀的URL,就不存在访问不到静态资源的问题. 如果你的DispatcherServlet拦截"/&qu ...
- LinuxShell脚本——认识Shell脚本
LinuxShell脚本——认识Shell脚本 摘要:本文主要介绍了Shell脚本的一些基本知识. 什么是Shell脚本 shell脚本是利用shell的功能所写的一个程序,这个程序是使用纯文本文件, ...
- PHP面试题2019年滴滴出行工程师面试题及答案解析
一.单选题(共30题,每题5分) 1.下列关于PHP垃圾回收的说法,错误的是? A.开启/关闭垃圾回收机制可以通过修改php配置实现 B.可以在程序中使用gc_enable() 和 gc_disabl ...
- SpringBoot(三) 配置文件 篇章
SpringBoot 配置文件默认为application.properties,但是本章节主要讲解yaml文件配置,因为现在的趋势是使用yaml,它是类似于标准通用标记语言的子集XML的数据描述语言 ...
- CSS语法规范一
CSS语法规范 CSS规则由两个主要的部分构成:选择器以及一条或多条声明. p{ color: red; font-size: 12px; } CSS代码风格 样式格式书写 紧凑格式 h3 {colo ...
- Xcode模拟器无法启动解决办法
今天遇到模拟器无法启动问题,点击模拟器或者Xcode build模拟器就一直跳,跳一会就不跳了,然后查看模拟器状态,显示为无响应.或者黑屏,等半天不动. 如果你有类似情况可以尝试在终端执行以下命令: ...
- webpack中使用jquery
首先我只有一个js文件,只用在那个文件中引入,因此 一 下载包 npm install jquery --save 二 在使用的jquery的文件顶部输入js代码 import $ from 'jq ...
- docker系列(四):数据卷
1 引言 容器就相当于一个简易的操作系统,我们在上面部署我们的环境,不可避免地产生一些数据,但是,可能由于断电等等原因,容器退出了,那么之前容器中的数据就不符存在,则往往不是我们想要的,更多的,我们是 ...