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 ...
随机推荐
- Java日期时间API系列4-----Jdk7及以前的日期时间类的线程安全问题
1.Date类为可变的,在多线程并发环境中会有线程安全问题. (1)可以使用锁来处理并发问题. (2)使用JDK8 Instant 或 LocalDateTime替代. 2.Calendar的子类为 ...
- DevExpress的TextEdit控件没法调整高度解决
场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...
- [b0020] python 归纳 (六)_模块变量作用域
test_module2.py: # -*- coding: utf-8 -*-"""测试 模块变量的作用域 总结:1 其他模块的变量,在当前模块的任何地方,包括函数都可 ...
- Falling back to java on path. This behavior is deprecated
windows启动elasticsearch报错:warning: Falling back to java on path. This behavior is deprecated. Specify ...
- [Go] gocron源码阅读-go语言中数组和切片的字面值初始化语法
源码中有这么一句,这个函数要求返回的是[]cli.Command,cli.Command类型的切片,这个地方直接使用字面值初始化了一个切片返回去了 return []cli.Command{comma ...
- [日常] git版本回退
还没有push到远程的时候,版本回退的测试如下 先克隆一个空的测试仓库,这是我自己在gitlab里创建的空仓库git clone http://192.168.1.114:8090/admintsh/ ...
- CentOS7设置开机自启动方式
方式一: # 在/etc/rc.d/rc.local文件中追加启动命令,该文件追加后,会随着机器自动后,自动运行文件中的命令 # vim /etc/rc.d/rc.local # 权限问题:在cent ...
- FPDF_CloseDocument(doc);
FPDF_CloseDocument(doc); 创建不加载就会段错误 必须创建 doc = FPDF_CreateNewDocument();
- gyp --depth . -D component=shared_library -Ibuild/standalone.gypi build/all.gyp
gyp --depth . -D component=shared_library -Ibuild/standalone.gypi build/all.gyp
- ionic4之ion-sliders
<ion-content class="details_"> <img class="courimg_" src="assets/i ...