zoukankan      html  css  js  c++  java
  • 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);
    }
    }
    }
    }

  • 相关阅读:
    国王游戏
    从2014到2015,还有什么?
    【转载】别把自己推到了墙角
    IE9+浏览器input文本框/密码框后面的小叉子/小眼睛清除
    ajax开发模拟后端数据接口
    谈谈JavaScript事件
    也说border-box盒模型
    极其简单的使用基于gulp和sass前端工作流
    如何使用javascript书写递归函数
    Git基本命令和GitFlow工作流
  • 原文地址:https://www.cnblogs.com/Fred1987/p/10423128.html
Copyright © 2011-2022 走看看