zoukankan      html  css  js  c++  java
  • 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

    前后端分离,导出数据为文件或下载文件,前端如何处理后端返回的数据

  • 相关阅读:
    Runtime Type Information 运行时类型信息RTTI
    ADO实现单条记录的刷新
    TDataLink类说明
    编程实现文件关联
    咏南的连接池
    关系数据库系统PK面向对象数据库系统
    div+CSS编程技巧
    Hadoop编程笔记(一):Mapper及Reducer类详解
    如何统计博客园的个人博客访问量
    MapReduce编程模型:用MapReduce进行大数据分析
  • 原文地址:https://www.cnblogs.com/jianjialin/p/export-files-with-aspnetcore-webapi.html
Copyright © 2011-2022 走看看