using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using System.Diagnostics;

namespace ConsoleApp326
{
class Program
{
static string excelFullName = Directory.GetCurrentDirectory() + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".xlsx";
static string logFullName = Directory.GetCurrentDirectory() + "\\" + DateTime.Now.ToString("yyyyMMdd") + "log.txt";
static int exportNum = 0;
static string exportMsg = "";
static Stopwatch stopWatch = new Stopwatch();
static void Main(string[] args)
{
ExportOrderList();
}

static void ExportOrderList()
{
List<SalesOrderDetail> orderList = GetOrdersDetailList();
ExportTData<SalesOrderDetail>(orderList);
}
static List<SalesOrderDetail> GetOrdersDetailList()
{
List<SalesOrderDetail> orderList = new List<SalesOrderDetail>();
using (AdventureWorks2017Entities db = new AdventureWorks2017Entities())
{
orderList = db.SalesOrderDetails.ToList();
orderList.AddRange(orderList);
orderList.AddRange(orderList);
orderList.AddRange(orderList);
if(orderList!=null && orderList.Any())
{
return orderList;
}
}
return null;
}
static void ExportTData<T>(List<T> dataList)
{
stopWatch.Start();
if(dataList==null || !dataList.Any())
{
return;
}

XSSFWorkbook book;
ISheet firstSheet;
try
{
book = new XSSFWorkbook();
var firstRowData = dataList.FirstOrDefault();
var props = firstRowData.GetType().GetProperties().ToList();
firstSheet = book.CreateSheet("First Sheet");
if (props!=null && props.Any())
{
IRow headerRow = firstSheet.CreateRow(0);
for(int i=0;i<props.Count;i++)
{
ICell headerCell = headerRow.CreateCell(i);
if(!string.IsNullOrEmpty(props[i].Name))
{
headerCell.SetCellValue(props[i].Name);
}
}
}

for(int rowIndex=1;rowIndex<=dataList.Count;rowIndex++)
{
IRow dataRow = firstSheet.CreateRow(rowIndex);
for(int j=0;j<props.Count;j++)
{
ICell dataCell = dataRow.CreateCell(j);
var dataCellValue = props[j].GetValue(dataList[rowIndex - 1]);
if(dataCellValue!=null)
{
dataCell.SetCellValue(dataCellValue.ToString());
}
}
}

using (FileStream excelStream = File.OpenWrite(excelFullName))
{
book.Write(excelStream);
}

stopWatch.Stop();
exportMsg = $"Export excel name {excelFullName},export num {exportNum}, " +
$"time cost {stopWatch.ElapsedMilliseconds}" +
$" milliseconds, memory {Process.GetCurrentProcess().PrivateMemorySize64}";
LogMessage(exportMsg);
}
catch(Exception ex)
{
LogMessage(ex.TargetSite.ToString());
}
}

static void LogMessage(string msg)
{
using (StreamWriter logStream = new StreamWriter(logFullName, true))
{
logStream.WriteLine(msg + Environment.NewLine);
}
}
}
}

C# NPOI Excel的更多相关文章

  1. 转载 NPOI Excel 单元格背景颜色对照表

    NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置,FillP ...

  2. NPOI Excel 单元格背景颜色对照表

    NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置,FillP ...

  3. <转载>NPOI Excel 单元格背景颜色对照表

    我转载地址:http://www.holdcode.com/web/details/117 NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 ...

  4. [C#] NPOI Excel解析

    通过NPOI解析Excel,将数据保存到DataTable中. #region excel解析 public DataTable ImportExcelFile(string filePath) { ...

  5. NPOI Excel类

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using NPOI.HSSF.Us ...

  6. NPOI Excel导入 导出

    添加引用 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System; using System.Collections.Gene ...

  7. winform npoi excel 样式设置

    IWorkbook excel = new HSSFWorkbook();//创建.xls文件 ISheet sheet = excel.CreateSheet("sheet1") ...

  8. NPOI Excel表格处理

    //创建一个Excel文件 HSSFWorkbook work = new HSSFWorkbook(); //新建一个工作表 ISheet sheet1 = work.CreateSheet(&qu ...

  9. winform NPOI excel 导出并选择保存文件路径

    public void ExcelOp(DataGridView gdv,ArrayList selHead) { if (selHead.Count==0) { MessageBox.Show(&q ...

随机推荐

  1. SPA项目开发登陆注册

    使用vue-cli脚手架工具创建一个vue项目 vue init webpack pro01 npm安装elementUI cd pro01 #进入新建项目的根目录 安装: npm install a ...

  2. VMWare虚拟机应用介绍

    目录 一:VMWare简介 二:VMWare启动第一个虚拟机 一:VMWare简介   VMWare虚拟机软件是一个"虚拟PC"软件,它使你可以在一台机器上同时运行二个或更多Win ...

  3. kali linux maltego-情报收集工具

    Maltego是一个交互式数据挖掘工具,它为链接分析呈现有向图.该工具用于在线调查,以发现互联网上各种来源的信息片段之间的关系. 注册Maltego账号,注册地址:https://www.paterv ...

  4. 告诉你如何在Xcode中安装炫酷的插件!!!!!

    (如果你已经有了自己喜欢的插件,你可以直接找到该插件的下载地址,下载下来,参照下面的步骤来安装到Xcode工程之中) 如果还没有找到自己想要的插件,那么推荐你在开源中国社区http://www.osc ...

  5. Python当中的array数组对象

    计算机为数组分配一段连续的内存,从而支持对数组随机访问:由于项的地址在编号上是连续的,数组某一项的地址可以通过将两个值相加得出,即将数组的基本地址和项的偏移地址相加.数组的基本地址就是数组的第一项的机 ...

  6. 使history命令显示时间

      添加环境变量HISTTIMEFORMAT就行了 # export HISTTIMEFORMAT='%F %T ' .csharpcode, .csharpcode pre { font-size: ...

  7. 爬虫---lxml简单操作

    前几篇写了一些Beautiful Soup的一些简单操作,也拿出来了一些实例进行实践,今天引入一个新的python库lxmt,lxmt也可以完成数据的爬取哦 什么是lxml lxml是python的一 ...

  8. Html学习之九(CSS选择器的使用--位置选择器)

    位置选择器: 一..:first-child选择器 <!doctype html> <html> <head> <meta charset="utf ...

  9. Pwn-level3

    题目地址 https://dn.jarvisoj.com/challengefiles/level3.rar.2047525b05c499c9dd189ba212bba1f8 借鉴 https://w ...

  10. cookiecutter

    1.安装cookiecutter pip3 install cookiecutter 2.设置模板 https://github.com/cookiecutter/cookiecutter   搜索需 ...