zoukankan      html  css  js  c++  java
  • 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}. There are totally {exportNum} salesorderdetails and cost {watch.ElapsedMilliseconds} millseconds and now is {DateTime.Now.ToString("yyyyMMddHHmmssfff")} ");
    File.AppendAllText(logPath, exportMsg);
    }
    arr = null;
    }
    }
    }
    }

  • 相关阅读:
    App_Data文件夹的用处.
    .net工资管理系统 C#2.0开发
    红警2尤里的复仇 无限金钱 无限电力修改器
    用bho方式拦截中国电信流氓广告
    仙剑4按键取钱的东东。
    多浏览器测试的利器IETester,自带IE5.5,IE6,IE7,IE8等多个内核.
    dataGridView使用指南系列一、回车换行或换列完美解决方案
    红警2共和国之辉无限金钱修改器,造东西不花钱还赚钱。
    UpdatePanel控件,你真的会用了吗?
    XP+WIN7共享动态磁盘的一点收获
  • 原文地址:https://www.cnblogs.com/Fred1987/p/10211619.html
Copyright © 2011-2022 走看看