using DS.Module.Core; using DS.Module.ExcelModule; using DS.Module.ExcelModule.Model; using DS.WMS.Core.Code.Interface; using Masuit.Tools.Files; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using NPOI.HPSF; using NPOI.XSSF.UserModel; using SharpCompress.Common; using System.IO; using System.Text; using DS.Module.Core.Data; using DS.WMS.Core.Code.Dtos; using DS.WMS.Core.Op.Dtos; using DS.WMS.Core.Op.Entity; using DS.WMS.Core.Op.Interface; using DS.WMS.Core.Sys.Dtos; using DS.WMS.Core.Sys.Interface; using Microsoft.AspNetCore.Authorization; using SixLabors.ImageSharp.Drawing; using System.IO; using System.Web; namespace DS.WMS.MainApi.Controllers { /// /// Excel服务 模块 /// public class ExcelController : ApiController { private readonly IExcelService _invokeService; /// /// 构造函数 /// /// public ExcelController(IExcelService invokeService) { _invokeService = invokeService; } /// /// 根据用户列导出Excel数据 /// /// 请求数据 /// [HttpPost] [Route("ExportExcelByColumn")] public DataResult ExportExcelByColumn([FromBody] ExportByColumnReq req) { return _invokeService.ExportExcelByColumn(req); } /// /// 根据用户列导出Excel文件流 /// /// 请求数据 /// [HttpPost] [Route("ExportExcelStreamByColumn")] [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] //[AllowAnonymous] public async Task ExportExcelStreamByColumn([FromBody] ExportByColumnReq req)// { var result = _invokeService.ExportExcelStreamByColumn(req); //HttpContext.Response.Headers.Add("Content-Length", result.Length.ToString()); //HttpContext.Response.Headers.Add("Content-Type", "application/octet-stream;charset=UTF-8"); var dtstr = DateTime.Now.ToString("yyyyMMddHHmmssfff"); var filename = $"{req.Title}-{dtstr}.xlsx"; byte[] bytes = new byte[result.Length]; result.ReadExactly(bytes, 0, bytes.Length); string mimeType = "application/octet-stream"; var _r = new FileContentResult(bytes, mimeType) { FileDownloadName = filename }; return _r; //var path = Path.Combine("", "wwwroot/export/" + filename); //using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write)) //{ // result.WriteTo(fileStream); //} //byte[] byteArr = System.IO.File.ReadAllBytes(path); //string mimeType = "application/octet-stream"; //return new FileContentResult(byteArr, mimeType) //{ // FileDownloadName = filename //}; //return File(result, "application/octet-stream;charset=UTF-8",filename); ////return new FileStreamResult(result, "application/octet-stream;charset=UTF-8") ////{ //// FileDownloadName = filename ////}; //return File(result, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", filename); //var bytes = new byte[result.Length]; //result.Read(bytes, 0, bytes.Length); //result.Close(); //return new FileContentResult(bytes, "application/octet-stream"); //return new FileStreamResult(result, "application/octet-stream") { FileDownloadName = $"{req.Title}-{dtstr}.xlsx" }; //var provider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider(); //var memi = provider.Mappings[".xlsx"]; //var fileName = Path.GetFileName(filename); //return File(result, memi, fileName); //var result = _invokeService.OutputExcel(req); //return File(result, "application/octet-stream;charset=UTF-8", "UploadTemplate.xlsx"); } } }