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.

197 lines
6.8 KiB
C#

using DS.Module.Core;
using DS.Module.ExcelModule;
using DS.Module.ExcelModule.Model;
using DS.WMS.Core.Code.Interface;
2 months ago
using Masuit.Tools.Files;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2 months ago
using Newtonsoft.Json;
using NPOI.HPSF;
2 months ago
using NPOI.XSSF.UserModel;
using SharpCompress.Common;
using System.IO;
using System.Text;
2 months ago
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;
using DS.WMS.Core.Flow.Dtos;
using DS.WMS.Core.Flow.Entity;
using DS.WMS.Core;
using MathNet.Numerics;
using NPOI.SS.Formula.Functions;
using DS.Module.UserModule;
using SqlSugar;
using Newtonsoft.Json.Linq;
using DS.Module.Core.Extensions;
using LanguageExt.Common;
using DS.WMS.Core.TaskPlat.Dtos;
namespace DS.WMS.MainApi.Controllers
{
/// <summary>
/// Excel服务 模块
/// </summary>
public class ExcelController : ApiController
{
private readonly IServiceProvider _serviceProvider;
private readonly IExcelService _invokeService;
private readonly IUser user;
static readonly ApiFox api;
private readonly ISqlSugarClient db;
/// <summary>
/// 构造函数
/// </summary>
static ExcelController()
{
api = new ApiFox();
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="invokeService"></param>
public ExcelController(IExcelService invokeService, IServiceProvider serviceProvider)
{
_invokeService = invokeService;
_serviceProvider = serviceProvider;
user = _serviceProvider.GetRequiredService<IUser>();
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
}
/// <summary>
/// 根据用户列导出Excel数据
/// </summary>
/// <param name="req">请求数据</param>
/// <returns></returns>
[HttpPost]
[Route("ExportExcelByColumn")]
public DataResult<string> ExportExcelByColumn([FromBody] ExportByColumnReq req)
{
return _invokeService.ExportExcelByColumn(req);
}
8 months ago
/// <summary>
/// 通用Excel导出
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
[HttpPost]
[Route("CommonExcelExport")]
[ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)]
public async Task<IActionResult> CommonExcelExportAsync([FromBody] ExportExcelReq req)
{
if (api.DefaultHeaders.Contains("Authorization"))
api.DefaultHeaders.Remove("Authorization");
api.DefaultHeaders.Add("Authorization", "Bearer " + user.GetToken());
var result = await api.PostAsync<DataResult>(req.Url, req.QueryRequest);
if (!result.Succeeded)
{
await new ApplicationException($"调用URL{req.Url} 时返回了错误:" + result.Message).LogAsync(db);
}
var data = req.Url.Contains("GetSeaExportList") ?JsonConvert.SerializeObject(result.Data.ObjToDynamic()?.Data.list) : JsonConvert.SerializeObject(result.Data.Data);
var res1 = await _invokeService.ExportExcelStreamByColumnAsync(new ExportByColumnReq()
{
JsonDataStr = data,
ColumnSets = req.ColumnSets,
});
var dtstr = DateTime.Now.ToString("yyyyMMddHHmmssfff");
var filename = $"{dtstr}.xlsx";
byte[] bytes = new byte[res1.Length];
res1.ReadExactly(bytes, 0, bytes.Length);
string mimeType = "application/octet-stream";
var _r = new FileContentResult(bytes, mimeType)
{
FileDownloadName = filename
};
return _r;
}
//}
8 months ago
/// <summary>
/// 根据用户列导出Excel文件流
/// </summary>
/// <param name="req">请求数据</param>
/// <returns></returns>
[HttpPost]
[Route("ExportExcelStreamByColumn")]
2 months ago
[ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)]
//[AllowAnonymous]
public async Task<IActionResult> ExportExcelStreamByColumn([FromBody] ExportByColumnReq req)//
8 months ago
{
2 months ago
var result = _invokeService.ExportExcelStreamByColumn(req);
2 months ago
//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");
8 months ago
}
}
}