模板导出成功

This commit is contained in:
qianhao.xu
2024-01-20 14:37:32 +08:00
parent 7af284924d
commit 9b6e30f9a8
16 changed files with 963 additions and 17 deletions

View File

@@ -64,7 +64,8 @@ namespace ZR.Admin.WebApi.Controllers
var stream = Io.File.OpenRead(path); //创建文件流
Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", HttpUtility.UrlEncode(fileName));
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", HttpUtility.UrlEncode(fileName));
// return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",fileName);
}
#region

View File

@@ -0,0 +1,162 @@
using Infrastructure.Extensions;
using JinianNet.JNTemplate;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using MiniExcelLibs;
using Model.DBModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System.Text.Json;
using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters;
using ZR.Model.mes.pro;
using ZR.Model.MES.pro;
using ZR.Service.mes.pro;
using ZR.Service.mes.pro.IService;
namespace ZR.Admin.WebApi.Controllers.mes.pro
{
[Route("mes/pro/workorder_v2")]
public class ProWorkorderV2Controller : BaseController
{
private readonly IProWorkorderServiceV2 proWorkorderService;
public ProWorkorderV2Controller(IProWorkorderServiceV2 proWorkorderService)
{
this.proWorkorderService = proWorkorderService;
}
[HttpGet("getWorkoderList")]
public IActionResult GetWorkorderList(int pageNum, int pageSize, int year = -1, int week = -1, int date = -1)
{
(List<ProWorkorder_v2>, int) data = proWorkorderService.GetWorkorderList(pageNum, pageSize, year, week, date, 0);
return ToResponse(new ApiResult(200, "success", data));
}
/// <summary>
/// 生产工单模板下载
/// </summary>
/// <returns></returns>
[HttpGet("importTemplate")]
[Log(Title = "生产工单模板模板", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)]
[AllowAnonymous] //不需要授权 就可以访问
public IActionResult ImportTemplateExcel()
{
(string, string) result = DownloadImportTemplate("日生产计划模板");//返回文件名和路径
return ExportExcel(result.Item2, result.Item1);
}
/// <summary>
/// 导入
/// </summary>
/// <param name="formFile">使用IFromFile必须使用name属性否则获取不到文件</param>
/// <returns></returns>
[HttpPost("importData")]
[Log(Title = "生产计划导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)]
public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile, bool updateSupport)
{
//1.0 读取excel 文件 保存在指定位置
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
string sFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + formFile.FileName;
string target = Path.Combine(webHostEnvironment.WebRootPath, "workorder", sFileName);
int year = 0;
int week = 0;
int date = 0;
using (var stream = formFile.OpenReadStream())
{
FileStream targetFileStream = new FileStream(target, FileMode.Create, FileAccess.Write);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
targetFileStream.Write(buffer, 0, bytesRead);
}
//2.0 解析excel
//读取第一行 解析 年和月
var row = stream.Query().Skip(1).First();
year = Convert.ToInt32(row.A);
week = Convert.ToInt32(row.B);
date = Convert.ToInt32(row.C);
//读取列表数据
try
{
var list = stream.Query<ProWorkorder_v2>(sheetName: "Sheet1", startCell: "A3")
.Where(it => it.FinishedPartNumber != null)
.Where(it =>it.BlankNumber!=null&&!it.BlankNumber.Contains("圈数"))
.ToList();
list.ForEach(it =>
{
it.ToCreate(HttpContext);
it.Year = year;
it.Week = week;
it.Date = date;
});
string result = proWorkorderService.ImportExceldata(list);
return SUCCESS(result);
}
catch (Exception ex)
{
return ToResponse(ResultCode.GLOBAL_ERROR, "模板内容错误,请仔细检测格式,并联系管理员" + ex.Message);
}
}
return SUCCESS(null);
}
/// <summary>
/// 浏览器下载 生产工单
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
[HttpGet("downloadWorkorder")]
[Log(Title = "下载生产工单", BusinessType = BusinessType.EXPORT)]
public IActionResult UserExport(int? year, int? week,int? date)
{
if (year == null || week == null || date==null)
{
return SUCCESS(0);
}
var result = proWorkorderService.ExportExceldata((int)year, (int)week,(int)date);
return ExportExcel(result.Item2, result.Item1);
}
/// <summary>
/// 删除本周所有计划
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("deleteAll")]
public IActionResult DeleteAllItem(int? year, int? week, int? date)
{
int data = 0;
if (week != null && week > 0)
{
if (year != null && year > 0)
data = proWorkorderService.DeleteAllWorkorder((int)year, (int)week,(int)date);
}
return ToResponse(new ApiResult(200, "success", data));
}
}
}

