You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.3 KiB
C#

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
{
/// <summary>
/// Excel服务 模块
/// </summary>
public class ExcelController : ApiController
{
private readonly IExcelService _invokeService;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="invokeService"></param>
public ExcelController(IExcelService invokeService)
{
_invokeService = invokeService;
}
/// <summary>
/// 根据用户列导出Excel数据
/// </summary>
/// <param name="req">请求数据</param>
/// <returns></returns>
[HttpPost]
[Route("ExportExcelByColumn")]
public DataResult<string> ExportExcelByColumn([FromBody] ExportByColumnReq req)
{
return _invokeService.ExportExcelByColumn(req);
}
/// <summary>
/// 根据用户列导出Excel文件流
/// </summary>
/// <param name="req">请求数据</param>
/// <returns></returns>
[HttpPost]
[Route("ExportExcelStreamByColumn")]
[ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)]
//[AllowAnonymous]
public async Task<IActionResult> 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");
}
}
}