Beispiel #1
0
        public IActionResult Import(IFormFile excelfile)
        {
            string   sWebRootFolder = _webHostEnvironment.WebRootPath;
            string   sFileName      = $"{Guid.NewGuid()}.xlsx";
            FileInfo file           = new FileInfo(Path.Combine(sWebRootFolder, sFileName));

            try
            {
                using (FileStream fileStream = new FileStream(file.ToString(), FileMode.Create))
                {
                    excelfile.CopyTo(fileStream);
                    fileStream.Flush();
                }
                Stream fs = excelfile.OpenReadStream();
                //此处泛型为需要隐射的实体类
                Import2Excel <FundModel> it = new Import2Excel <FundModel>();
                //这里自定义将excel的表头或列名字映射到对应实体
                it.ForMember("日期", e => e.CurrentDateTime);
                it.ForMember("上证指数", e => e.ShanghaiCompositeIndex);
                it.ForMember("平安银行(000001)", e => e.PinganBank);
                it.ForMember("贵州茅台(600519)", e => e.MoutaiChina);
                it.ForMember("中信建投(601066)", e => e.CiciCitic);
                it.ForMember("华兴源创(688001)", e => e.SuzhouHYCTechnology);
                it.ForMember("同达创业(600647)", e => e.ShanghaiTongdaVentureCapitalCo);
                //通过此方法直接将流文件加载到内存
                IList <FundModel> list = it.LoadFromExcel(fs);
                _context.AddRangeAsync(list);
                var count = _context.SaveChangesAsync();
                return(Content("上传成功,请返回到上一页进行相对收益计算!"));
            }
            catch (Exception ex)
            {
                return(Content(ex.Message));
            }
        }