• 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);
    }
    }
    }
    }

  • 相关阅读:
    普通的一个python脚本,hadoop进军的准备
    Python之数据类型讲解
    开始博客的理由
    【微机原理及应用】程序的分类
    【jvm】jvm学习第二篇。jvm运行机制
    【jvm】jvm学习第一篇。初识jvm
    【it公司】it公司简介-项目流程-研发小技巧
    【感悟】20岁的人生不应该停止奋斗。----------------努力努力再努力
    【书籍学习】史上最全的Java进阶书籍推荐
    【职业规划】3年工作经验的程序员应该具备的技能
  • 原文地址:https://www.cnblogs.com/Fred1987/p/10423128.html
Copyright © 2020-2023  润新知