.NET Core 基于Npoi.Mapper导出excel Vue 基于axios下载
.NET Core 基于Npoi.Mapper导出excel Vue 基于axios下载
后端
准备项:negut安装Npoi.Mapper

实体配置要导出的表头

代码:
/// <summary>
/// 下载excel
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<ActionResult> DownLoadFileAsync([FromQuery] PriceToolsByWishParam priceToolsByShopeeParam)
{
var data = await GetWishFreightAsync(priceToolsByShopeeParam);
if (data == null)
return BadRequest();
var mapper = new Mapper();
using var stream = new MemoryStream();
mapper.Save(stream, data, "sheet1", true, true);
return File(stream.ToArray(), "application/vnd.ms-excel",
priceToolsByShopeeParam.Name + DateTime.Now.ToString("yyyy-MM-ddHH:mm:ss") + ".xlsx");
}
前端:
Axios配置返回类型
export const getDownWishPrice = params => {
return axios({
method: 'get',
url: `${base}/api/PriceToolsByWish/DownLoadFile`,
params: params,
responseType: 'blob'
})
};
调用:
getDownWishPrice(para).then((res) => {
console.log(res);
if (!res) {
return;
}
let blob = new Blob([res.data], {
type: "application/vnd.ms-excel;charset=utf-8",
});
var date =
new Date().getFullYear() +
"" +
(new Date().getMonth() + 1) +
"" +
new Date().getDate() +
"" +
new Date().getHours() +
"" +
new Date().getMinutes() +
"" +
new Date().getSeconds() +
"" +
new Date().getMilliseconds();
const fileName = this.searchForm.Name + ".xlsx";
let url = window.URL.createObjectURL(blob);
let link = document.createElement("a");
link.style.display = "none";
link.download = fileName;
link.href = url;
document.body.appendChild(link);
link.click();
});
});
效果:


本文来自博客园,作者:白将,转载请注明原文链接:https://www.cnblogs.com/dony-dong/p/15021421.html

浙公网安备 33010602011771号