C# NPOI Excel
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的更多相关文章
- 转载 NPOI Excel 单元格背景颜色对照表
NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置,FillP ...
- NPOI Excel 单元格背景颜色对照表
NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置,FillP ...
- <转载>NPOI Excel 单元格背景颜色对照表
我转载地址:http://www.holdcode.com/web/details/117 NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 ...
- [C#] NPOI Excel解析
通过NPOI解析Excel,将数据保存到DataTable中. #region excel解析 public DataTable ImportExcelFile(string filePath) { ...
- NPOI Excel类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using NPOI.HSSF.Us ...
- NPOI Excel导入 导出
添加引用 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System; using System.Collections.Gene ...
- winform npoi excel 样式设置
IWorkbook excel = new HSSFWorkbook();//创建.xls文件 ISheet sheet = excel.CreateSheet("sheet1") ...
- NPOI Excel表格处理
//创建一个Excel文件 HSSFWorkbook work = new HSSFWorkbook(); //新建一个工作表 ISheet sheet1 = work.CreateSheet(&qu ...
- winform NPOI excel 导出并选择保存文件路径
public void ExcelOp(DataGridView gdv,ArrayList selHead) { if (selHead.Count==0) { MessageBox.Show(&q ...
随机推荐
- wpf button style IsMouseOver
<Style x:Key="workButtonStyle" TargetType="{x:Type Button}"> <Style.Tri ...
- java基础(10):继承、抽象类
1. 继承 1.1 继承的概念 在现实生活中,继承一般指的是子女继承父辈的财产.在程序中,继承描述的是事物之间的所属关系,通过继承可以使多种事物之间形成一种关系体系.例如公司中的研发部员工和维护部员工 ...
- xml的解析(概述)
使用java解析xml☆☆☆ 四个类:分别是针对dom和sax解析使用的类 -dom : DocumentBuilder:解析器类 -这个类是个抽象类,不能new, ...
- Django 全局log process_exception中间件
class BaseResp: # 基础的返回值类 def __init__(self, code, msg, data): self.code = code self.msg = msg self. ...
- 使用JS通过Web API执行批量操作,多个操作是一个事务!
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复235或者20161105可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...
- IAP15W4K58S4引脚定义 STC15
- TypeScript 学习笔记(一)
TypeScript: 1.是 JavaScript 的一个超集,支持 ES6 标准 2.由微软开发的自由和开源的编程语言 3.设计目标是开发大型应用,它可编译成纯 JavaScript,编译出来的 ...
- 自定义MVC二
1. 什么是MVC MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写, 它是一种软件设计典范,用一种业务逻辑.数据. ...
- template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869)
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [code/leading], template m ...
- you have new mail in /var/spool/mail/root !
今天开发的同事告诉我,他在登录系统时老是提示you have new mail in /var/spool/mail/root ! 我一猜就知道他们肯定又自己写定时任务了,这样的事已经发生过好几回了, ...