zoukankan      html  css  js  c++  java
  • C#_.NetFramework_Web项目_EXCEL数据导入

    【推荐阅读我的最新的Core版文章,是最全的介绍:C#_.NetCore_Web项目_EXCEL数据导出

    需要引用NPOI的Nuget包:NPOI-v2.4.1

    B-1:EXCEL数据导入--C#获取数据:

    /// <summary>
            /// EXCEL帮助类
            /// </summary>
            /// <typeparam name="T">泛型类</typeparam>
            /// <typeparam name="TCollection">泛型类集合</typeparam>
            public class ExcelHelp<T, TCollection> where T : new() where TCollection : List<T>, new()
            {
                //http请求Request对象
                public static HttpRequest baseRequest = HttpContext.Current.Request;
                //http请求Response对象
                public static HttpResponse baseResponse = HttpContext.Current.Response;
                /// <summary>
                /// 将数据导出EXCEL
                /// </summary>
                /// <param name="columnNameAndShowNameDic">列名+显示名</param>
                /// <param name="tColl">数据集(tColl里的类属性名必须和字典中的列名一致)</param>
                public static void ExportExcelData(Dictionary<string, string> columnNameAndShowNameDic, TCollection tColl)
                {
                    IWorkbook workbook = new HSSFWorkbook();
                    ISheet worksheet = workbook.CreateSheet("sheet1");
    
                    List<string> columnNameList = columnNameAndShowNameDic.Keys.ToList();
                    List<string> showNameList = columnNameAndShowNameDic.Values.ToList();
                    //设置首列显示
                    IRow row1 = worksheet.GetRow(0);
                    ICell cell = null;
                    for (var i = 0; i < columnNameList.Count; i++)
                    {
                        cell = row1.CreateCell(i);
                        cell.SetCellValue(columnNameList[i]);
                    }
    
                    Dictionary<int, PropertyInfo> indexPropertyDic = GetIndexPropertyDic(columnNameList);
    
                    for (int i = 0; i < tColl.Count; i++)
                    {
                        row1 = worksheet.GetRow(i + 1);
                        for (int j = 0; j < indexPropertyDic.Count; j++)
                        {
                            cell = row1.CreateCell(i);
                            cell.SetCellValue(indexPropertyDic[j].GetValue(tColl[i]).ToString());
                        }
                    }
                    
                    MemoryStream ms = new MemoryStream();
                    workbook.Write(ms);
    
                    byte[] buffer = ms.GetBuffer();
    
                    baseResponse.Clear();
                    baseResponse.Buffer = true;
                    baseResponse.ContentEncoding = System.Text.Encoding.UTF8;
                    //baseResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    baseResponse.ContentType = "application/vnd.ms-excel";
                    //设置导出文件名
                    baseResponse.AddHeader("content-disposition", "attachment;  filename=" + "MaintainReport" + ".xlsx");
                    baseResponse.AddHeader("Content-Length", buffer.Length.ToString());
    
                    baseResponse.BinaryWrite(buffer);
                    baseResponse.Flush();
                    baseResponse.End();
                }
                /// <summary>
                /// 根据属性名顺序获取对应的属性对象
                /// </summary>
                /// <param name="fieldNameList"></param>
                /// <returns></returns>
                private static Dictionary<int, PropertyInfo> GetIndexPropertyDic(List<string> fieldNameList)
                {
                    Dictionary<int, PropertyInfo> indexPropertyDic = new Dictionary<int, PropertyInfo>(fieldNameList.Count);
                    List<PropertyInfo> tPropertyInfoList = typeof(T).GetProperties().ToList();
                    PropertyInfo propertyInfo = null;
                    for (int i = 0; i < fieldNameList.Count; i++)
                    {
                        propertyInfo = tPropertyInfoList.Find(m => m.Name.Equals(fieldNameList[i], StringComparison.OrdinalIgnoreCase));
                        indexPropertyDic.Add(i, propertyInfo);
                    }
    
                    return indexPropertyDic;
                }
            }
  • 相关阅读:
    KMP算法代码实现记录
    冒泡,插入,希尔,快速,归并,桶排序,堆排序算法汇总实现
    回溯法个人理解记录(C#八皇后)
    C#创建初始化链表的方式(个人目前写出3种创建的方式)
    算法汇总代表性学习记录
    C#集合去重
    C#获取数组/字符串的k个字符的全部组合
    pl/sql简单执行记录个人学习记录
    oracle为什么尽量不要使用外键的最好理解
    PickerController 添加照片---iOS
  • 原文地址:https://www.cnblogs.com/lxhbky/p/11759660.html
Copyright © 2011-2022 走看看