设备数采项目刷选 工艺标准模板导出

This commit is contained in:
2023-06-05 17:23:19 +08:00
parent 7ec192f711
commit 0d7e49695c
12 changed files with 471 additions and 1 deletions

View File

@@ -0,0 +1,94 @@
using JNPF;
using JNPF.Common.Core.Manager;
using JNPF.Common.Enums;
using JNPF.Common.Helper;
using JNPF.Common.Security;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.FriendlyException;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using SqlSugar;
using Tnb.PerMgr.Entities;
using Tnb.PerMgr.Entities.Dto;
using Tnb.PerMgr.Interfaces;
namespace Tnb.PerMgr
{
/// <summary>
/// 工艺标准
/// </summary>
[ApiDescriptionSettings(Tag = ModuleConsts.Tag, Area = ModuleConsts.Area, Order = 700)]
[Route("api/[area]/[controller]/[action]")]
public class PerProcessStandardsService : IPerProcessStandardsService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository<PerProcessStandardsH> _repository;
private readonly IUserManager _userManager;
public PerProcessStandardsService(ISqlSugarRepository<PerProcessStandardsH> repository, IUserManager userManager)
{
_userManager = userManager;
_repository = repository;
}
[AllowAnonymous]
[HttpGet]
public async Task<IActionResult> ExportTemplate()
{
try
{
var db = _repository.AsSugarClient();
string[] perProcessParamTypes = await db.Queryable<PerProcessParamType>().OrderBy(x => x.ordinal).Select(x=>x.name).ToArrayAsync();
string[] perProcessParams = await db.Queryable<PerProcessParam>().OrderBy(x => x.ordinal).Select(x=>x.name).ToArrayAsync();
XSSFWorkbook workbook = new XSSFWorkbook();
NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("BOM详情");
IRow row1 = sheet.CreateRow(0);
string[] titles = new[] { "工艺参数类型", "工艺参数", "设定值" };
for (int i = 0; i < titles.Length; i++)
{
ICell cell1 = row1.CreateCell(i);
cell1.SetCellValue(titles[i]);
sheet.SetColumnWidth(i,15 * 256);
}
int rowIndex = 1;
// var column = sheet.GetColumn(0);
// 设置下拉项
var validationHelper = sheet.GetDataValidationHelper();
var constraint = validationHelper.CreateExplicitListConstraint(perProcessParamTypes);
var region = new CellRangeAddressList(1, 1000, 0, 0);
var validation = validationHelper.CreateValidation(constraint, region);
sheet.AddValidationData(validation);
var constraint2 = validationHelper.CreateExplicitListConstraint(perProcessParams);
var region2 = new CellRangeAddressList(1, 1000, 1, 1);
var validation2 = validationHelper.CreateValidation(constraint2, region2);
sheet.AddValidationData(validation2);
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
MemoryStream ms2 = new MemoryStream(ms.ToArray());
ms2.Position = 0;
FileStreamResult fileStreamResult = new FileStreamResult(ms2, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = "template.xlsx" };
return fileStreamResult;
}
catch (Exception e)
{
Console.WriteLine(e);
throw Oops.Bah("导出失败");
}
}
}
}