首页 > 其他分享 >miniExcel导入导出

miniExcel导入导出

时间:2023-03-02 10:33:47浏览次数:50  
标签:Code stream miniExcel 导出 apiResult list 导入 内存 var

miniExcel的配置使用

具体可查看官网:https://gitee.com/dotnetchina/MiniExcel?_from=gitee_search#todo

第一步:Neget下载 MiniExcel

上传

///


/// 上传Excel
///

///

  [HttpPost]
        public ActionResult UpLoadExcel(IFormFile file)
        {
            //IFormFile form
        var formFile = HttpContext.Request.Form.Files[0];

        ApiResult apiResult = new ApiResult();
        try
        {
            //先保存到服务器上
            //读取服务器上的路径进行转成
            //FileStream 文件流
            //在你的服务器上  存储到一个文件里
            //var temp = Path.GetTempPath()+DateTime.Now.ToString("yyyyMMddhhmmss")+Path.GetExtension(formFile.FileName);

            //using (var file = System.IO.File.Create(temp))
            //{
            //    formFile.CopyTo(file);
            //}

            //var a = MiniExcel.Query<Domain.Entiry.User>(temp);


            //准备一个内存流(在内存中开辟一个空间)
            var stream = new MemoryStream();
            //将上传的excel  放到 刚刚  开辟的内存当中了
            formFile.CopyTo(stream);
            var list = stream.Query<Domain.Entiry.User>();
            if (list.Count() == 0) {
                apiResult.Code = 520;
                apiResult.Msg = "Excel当中无上传数据";
                return Ok(apiResult);
            }

            foreach (var item in list)
            {
                //将导入的excel 密码部分  手动 改更成md5部分
                item.UPass = Md5Helper.GetMD5Hash(item.UPass);
            }

            //添加之前需要做去重操作
            var dbList = _userRepostiory.GetUsers().Select(u => u.UName).ToList();
            //如何判断 导入的数据集合和 数据库的数据集合重复呢?
            
            //获取我们的数据交集
            var newList =  dbList.Intersect(list.Select(u => u.UName).ToList());
            if (newList.Count()>0)
            {
                apiResult.Code = 599;
                apiResult.Data = newList.ToList();
                apiResult.Msg = "当前导入数据在数据库中已存在";
                return Ok(apiResult);
            }


            if (_userRepostiory.AddBatch(list))
            {
                apiResult.Code = 200;

            }
            else
            {
                apiResult.Code = 500;
            }
            return Ok(apiResult);
        }
        catch (Exception ex)
        {
            apiResult.Code = 500;
            apiResult.Msg = ex.Message; 
            return Ok(apiResult);
        }
    }
下载

///


/// 下载
///

///

   [HttpGet]
        public ActionResult DownLoadExcel()
        {
            //将导出的Excel数据 读取出来
            var list = _userRepostiory.GetUsers();
            var resutl = _mapper.Map<List<LoginResponseDto>>(list);
        //创建内存流
        var stream = new MemoryStream();
        //将list 集合 的数据 保存到内存流里
        stream.SaveAs(resutl);
        //从哪个位置开始
        stream.Seek(0, SeekOrigin.Begin);
        //将内存流转成  文件流
        return new FileStreamResult(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
        {
            FileDownloadName = "用户信息表"+ DateTime.Now.ToString("yyyyMMdd") + ".xlsx"
        };

    }


}

标签:Code,stream,miniExcel,导出,apiResult,list,导入,内存,var
From: https://www.cnblogs.com/Zhuo-Hu/p/17170968.html

相关文章