asp.Netcore webapi生成エクスポートexcel
12548 ワード
///
///
///
///
///
[Route("export")]
[JdyAuthorize(Roles = "ordermanager")]
[HttpGet]
public IActionResult DownloadOrders([FromQuery]OrderSearchModel model)
{
//var currentUseId = User.FindFirst("sub").Value;
var currentUseId = "test";
var rootPath = _hostingEnvironment.ContentRootPath + "/orderExcels/";
if (System.IO.Directory.Exists(rootPath) == false)
System.IO.Directory.CreateDirectory(rootPath);
var newFile = rootPath + "/orderExcels" + currentUseId + ".xls";
if (System.IO.File.Exists(newFile))
{
System.IO.File.Delete(newFile);
}
var result = _orderReadService.GetOrderListAsync(
0, 1000, model.OrderNo,
model.OrderStatus, model.CustomerManagerId, model.StartTime,
model.EndTime, model.ProductId, model.ProductVariantId,
model.OrderClaimedStatus, model.TradeType, model.ChannelId, string.Empty).Result;
if (result.Items.Count() == 0)
return BadRequest();
using (var fs = new FileStream(newFile, FileMode.Create, FileAccess.Write))
{
IWorkbook workbook = new XSSFWorkbook();
var sheet = workbook.CreateSheet("orders");
var header = sheet.CreateRow(0);
header.CreateCell(0).SetCellValue(" ");
header.CreateCell(1).SetCellValue(" ");
header.CreateCell(2).SetCellValue(" ");
header.CreateCell(3).SetCellValue(" ");
header.CreateCell(4).SetCellValue(" ");
header.CreateCell(5).SetCellValue(" ");
header.CreateCell(6).SetCellValue(" ");
header.CreateCell(7).SetCellValue(" ");
header.CreateCell(8).SetCellValue(" ");
header.CreateCell(9).SetCellValue(" ");
header.CreateCell(10).SetCellValue(" ");
header.CreateCell(11).SetCellValue(" ");
var rowIndex = 1;
foreach (var item in result.Items)
{
var datarow = sheet.CreateRow(rowIndex);
datarow.CreateCell(0).SetCellValue(item.OrderCreateTime.ToString("yyyy-MM-dd HH:mm:ss"));
datarow.CreateCell(1).SetCellValue(item.ProductVariantName);
datarow.CreateCell(2).SetCellValue(item.TradeType.GetEnumDescribe());
datarow.CreateCell(3).SetCellValue(item.CustomerName);
datarow.CreateCell(4).SetCellValue(item.OrderNo);
datarow.CreateCell(5).SetCellValue(item.ChannelName);
datarow.CreateCell(6).SetCellValue(" ");
datarow.CreateCell(7).SetCellValue(item.CustomerManager);
datarow.CreateCell(8).SetCellValue(item.OrderStatus.GetEnumDescribe());
datarow.CreateCell(9).SetCellValue(" ");
datarow.CreateCell(10).SetCellValue(item.ChannelPaymentOrderTotal.ToString("#0.00"));
datarow.CreateCell(11).SetCellValue(item.ChannelActualOrderTotal.ToString("#0.00"));
rowIndex++;
}
workbook.Write(fs);
}
var memory = new MemoryStream();
using (var stream = new FileStream(newFile, FileMode.Open))
{
stream.CopyTo(memory);
}
memory.Position = 0;
return File(memory, "application/vnd.ms-excel", "order.xlsx");
}
使いましたNPOI (from nuget).
参考になりました
http://www.emanuelebartolesi.com/asp-net-core-webapi-download-upload-files/
いくつかの補足
csvのエクスポートhttps://stackoverflow.com/questions/47423563/how-can-i-return-a-csv-file-in-asp-net-core-2
前後の端は分離して、データをエクスポートしてファイルあるいはダウンロードのファイルで、フロントエンドはどのようにバックエンドの帰ってくるデータを処理します
転載先:https://www.cnblogs.com/jianjialin/p/export-files-with-aspnetcore-webapi.html