asp.net core webapi 生成导出excel
/// <summary>
/// 下载订单
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[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");
}
用到了 dotnetcore.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
前后端分离,导出数据为文件或下载文件,前端如何处理后端返回的数据
asp.net core webapi 生成导出excel的更多相关文章
- 第二十节:Asp.Net Core WebApi生成在线文档
一. 基本概念 1.背景 使用 Web API 时,了解其各种方法对开发人员来说可能是一项挑战. Swagger 也称为OpenAPI,解决了为 Web API 生成有用文档和帮助页的问题. 它具有诸 ...
- Asp.net core WebApi 使用Swagger生成帮助页
最近我们团队一直进行.net core的转型,web开发向着前后端分离的技术架构演进,我们后台主要是采用了asp.net core webapi来进行开发,开始每次调试以及与前端人员的沟通上都存在这效 ...
- ASP.NET Core WebApi使用Swagger生成api说明文档看这篇就够了
引言 在使用asp.net core 进行api开发完成后,书写api说明文档对于程序员来说想必是件很痛苦的事情吧,但文档又必须写,而且文档的格式如果没有具体要求的话,最终完成的文档则完全取决于开发者 ...
- ASP.NET Core WebApi使用Swagger生成api
引言 在使用asp.net core 进行api开发完成后,书写api说明文档对于程序员来说想必是件很痛苦的事情吧,但文档又必须写,而且文档的格式如果没有具体要求的话,最终完成的文档则完全取决于开发者 ...
- ASP.NET Core WebApi使用Swagger生成api说明文档
1. Swagger是什么? Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件 ...
- Asp.net core WebApi 使用Swagger生成帮助页实例
最近我们团队一直进行.net core的转型,web开发向着前后端分离的技术架构演进,我们后台主要是采用了asp.net core webapi来进行开发,开始每次调试以及与前端人员的沟通上都存在这效 ...
- asp.net core webapi 使用ef 对mysql进行增删改查,并生成Docker镜像构建容器运行
1.构建运行mysql容器,添加数据库user 参考Docker创建运行多个mysql容器,地址 http://www.cnblogs.com/heyangyi/p/9288402.html 添加us ...
- 【转】ASP.NET Core WebApi使用Swagger生成api说明文档看这篇就够了
原文链接:https://www.cnblogs.com/yilezhu/p/9241261.html 引言 在使用asp.net core 进行api开发完成后,书写api说明文档对于程序员来说想必 ...
- ASP.NET Core WebApi使用Swagger生成API说明文档【xml注释版】
⒈新建ASP.NET Core WebAPi项目 ⒉添加 NuGet 包 Install-Package Swashbuckle.AspNetCore ⒊Startup中配置 using System ...
随机推荐
- 端口转发 Port Forwarding (一)
0x00First 最近发现一些好用的端口转发工具和技巧,计划认真梳理一下 SSH.NC.LCX.EW.FRP 0x01 SSH隧道端口转发 目前利用SSH隧道(SSH tunneling)进行端口转 ...
- 初识Dubbo+Zookeeprt搭建SOA项目
由于工作中天天和Dubbo打交道,天天写对外服务,所以有必要自己动手搭建一个Dubbo+zookeeper项目来更更深层次的认识Dubbo 首先了解一下SOA: 英文名称(Service Orient ...
- Tmux会话的使用
不想看废话的直接拖到下面看干货部分! 我们管理Linux服务器通常是通过ssh远程连接过去,如果在服务器上执行比较耗时的操作,比如下载安装软件.编译等等,如果需要数个小时来完成这些工作,但是又不得不关 ...
- SpringBoot 通过 Exploded Archives 的方式部署
之前部署 SpringBoot 一直是用可执行 jar 的方式. java -jar codergroup-1.0.0.jar 就可以启动项目,为了能在后台运行,通常我们会使用这行命令 nohup j ...
- Eclipse常用20个快捷键
Eclipse常用20个快捷键 1. Ctrl+F : 本文查找Find与替换Replace 2. Ctrl+H : 全局搜索,可按照文件类型搜索 3. Ctrl+1 : 快速修复,能快速的显示光标所 ...
- 12、json、GridView、缓存
1.解析json数据: public class PhotosData { public int retcode; public PhotosInfo data; public class Photo ...
- 跟繁琐的命令行说拜拜!Gerapy分布式爬虫管理框架来袭!
背景 用 Python 做过爬虫的小伙伴可能接触过 Scrapy,GitHub:https://github.com/scrapy/scrapy.Scrapy 的确是一个非常强大的爬虫框架,爬取效率高 ...
- Go语言数组
目录 声明数组 数组在内存的结构 初始化数组 使用数组 函数间传递数组 数组使用注意事项 数组是具有相同唯一类型的一组已编号且长度固定的数据项序列(这是一种同构的数据结构);这种类型可以是任意的原始类 ...
- Ubuntu 16.04下GDB调试
在linux中还有一个更受大家欢迎的调试工具:GDB.GDB是一个由GNU开源组织发布的.UNIX/LINUX操作系统下的.基于命令行的.功能强大的程序调试工具.可以用来调试C,C++程序. GDB功 ...
- ubuntu默认的Python版本号修改
ubuntu默认的Python是2.7版本的,键入Python3 就可以使用Python3.X版本.下面修改默认的Python版本, 1.删除/usr/bin 下的Python链接 sudo rm / ...