.Net Core 使用MiNiExcel导入导出 vue端下载

一,下载相对应的NuGet包:

  MiNiExcel

 

二,直接用

导入 

public IActionResult UploadExcel(IFormFile excel)
        {
            //var excel = Request.Form.Files[0];

            var stream = new MemoryStream();
            excel.CopyTo(stream);
            //获取excel中的内容
            var list = stream.Query<自定义>().ToList();
       //至此 就可以对list为所欲为
return Ok(list); }

导出

public IActionResult DownloadExcel()
        {
            //此values可以是任何你要导出的数据
            var values = new[] {
                new { Column1 = "MiniExcel", Column2 = 1 },
                new { Column1 = "Github", Column2 = 2}
            };

            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(values);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                //文件名
                FileDownloadName = "demo.xlsx"
            };
        }

 

 三,导出后下载

//返回流时方式
this.$http
        .post(`url`, { responseType: "blob" })
        .then((u) => {
          const blob = new Blob([u.data]);
          const fileName = "dddd.xlsx"; //下载文件名称
          const elink = document.createElement("a");
          elink.download = fileName;
          elink.style.display = "none";
          elink.href = URL.createObjectURL(blob);
          document.body.appendChild(elink);
          elink.click();
          URL.revokeObjectURL(elink.href); // 释放URL 对象
        });

 

 

 

注:操作简单 使用便捷 真是66666

posted @ 2021-11-18 16:31  董某的智慧所在  阅读(655)  评论(1)    收藏  举报