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. DataGridView中的rows.Count比实际行数多1的原因以及解决办法

    场景 DataGridView怎样实现添加.删除.上移.下移一行: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10281414 ...

  2. Spring之 JDBC 异常

    JDBC异常抽象 Spring会将数据操作的异常转换为DataAccessException 解析错误码 SQLErrorCodeSQLExceptionTranslator ErrorCode定义 ...

  3. CTF丨Linux Pwn入门教程:针对函数重定位流程的相关测试(下)

    Linux Pwn入门教程系列分享已接近尾声,本套课程是作者依据i春秋Pwn入门课程中的技术分类,并结合近几年赛事中出现的题目和文章整理出一份相对完整的Linux Pwn教程. 教程仅针对i386/a ...

  4. 一文解读DDD (转)

    从遇到问题开始 当人们要做一个软件系统时,一般总是因为遇到了什么问题,然后希望通过一个软件系统来解决. 比如,我是一家企业,然后我觉得我现在线下销售自己的产品还不够,我希望能够在线上也能销售自己的产品 ...

  5. SparkStreaming wordCountDemo基础案例

    体现sparkStreaming的秒级准实时性,所以我们需要一个能够持续输入数据的东东 1.CentOS上下载nc 创建一个scala工程,导入相关pom依赖 <?xml version=&qu ...

  6. ckeditor4.7配置图片上传

    ckeditor作为老牌的优秀在线编辑器,一直受到开发者的青睐. 这里我们讲解下 ckeditor最新版本4.7的图片上传配置. https://ckeditor.com/ 官方 进入下载 https ...

  7. nginx代理ambassador,再转到mlfow-tracking服务

    这个服务的代理,相对于服务网关来说,有些典型, 今天调通了,作个记录. 一,nginx配置 upstream ai_ambassador { ip_hash; server 1.2.3.4:30080 ...

  8. 5. Vue - 小清单实例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Python高级特性之:List Comprehensions、Generator、Dictionary and set ...

    今天帅气的易哥和大家分享的是Pyton的高级特性,希望大家能和我一起学习这门语言的魅力. Python高级特性之:List Comprehensions.Generator.Dictionary an ...

  10. Node.js Koa2开发微信小程序服务端

    1.promise.async.await const Koa = require('koa') const app = new Koa() // 应用程序对象 有很多中间件 // 发送HTTP KO ...