View File

@@ -1,7 +1,21 @@
using Microsoft.AspNetCore.Mvc;
using Aliyun.OSS;
using AutoMapper.Configuration.Conventions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting.Internal;
using MimeKit;
using MiniExcelLibs;
using MiniExcelLibs.OpenXml;
using Model.DBModel;
using Org.BouncyCastle.Crypto.IO;
using System.IO;
using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters;
using ZR.Model;
using ZR.Model.mes.pro;
using ZR.Model.MES.pro.DTO;
using ZR.Model.System;
using ZR.Model.System.Dto;
using ZR.Service.mes.pro;
using ZR.Service.mes.pro.IService;
namespace ZR.Admin.WebApi.Controllers.mes.pro
@@ -71,7 +85,23 @@ namespace ZR.Admin.WebApi.Controllers.mes.pro
return ToResponse(new ApiResult(200, "success", data));
}
/// <summary>
/// 删除本周所有计划
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("deleteAll")]
public IActionResult DeleteAllItem(int? year,int? week)
{
int data = 0;
if (week!=null&&week>0)
{
if (year != null && year > 0)
data = proWorkplanService.DeleteAllWorkPlan((int)year,(int)week);
}
return ToResponse(new ApiResult(200, "success", data));
}
/// <summary>
@@ -93,5 +123,131 @@ namespace ZR.Admin.WebApi.Controllers.mes.pro
return ToResponse(new ApiResult(200, "success", data));
}
/// <summary>
/// 生产计划模板下载
/// </summary>
/// <returns></returns>
[HttpGet("importTemplate")]
[Log(Title = "生产计划模板模板", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)]
[AllowAnonymous] //不需要授权 就可以访问
public IActionResult ImportTemplateExcel()
{
(string, string) result = DownloadImportTemplate("周生产计划模板");//返回文件名和路径
return ExportExcel(result.Item2, result.Item1);
}
/// <summary>
/// 获取周汇总
/// </summary>
/// <param name="year"></param>
/// <param name="week"></param>
/// <returns></returns>
[HttpGet("getWeekSummary")]
public IActionResult GetWeekSummary(int? year, int? week)
{
if (year == null && week == null)
{
return SUCCESS(0);
}
WorkplanSummaryDto workplanSummaryDto = proWorkplanService.GetWeekSummary((int)year, (int)week);
return SUCCESS(workplanSummaryDto);
}
/// <summary>
/// 导入
/// </summary>
/// <param name="formFile">使用IFromFile必须使用name属性否则获取不到文件</param>
/// <returns></returns>
[HttpPost("importData")]
[Log(Title = "生产计划导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)]
public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile, bool updateSupport)
{
//1.0 读取excel 文件 保存在指定位置
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
string sFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + formFile.FileName;
string target = Path.Combine(webHostEnvironment.WebRootPath, "workplan", sFileName);
int year = 0;
int week = 0;
using (var stream = formFile.OpenReadStream())
{
FileStream targetFileStream = new FileStream(target, FileMode.Create, FileAccess.Write);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
targetFileStream.Write(buffer, 0, bytesRead);
}
//2.0 解析excel
//读取第一行 解析 年和月
var row = stream.Query().Take(1).First();
year = Convert.ToInt32(row.A);
week = Convert.ToInt32(row.B);
//读取列表数据
try
{
var list = stream.Query<ProWorklplan_v2>(sheetName: "Sheet1", startCell: "B3")
.Where(it => it.Partnumber != null)
.Where(it => !it.Partnumber.Contains("合计"))
.Where(it => it.RequireNum > 0)
.ToList();
list.ForEach(it =>
{
it.ToCreate(HttpContext);
it.Year = year;
it.Week = week;
});
string result = proWorkplanService.ImportExceldata(list);
return SUCCESS(result);
}
catch (Exception ex)
{
return ToResponse(ResultCode.GLOBAL_ERROR,"模板内容错误,请仔细检测格式,并联系管理员"+ex.Message);
}
}
return SUCCESS(null);
}
/// <summary>
/// 浏览器下载 生产计划
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
[HttpGet("downloadWorkplan")]
[Log(Title = "下载生产计划", BusinessType = BusinessType.EXPORT)]
public IActionResult UserExport(int? year, int? week)
{
if (year == null || week == null)
{
return SUCCESS(0);
}
var result = proWorkplanService.ExportExceldata((int)year, (int)week);
return ExportExcel(result.Item2, result.Item1);
}
}
}