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

  • 相关阅读:
    83. Remove Duplicates from Sorted List
    35. Search Insert Position
    96. Unique Binary Search Trees
    94. Binary Tree Inorder Traversal
    117. Populating Next Right Pointers in Each Node II
    116. Populating Next Right Pointers in Each Node
    111. Minimum Depth of Binary Tree
    169. Majority Element
    171. Excel Sheet Column Number
    190. Reverse Bits
  • 原文地址:https://www.cnblogs.com/Fred1987/p/10423128.html
Copyright © 2011-2022 走看看