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

  • 相关阅读:
    EEPlat 的 后台业务处理模型
    poj 1012 Joseph (约瑟夫问题)
    python使用正則表達式
    二维码_encode与decode
    UITableView显示不全
    Bottle 中文文档
    不相交集python实现
    面试题1:落单的数
    Android开发/源代码资源汇总
    leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
  • 原文地址:https://www.cnblogs.com/Fred1987/p/10423128.html
Copyright © 2011-2022 走看看