C #_. NetCore_WebAPI project _EXCEL data export (ExcelHelper_ second edition _ optimization logic)

NPOI projects need to reference the Nuget package: DotNetCore.NPOI-v1.2.2

 

Help class when this article is to use Excel to WebAPI NPOI operating items: ExcelHelper improvements optimized to do the next record:

NOTE: The following file formats used to help the class code: xlsx file, xls xlsx relative advantages and disadvantages of the code notes, recommended xlsx files saved data!

 

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace PaymentAccountAPI.Helper
{
    /// <summary>
    /// EXCEL帮助类
    /// </summary>
    /// <typeparam name="T">泛型类</typeparam>
    /// <typeparam name = "TCollection"> generic class set </ typeParam> 
    public  class ExcelHelp 
    { 
        Private ILogger Logger = null ; 

        public ExcelHelp (ILogger <ExcelHelp> Logger) 
        { 
            the this .Logger = Logger; 
        } 

        ///  <Summary> 
        / // the data export the EXCEL
         ///  </ Summary> 
        ///  <param name = "tlist"> to export data sets </ param> 
        ///  <param name = "fieldNameAndShowNameDic"> key to set (button : field name value: display name) </ param> 
        ///  <param name = "fileDirectoryPath "> File Path</ param> 
        ///  <param name = "excelName"> filename (must be in English or digital) </ param> 
        ///  <returns A> </ returns A> 
        public IWorkbook CreateOrUpdateWorkbook <T> (List <T> tlist , the Dictionary < String , String > fieldNameAndShowNameDic, IWorkbook Workbook = null , String sheetName = " sheet1 " ) the WHERE T: new new () 
        { 
            // XLS file format belongs to the old version of the file, save a sheet up to 65536 lines; and xlsx belong to the new file type;
             // Excel 07 - 2003 a worksheet can have up to 65536 rows, rows with numbers 1-65536 representation;
            There may be up to 256, row by letters AZ, AA-AZ, BA-BZ, ......, IA-IV represents; workbook contains a maximum of 255 sheets, the three sheets by default;// Excel 2007 and later versions, a sheet can be up to 1,048,576 rows, 16,384; 
            IF (Workbook == null ) 
            { 
                Workbook = new new XSSFWorkbook ();
                 // Workbook new new HSSFWorkbook = (); 
            } 
            ISheet Worksheet = Workbook. createSheet (sheetName); 

            List < String > columnNameList = fieldNameAndShowNameDic.Values.ToList ();
             // set the first column shows 
            the IRow ROW1 = worksheet.CreateRow ( 0 ); 

            ICell Cell = null ;
            ICellStyle cellHeadStyle = workbook.CreateCellStyle();
            //设置首行字体加粗
            IFont font = workbook.CreateFont();
            font.Boldweight = short.MaxValue;
            cellHeadStyle.SetFont(font);
            int cloumnCount = columnNameList.Count;
            for (var i = 0; i < cloumnCount; i++)
            {
                cell = row1.CreateCell(i);
                cell.SetCellValue(columnNameList[i]);
                cell.CellStyle = cellHeadStyle;
            }

            //根据反射创建其他行数据
            var raws = tList.Count;
            Dictionary<string, PropertyInfo> titlePropertyDic = this.GetIndexPropertyDic<T>(fieldNameAndShowNameDic);

            PropertyInfo propertyInfo = null;
            T t = default(T);
            for (int i = 0; i < raws; i++)
            {
                if (i % 10000 == 0)
                {
                    this.Logger.LogInformation($"Excel已创建{i + 1}条数据");
                }
                row1 = worksheet.CreateRow(i + 1);
                t = tList[i];

                int cellIndex = 0;
                foreach (var titlePropertyItem in titlePropertyDic)
                {
                    propertyInfo = titlePropertyItem.Value;
                    cell = row1.CreateCell(cellIndex);

                    if (propertyInfo.PropertyType == typeof(int)
                        || propertyInfo.PropertyType == typeof(decimal)
                        || propertyInfo.PropertyType == typeof(double))
                    {
                        cell.SetCellValue(Convert.ToDouble(propertyInfo.GetValue(t) ?? 0));
                    }
                    else if (propertyInfo.PropertyType == typeof(DateTime))
                    {
                        cell.SetCellValue(Convert.ToDateTime(propertyInfo.GetValue(t)?.ToString()).ToString("yyyy-MM-dd HH:mm:ss" )); 
                    } 
                    The else  IF (propertyInfo.PropertyType == typeof ( BOOL )) 
                    { 
                        cell.SetCellValue (Convert.ToBoolean (propertyInfo.GetValue (T) .ToString ())); 
                    } 
                    the else 
                    { 
                        cell.SetCellValue (propertyInfo.GetValue (t) .ToString () ??? "" ); 
                    } 
                    the cellIndex ++ ; 
                } 

                // important: when you set the line width adaptation (adding large quantities of data that need the comment line, otherwise it will greatly slow down the Excel add rows speed!)
                 //worksheet.AutoSizeColumn (I, to true); 
            } 

            return Workbook; 
        } 

        ///  <Summary> 
        /// Save Workbook data file
         ///  </ Summary> 
        ///  <param name = "Workbook"> </ param> 
        ///  <param name = "fileDirectoryPath"> </ param> 
        ///  <param name = "fileName"> </ param> 
        public  void SaveWorkbookToFile (IWorkbook Workbook, String fileDirectoryPath, String fileName) 
        { 
            // XLS file format belongs the old version of the file, save a sheet up to 65536 lines; and xlsx belong to the new version of file types;
             //Excel 07 - 2003 a worksheet can have up to 65536 lines, digital lines represent 1-65536; can have up to 256 columns with letters AZ, AA-AZ, BA- BZ, ......, IA-IV represents ; workbook contains a maximum of 255 sheets, the three sheets by default;
             // Excel 2007 and later versions, a sheet can be up to 1,048,576 rows, 16384; 

            the MemoryStream MS = new new the MemoryStream ();
             / / this code is very important, if not, will be reported: format inconsistent with the EXCEL open extension format specified 
            ms.Seek ( 0 , SeekOrigin.Begin); 
            workbook.Write (MS); 
            byte [] = myByteArray MS. the GetBuffer (); 

            fileDirectoryPath = fileDirectoryPath.TrimEnd ( ' \\ ' ) + " \\ " ;
             IF (!Directory.Exists(fileDirectoryPath))
            {
                Directory.CreateDirectory(fileDirectoryPath);
            }

            string filePath = fileDirectoryPath + fileName;
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            File.WriteAllBytes(filePath, myByteArray);
        }

        /// <summary>
        /// 保存Workbook数据为下载文件
        /// </summary>
        public FileContentResult SaveWorkbookToDownloadFile(IWorkbook workbook)
        {
            MemoryStream ms = newMemoryStream ();
             // This code is very important, if not, will be reported: Inconsistent format with the extension of the open EXCEL format specified 
            ms.Seek ( 0 , SeekOrigin.Begin); 
            workbook.Write (MS); 
            byte [ ] = myByteArray ms.GetBuffer (); 

            // for .xls files
             // file application / vnd.ms-Excel
             // for .xlsx files.
            // file application / vnd.openxmlformats-officedocument.spreadsheetml.sheet 
            MediaTypeHeaderValue mediaType = new new MediaTypeHeaderValue ( " file application / vnd.openxmlformats-officedocument.spreadsheetml.sheet " ); 
            mediaType.Encoding = System.Text.Encoding.UTF8;

            return new FileContentResult(myByteArray, mediaType.ToString());
        }


        /// <summary>
        /// 读取Excel数据
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fieldNameAndShowNameDic"></param>
        /// <returns></returns>
        public List<T> ReadDataList<T>(string filePath, Dictionary<string, string> fieldNameAndShowNameDic) where T : new()
        {
            List<T> tList = null;
            T T = default (T); 

            // title attribute dictionary listing 
            the Dictionary < String , PropertyInfo> = titlePropertyDic the this .GetIndexPropertyDic <T> (fieldNameAndShowNameDic);
             // heading subscript listing 
            the Dictionary < String , int > = titleIndexDic new new the Dictionary < String , int > ( 0 ); 

            PropertyInfo PropertyInfo = null ; 

            the using (= the fileStream fileStream new new  FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                IWorkbook xssfWorkbook = new XSSFWorkbook(fileStream);
                var sheet = xssfWorkbook.GetSheetAt(0);

                var rows = sheet.GetRowEnumerator();
                tList = new List<T>(sheet.LastRowNum + 1);
                //第一行数据为标题,
                if (rows.MoveNext())
                {
                    IRow row = (XSSFRow)rows.Current;
                    ICell cell = null;
                    string cellValue = null;
                    for (int i = 0; i < row.Cells.Count; i++)
                    {
                        cell = row.Cells[i];
                        cellValue = cell.StringCellValue;
                        if (titlePropertyDic.ContainsKey(cellValue))
                        {
                            titleIndexDic.Add(cellValue, i);
                        }
                    }
                }
                //从第2行数据开始获取
                while (rows.MoveNext())
                {
                    IRow row = (XSSFRow)rows.Current;
                    t = new T();

                    foreach (var titleIndexItem in titleIndexDic)
                    {
                        var cell = row.GetCell(titleIndexItem.Value);
                        if (cell != null)
                        {
                            propertyInfo = titlePropertyDic[titleIndexItem.Key];
                            if (propertyInfo.PropertyType == typeof(int))
                            {
                                propertyInfo.SetValue(t, Convert.ToInt32(cell.NumericCellValue));
                            }
                            else if (propertyInfo.PropertyType == typeof(decimal))
                            {
                                propertyInfo.SetValue(t, Convert.ToDecimal(cell.NumericCellValue));
                            }
                            else if (propertyInfo.PropertyType == typeof(double))
                            {
                                propertyInfo.SetValue(t, Convert.ToDouble(cell.NumericCellValue));
                            }
                            else if (propertyInfo.PropertyType == typeof(bool))
                            {
                                propertyInfo.SetValue(t, Convert.ToBoolean(cell.StringCellValue));
                            }
                            else if (propertyInfo.PropertyType == typeof(DateTime))
                            {
                                propertyInfo.SetValue(t, Convert.ToDateTime(cell.StringCellValue));
                            }
                            else
                            {
                                propertyInfo.SetValue(t, cell.StringCellValue);
                            }

                        } 
                    } 
                    TList.Add (T); 
                } 
            } 
            return tlist ?? new new List <T> ( 0 ); 
        } 

        ///  <Summary> 
        /// Gets an object according to the attribute corresponding to the attribute name order
         ///  </ Summary> 
        ///  <param name = "FieldNameList"> </ param> 
        ///  <Returns> </ Returns> 
        Private the Dictionary < String , PropertyInfo> GetIndexPropertyDic <T> (the Dictionary < String , String > fieldNameAndShowNameDic)
        {
            Dictionary<string, PropertyInfo> titlePropertyDic = new Dictionary<string, PropertyInfo>(fieldNameAndShowNameDic.Count);

            List<PropertyInfo> tPropertyInfoList = typeof(T).GetProperties().ToList();
            PropertyInfo propertyInfo = null;

            foreach (var item in fieldNameAndShowNameDic)
            {
                propertyInfo = tPropertyInfoList.Find(m => m.Name.Equals(item.Key, StringComparison.OrdinalIgnoreCase));
                titlePropertyDic.Add(item.Value, propertyInfo);
            }
            return titlePropertyDic;
        }

    }
}

 

Guess you like

Origin www.cnblogs.com/lxhbky/p/12063545.html