diff --git a/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs b/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs index 9961055d..2b4e3297 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs @@ -93,111 +93,6 @@ namespace Tnb.WarehouseMgr } return Task.FromResult(isMatch); } - - /// - /// 解析Excel到内存 - /// - /// - /// - protected Task>> ImportExcelToMemory(IFormFile file) - { - int rowIndex = 1; - List> dics = new(); - IWorkbook? workbook = null; - try - { - using (Stream stream = file.OpenReadStream()) - { - // 2007版本 - if (file.Name.IndexOf(".xlsx") > 0) - workbook = new XSSFWorkbook(stream); - else if (file.Name.IndexOf(".xls") > 0) - workbook = new HSSFWorkbook(stream); - - ISheet? sheet = workbook?.GetSheetAt(0); - - if (workbook == null || sheet == null) - throw Oops.Bah("无导入数据"); - - if (sheet?.LastRowNum <= 1) - throw Oops.Bah("无导入数据"); - - ICellStyle style = workbook.CreateCellStyle(); - IFont font = workbook.CreateFont(); - font.Color = IndexedColors.Black.Index; // 将字体颜色设置为红色 - style.SetFont(font); - var nameRow = sheet?.GetRow(0); - if (nameRow != null) - { - Dictionary dic = new Dictionary(); - List columns = new List(); - columns = nameRow.Cells.Select(x => x.StringCellValue).ToList(); - columns.ForEach(x => { - var strings =x.Split(new char[2] { '(', ')' }); - x = strings[1]; - }); - if (columns?.Count > 0) - { - for (rowIndex = 1; rowIndex <= sheet?.LastRowNum; rowIndex++) - { - var row = sheet?.GetRow(rowIndex); - if (row != null) - { - foreach (var col in columns) - { - ICell cell = row.GetCell(columns.IndexOf(col)); - dic.Add(col, cell.StringCellValue); - } - dics.Add(dic); - } - } - } - } - - } - } - catch (Exception) - { - - throw; - } - - return Task.FromResult(dics); - } - - /// - /// WMS导入模板 - /// - /// - /// - /// - protected dynamic WmsTemplate(Dictionary dic, string sheetName) - { - XSSFWorkbook workbook = new XSSFWorkbook(); - ISheet sheet = workbook.CreateSheet(sheetName); - IRow nRow = sheet.CreateRow(0); - IRow dRow = sheet.CreateRow(1); - dRow.Hidden = true; - dic.Remove("id"); - dic.Remove("create_id"); - dic.Remove("create_time"); - //sheet.SetColumnHidden(1, true); - for (int i = 0; i < dic?.Count; i++) - { - ICell cell1 = nRow.CreateCell(i); - cell1.SetCellValue(dic.Values.GetIndex(i)); - ICell cell2 = dRow.CreateCell(i); - cell2.SetCellValue(dic.Keys.GetIndex(i)); - } - - 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; - } [NonAction] diff --git a/WarehouseMgr/Tnb.WarehouseMgr/ExcelDataImportManager.cs b/WarehouseMgr/Tnb.WarehouseMgr/ExcelDataImportManager.cs new file mode 100644 index 00000000..9e53412f --- /dev/null +++ b/WarehouseMgr/Tnb.WarehouseMgr/ExcelDataImportManager.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using JNPF.FriendlyException; +using Microsoft.AspNetCore.Http; +using NPOI.HSSF.UserModel; +using NPOI.SS.UserModel; +using NPOI.XSSF.UserModel; + +namespace Tnb.WarehouseMgr +{ + public class ExcelDataImportManager : BaseWareHouseService + { + /// + /// 解析Excel到内存 + /// + /// + /// + protected Task>> ImportExcelToMemory(IFormFile file) + { + int rowIndex = 1; + List> dics = new(); + IWorkbook? workbook = null; + try + { + using (Stream stream = file.OpenReadStream()) + { + // 2007版本 + if (file.Name.IndexOf(".xlsx") > 0) + { + workbook = new XSSFWorkbook(stream); + } + else if (file.Name.IndexOf(".xls") > 0) + { + workbook = new HSSFWorkbook(stream); + } + + ISheet? sheet = workbook?.GetSheetAt(0); + if (workbook == null || sheet == null) throw Oops.Bah("无导入数据"); + if (sheet?.LastRowNum <= 1) throw Oops.Bah("无导入数据"); + + ICellStyle style = workbook.CreateCellStyle(); + IFont font = workbook.CreateFont(); + font.Color = IndexedColors.Black.Index; // 将字体颜色设置为红色 + style.SetFont(font); + var nameRow = sheet?.GetRow(0); + if (nameRow != null) + { + Dictionary dic = new Dictionary(); + List columns = new List(); + columns = nameRow.Cells.Select(x => x.StringCellValue).ToList(); + columns.ForEach(x => + { + var strings = x.Split(new char[2] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries); + x = strings[1]; + }); + if (columns?.Count > 0) + { + for (rowIndex = 1; rowIndex <= sheet?.LastRowNum; rowIndex++) + { + var row = sheet?.GetRow(rowIndex); + if (row != null) + { + foreach (var col in columns) + { + ICell cell = row.GetCell(columns.IndexOf(col)); + dic.Add(col, cell.StringCellValue); + } + dics.Add(dic); + } + } + } + } + } + } + catch (Exception) + { + throw; + } + return Task.FromResult(dics); + } + } +} diff --git a/WarehouseMgr/Tnb.WarehouseMgr/WmsCarryLedgerService.cs b/WarehouseMgr/Tnb.WarehouseMgr/WmsCarryLedgerService.cs index d6967c4b..9ce2b72c 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/WmsCarryLedgerService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/WmsCarryLedgerService.cs @@ -32,7 +32,7 @@ namespace Tnb.WarehouseMgr /// 载具台账服务 /// [OverideVisualDev(ModuleConsts.MODULE_WMSCARRY_ID)] - public class WmsCarryLedgerService : BaseWareHouseService, IWmsCarryLedgerService + public class WmsCarryLedgerService : ExcelDataImportManager, IWmsCarryLedgerService { private const string ModuleId = ""; private readonly ISqlSugarClient _db; diff --git a/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs b/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs deleted file mode 100644 index 79d92592..00000000 --- a/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using DingTalk.Api.Request; -using JNPF.Common.Configuration; -using JNPF.Common.Contracts; -using JNPF.Common.Core.Manager; -using JNPF.Common.Extension; -using JNPF.Common.Helper; -using JNPF.Common.Models; -using JNPF.Common.Security; -using JNPF.Extras.CollectiveOAuth.Enums; -using JNPF.FriendlyException; -using JNPF.Logging; -using Mapster; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using NPOI.HPSF; -using NPOI.HSSF.UserModel; -using NPOI.SS.UserModel; -using NPOI.XSSF.UserModel; -using SqlSugar; -using Tnb.BasicData.Entities; -using Tnb.WarehouseMgr.Entities; -using Tnb.WarehouseMgr.Entities.Enums; - -namespace Tnb.WarehouseMgr -{ - /// - /// 导入和导出接口服务提供类 - /// - public class WmsImportAndExportService : BaseWareHouseService - { - private readonly ISqlSugarClient _db; - private readonly IUserManager _userManager; - public WmsImportAndExportService(ISqlSugarRepository repository, IUserManager userManager) - { - _db = repository.AsSugarClient(); - _userManager = userManager; - } - - /// - /// 载具导入模板 - /// - /// - /// - [HttpPost] - public async Task CarryImportTemplate() - { - int row = 0; - Dictionary dic = new Dictionary(); - dic.Add("carry_code", "载具编号"); - dic.Add("carry_name", "载具名称"); - dic.Add("carrystd_id", "载具规格"); - //headers = _db.DbMaintenance.GetColumnInfosByTableName("wms_carry_h").FindAll(x=>!x.IsNullable).ToDictionary(x => x.DbColumnName ,x => x.ColumnDescription); - FileStreamResult fileStreamResult = WmsTemplate(dic, "载具台账"); - - return fileStreamResult; - } - - } -}