C#_.NetFramework_Web项目_EXCEL数据导入 C#_.NetCore_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对象
            static HttpRequest baseRequest = HttpContext.Current.Request;
            http请求Response对象
            static HttpResponse baseResponse = HttpContext.Current.Response;
            <summary>
             将数据导出EXCEL
            </summary>
            <param name="columnNameAndShowNameDic">列名+显示</param>
            <param name="tColl">数据集(tColl里的类属性名必须和字典中的列名一致)</param>
            static void ExportExcelData(Dictionary<string,1)">string> columnNameAndShowNameDic,TCollection tColl)
            {
                IWorkbook workbook =  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);

                int i = 0; i < tColl.Count; i++)
                {
                    row1 = worksheet.GetRow(i + 1);
                    int j = 0; j < indexPropertyDic.Count; j++)
                    {
                        cell = row1.CreateCell(i);
                        cell.SetCellValue(indexPropertyDic[j].GetValue(tColl[i]).ToString());
                    }
                }
                
                MemoryStream ms =  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();
            }
             根据属性名顺序获取对应的属性对象
            <param name="fieldNameList"></param>
            <returns></returns>
            private static Dictionary< fieldNameList)
            {
                Dictionary<new Dictionary<(fieldNameList.Count);
                List<PropertyInfo> tPropertyInfoList = typeof(T).GetProperties().ToList();
                PropertyInfo propertyInfo = 0; i < fieldNameList.Count; i++)
                {
                    propertyInfo = tPropertyInfoList.Find(m => m.Name.Equals(fieldNameList[i],StringComparison.OrdinalIgnoreCase));
                    indexPropertyDic.Add(i,propertyInfo);
                }

                return indexPropertyDic;
            }
        }

 

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....