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 ...
随机推荐
- ASP.NET Core框架深度学习(三) Server对象
8.Server 第五个对象 服务器在管道中的职责非常明确,当我们启动应用宿主的WebHost的时候,服务它被自动启动.启动后的服务器会绑定到指定的端口进行请求监听,一旦有请求抵达,服务器会根据该 ...
- PHP-Curl模拟HTTPS请求
使用PHP-Curl方式模拟HTTPS请求,测试接口传参和返回值状态 上代码!! <?php /** * 模拟post进行url请求 * @param string $url * @par ...
- java核心技术第二篇之数据库SQL语法
#查询products表记录SELECT * FROM products WHERE price > 2000;-- 单行注释/* 多行注释*/#创建数据库CREATE DATABASE hei ...
- 使用element-ui的el-menu导航选中后刷新页面保持当前选中
<el-menu :default-active=‘$route.path‘ :router=‘true‘ :unique-opened=‘true‘ :default-openeds=&quo ...
- 程序员编程时常用的mac快捷方式
fn + F2/F3 = 调节音量 commend + shift +k = 显示或隐藏键盘 commend+shift +h = iPhone返回主页面 commend+ shift + hh = ...
- Android框架Volley之:ImageRequest请求实现图片加载
首先我们在项目中导入这个框架: implementation 'com.mcxiaoke.volley:library:1.0.19' 在AndroidManifest文件当中添加网络权限: < ...
- [20190524]使用use_concat or_expand提示优化.txt
[20190524]使用use_concat or_expand提示优化.txt --//上午看了链接https://connor-mcdonald.com/2019/05/22/being-gene ...
- [Go] 分页计算页码的主要逻辑
当使用imap进行读取邮件体的时候,有个函数可以根据传入的开始和结束索引来一次读取多条邮件内容主要逻辑类似这样,从1开始, 1,10 11,20 21,30 或者 1,31 32,63 ...
- 爬虫---lxml爬取博客文章
上一篇大概写了下lxml的用法,今天我们通过案例来实践,爬取我的博客博客并保存在本地 爬取博客园博客 爬取思路: 1.首先找到需要爬取的博客园地址 2.解析博客园地址 # coding:utf-8 i ...
- 爬虫---Beautiful Soup 初始
我们在工作中,都会听说过爬虫,那么什么是爬虫呢? 什么是网络爬虫 爬虫基本原理 所谓网络爬虫就是一个自动化数据采集工具,你只要告诉它要采集哪些数据,丢给它一个 URL,就能自动地抓取数据了.其背后的基 ...