2025-11-11 09:20:17 +08:00

139 lines
5.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Syc.Basic.Web.WMS.Dto;
using Syc.Basic.Web.WMS.Entitys;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
namespace Syc.Basic.Web.WMS.Service
{
[AllowAnonymous]
public class DownService:ApiService
{
private readonly IRepository<Silk> silkRepository;
private readonly IRepository<Box> boxRepository;
public DownService(IRepository<Silk> silkRepository, IRepository<Box> boxRepository)
{
this.silkRepository = silkRepository;
this.boxRepository = boxRepository;
}
[HttpPost("api/DownSilk")]
public async Task<string> DownLoadSilk(IdsInput input)
{
var silks = await silkRepository.GetListAsync(x => input.Ids.Contains(x.Id));
var silklist = silks.Select(x => new DownSilkDto
{
Id = x.Id,
Code = x.Code,
Lot_No = x.Lot_No,
Name = x.Name,
Type = x.Type,
Length = x.Length,
Net_Weight = x.Net_Weight,
Color = x.Color,
Date = x.Date,
BoxId = x.BoxId,
Createtime = x.Createtime,
Status_Details = x.Status_Details
}).ToList();
if (silks.Count == 0)
throw Oops.Oh("没有导出的数据");
// 2. 自定义 Excel 表头(可选:替换模型属性名)
var customHeaders = new Dictionary<string, string>
{
{ nameof(Silk.Id), "Id" },
{ nameof(Silk.Code), "编号" },
{ nameof(Silk.Lot_No), "批次号" },
{ nameof(Silk.Name), "名称" },
{ nameof(Silk.Type), "规格" },
{ nameof(Silk.Length), "长度" },
{ nameof(Silk.Net_Weight), "重量(kg)" },
{ nameof(Silk.Color), "标注" },
{ nameof(Silk.BoxId), "成箱编号" },
{ nameof(Silk.Date), "生产时间" },
{ nameof(Silk.Status_Details), "状态说明" },
{ nameof(Silk.Createtime), "创建时间" }
};
var webHost = base.HttpContext.RequestServices.GetService(typeof(IWebHostEnvironment)) as IWebHostEnvironment;
// 3. 生成 Excel 内存流
var fileName1 = $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
var fiePath = Path.Combine(webHost.WebRootPath, fileName1);
ExcelExporter.ExportToExcelStream(silklist, "丝锭数据报表", fiePath, customHeaders);
// 4. 配置下载响应(解决中文文件名乱码)
string fileName = $"丝锭数据报表_{DateTime.Now:yyyyMMddHHmmss}.xlsx";
string encodedFileName = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
// 5. 返回文件流浏览器自动下载xx
return $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.Host}:{HttpContext.Request.Host.Port}/{fileName1}";
}
[HttpPost("api/DownBox")]
public async Task<string> DownLoadBox(IdsInput input)
{
var box = await boxRepository.GetListAsync(x => input.Ids.Contains(x.Id));
var boxlist = box.Select(x => new DownBoxDto
{
Code = x.Code,
Lot_No = x.Lot_No,
Type = x.Type,
Spec = x.Spec,
Length = x.Length,
Net_Weight = x.Net_Weight,
Gross_Weight = x.Gross_Weight,
Qty = x.Qty,
Color = x.Color,
Dom_Time = x.Dom_Time,
Exp_Time = x.Exp_Time,
Mark = x.Mark,
Create_Time = x.Create_Time
}).ToList();
if (box.Count == 0)
throw Oops.Oh("没有导出的数据");
// 2. 自定义 Excel 表头(可选:替换模型属性名)
var customHeaders = new Dictionary<string, string>
{
{ nameof(Box.Code), "编号" },
{ nameof(Box.Lot_No), "批次号" },
{ nameof(Box.Type), "名称" },
{ nameof(Box.Spec), "规格" },
{ nameof(Box.Length), "长度" },
{ nameof(Box.Net_Weight), "重量(kg)" },
{ nameof(Box.Gross_Weight), "毛重(kg)" },
{ nameof(Box.Qty), "每箱/个" },
{ nameof(Box.Color), "标注" },
{ nameof(Box.Dom_Time), "生产时间" },
{ nameof(Box.Exp_Time), "有效期" },
{ nameof(Box.Mark), "状态说明" },
{ nameof(Box.Create_Time), "创建时间" }
};
var webHost = base.HttpContext.RequestServices.GetService(typeof(IWebHostEnvironment)) as IWebHostEnvironment;
// 3. 生成 Excel 内存流
var fileName1 = $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
var fiePath = Path.Combine(webHost.WebRootPath, fileName1);
ExcelExporter.ExportToExcelStream(boxlist, "丝锭数据报表", fiePath, customHeaders);
// 4. 配置下载响应(解决中文文件名乱码)
string fileName = $"成箱数据报表_{DateTime.Now:yyyyMMddHHmmss}.xlsx";
string encodedFileName = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
// 5. 返回文件流浏览器自动下载xx
return $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.Host}:{HttpContext.Request.Host.Port}/{fileName1}";
}
}
}