码迷,mamicode.com
首页 > Windows程序 > 详细

asp.net core webapi 生成导出excel

时间:2018-08-28 18:52:22      阅读:1850      评论:0      收藏:0      [点我收藏+]

标签:otn   tac   tom   work   ted   route   payment   返回   http   


/// <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

标签:otn   tac   tom   work   ted   route   payment   返回   http   

原文地址:https://www.cnblogs.com/jianjialin/p/export-files-with-aspnetcore-webapi.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!