diff --git a/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs b/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs index bcacf6c3..c773f551 100644 --- a/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs +++ b/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs @@ -59,6 +59,8 @@ using System.Text.RegularExpressions; using MathNet.Numerics.Distributions; using Microsoft.IdentityModel.Tokens; using Myshipping.Application.EDI.ESL; +using NPOI.HSSF.UserModel; +using NPOI.SS.UserModel; namespace Myshipping.Application { @@ -93,6 +95,7 @@ namespace Myshipping.Application private readonly SqlSugarRepository _repBookingStatus; private readonly SqlSugarRepository _bookingEDIExt; private readonly SqlSugarRepository _repUserConfig; + private readonly SqlSugarRepository _excelrep; private readonly IHttpContextAccessor _httpContextAccessor; const string CONST_MAPPING_MODULE = "BOOK_OR_CLOSING"; const string CONST_MAPPING_MODULE_ROUTE = "BOOK_OR_CLOSING_RT"; @@ -105,7 +108,8 @@ namespace Myshipping.Application ILogger logger, ISysCacheService cache, IDjyWebsiteAccountConfigService webAccountConfig, ISysDataUserMenu right, SqlSugarRepository repPrintTemplate, SqlSugarRepository repLetterYard, SqlSugarRepository repUser, SqlSugarRepository repOrderUrl, SqlSugarRepository repOrderContact, SqlSugarRepository repSampleBill, - SqlSugarRepository repUserMail, SqlSugarRepository repTenant, SqlSugarRepository repBookingStatus, SqlSugarRepository bookingEDIExt, + SqlSugarRepository excelrep, SqlSugarRepository repUserMail, + SqlSugarRepository repTenant, SqlSugarRepository repBookingStatus, SqlSugarRepository bookingEDIExt, IHttpContextAccessor httpContextAccessor) { this._logger = logger; @@ -133,6 +137,7 @@ namespace Myshipping.Application this._repBookingStatus = repBookingStatus; this._bookingEDIExt = bookingEDIExt; this._repUserConfig = repUserConfig; + this._excelrep = excelrep; _httpContextAccessor = httpContextAccessor; } @@ -1712,7 +1717,55 @@ namespace Myshipping.Application } else if (printType == BookingPrintTemplateType.ExcelTemplate) { - //todo:excel模板打印功能 + var opt = App.GetOptions(); + var dirAbs = opt.basePath; + if (string.IsNullOrEmpty(dirAbs)) + { + dirAbs = App.WebHostEnvironment.WebRootPath; + } + var fileAbsPath = Path.Combine(dirAbs, printTemplate.FilePath); + _logger.LogInformation($"准备调用EXCEL生成:id:{bookingId},文件:{printTemplate.FileName}"); + var result = new FileStream(fileAbsPath, FileMode.Open); + var excelwork = new HSSFWorkbook(result); + var sheet = excelwork.GetSheetAt(0); + var order = await _rep.AsQueryable().Where(x => x.Id == bookingId).FirstAsync(); + var entity = await _excelrep.AsQueryable().Where(x => x.Pid == templateId).OrderBy(x => x.Row).ToListAsync(); + //单元格样式 + var cellStyle = NpoiExcelExportHelper._.CreateStyle(excelwork, HorizontalAlignment.Center, VerticalAlignment.Center, 10, true, 0); + for (int _row = 0; _row < entity.Max(x => x.Row); _row++) + { + + if (entity.Where(x => x.Row == (_row + 1)).Count() > 0) + { + ///创建行 + var row = NpoiExcelExportHelper._.CreateRow(sheet, _row, 25); + for (int _cellNum = 0; _cellNum < entity.Max(x => x.Column); _cellNum++) + { + if (entity.Where(x => x.Row == _row + 1 && x.Column == _cellNum + 1).Count() > 0) + { + foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(order)) + { + var name = entity.Where(x => x.Row == _row + 1 && x.Column == _cellNum + 1).Select(x => x.Field).FirstOrDefault(); + var _name = descriptor.Name.ToLower(); + if (name == _name) + { + var value = descriptor.GetValue(order).ToString(); + var cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, _cellNum + 1, value); + } + } + } + } + } + } + var fileFullPath = Path.Combine(App.WebHostEnvironment.WebRootPath, App.GetOptions().Path);//服务器路径 + if (!Directory.Exists(fileFullPath)) + { + Directory.CreateDirectory(fileFullPath); + } + fileName = string.Format("订舱{0}.xls", DateTime.Now.Ticks); + _logger.LogInformation("导出excel:" + Path.Combine(fileFullPath, fileName)); + var filestream = new FileStream(Path.Combine(fileFullPath, fileName), FileMode.OpenOrCreate, FileAccess.ReadWrite); + excelwork.Write(filestream); } //记录打印次数和时间,用于前端动态展示常用的打印类型 diff --git a/Myshipping.Application/Service/BookingPrintTemplate/BookingPrintTemplateService.cs b/Myshipping.Application/Service/BookingPrintTemplate/BookingPrintTemplateService.cs index b1d359e7..93f28cf8 100644 --- a/Myshipping.Application/Service/BookingPrintTemplate/BookingPrintTemplateService.cs +++ b/Myshipping.Application/Service/BookingPrintTemplate/BookingPrintTemplateService.cs @@ -439,7 +439,7 @@ namespace Myshipping.Application /// /// [HttpPost("/BookingTemplate/AddOrUpdateExcelTemplate")] - public async Task AddOrUpdateExcelTemplate(BookingExcelTemplateDto dto) + public async Task AddOrUpdateExcelTemplate(BookingExcelTemplateDto dto) { @@ -450,7 +450,6 @@ namespace Myshipping.Application await _excelrep.InsertAsync(entity); } - return null; } /// /// 获取excel模板详情 diff --git a/Myshipping.Application/Service/BookingTemplate/BookingTemplateService.cs b/Myshipping.Application/Service/BookingTemplate/BookingTemplateService.cs index e4351222..98b96d24 100644 --- a/Myshipping.Application/Service/BookingTemplate/BookingTemplateService.cs +++ b/Myshipping.Application/Service/BookingTemplate/BookingTemplateService.cs @@ -40,7 +40,7 @@ namespace Myshipping.Application public BookingTemplateService(SqlSugarRepository rep, SqlSugarRepository repOrder, SqlSugarRepository repPrintTemplate, ILogger logger) { - _excelrep = excelrep; + _repOrder = repOrder; _rep = rep; @@ -123,97 +123,6 @@ namespace Myshipping.Application { return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id); } - - - - - /// - /// 导出excel - /// - /// - /// - /// - /// - [HttpGet("/BookingTemplate/ExportExcel")] - public async Task ExportExcel([FromQuery] long bookingId, long excelTemplateId,long printTemplateId) - { - if (bookingId == 0 || excelTemplateId == 0|| printTemplateId==0) - { - - throw Oops.Bah("请传入正确参数"); - } - var printTemplate = await _repPrintTemplate.AsQueryable().Filter(null, true).FirstAsync(x => x.Id== printTemplateId); - if (printTemplate == null) - { - throw Oops.Bah(BookingErrorCode.BOOK115); - } - var opt = App.GetOptions(); - var dirAbs = opt.basePath; - if (string.IsNullOrEmpty(dirAbs)) - { - dirAbs = App.WebHostEnvironment.WebRootPath; - } - - var fileAbsPath = Path.Combine(dirAbs, printTemplate.FilePath); - _logger.LogInformation($"查找EXCEL模板文件:{fileAbsPath}"); - if (!File.Exists(fileAbsPath)) - { - throw Oops.Bah(BookingErrorCode.BOOK115); - } - - _logger.LogInformation($"准备调用EXCEL生成:id:{bookingId},文件:{printTemplate.FileName}"); - - var result = new FileStream(fileAbsPath, FileMode.Open) ; - - var excelwork = new HSSFWorkbook(result); - var sheet = excelwork.GetSheetAt(0); - - var order = await _repOrder.AsQueryable().Where(x => x.Id == bookingId).FirstAsync(); - var entity = await _excelsubrep.AsQueryable().Where(x => x.Pid == excelTemplateId).OrderBy(x => x.Row).ToListAsync(); - //单元格样式 - var cellStyle = NpoiExcelExportHelper._.CreateStyle(excelwork, HorizontalAlignment.Center, VerticalAlignment.Center, 10, true, 0); - - for (int _row = 0; _row < entity.Max(x => x.Row); _row++) - { - - if (entity.Where(x => x.Row == (_row + 1)).Count() > 0) - { - ///创建行 - var row = NpoiExcelExportHelper._.CreateRow(sheet, _row, 25); - for (int _cellNum = 0; _cellNum < entity.Max(x => x.Column); _cellNum++) - { - if (entity.Where(x => x.Row == _row + 1 && x.Column == _cellNum + 1).Count() > 0) - { - foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(order)) - { - var name = entity.Where(x => x.Row == _row + 1 && x.Column == _cellNum + 1).Select(x => x.Field).FirstOrDefault(); - var _name = descriptor.Name.ToLower(); - if (name == _name) - { - var value = descriptor.GetValue(order).ToString(); - var cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, _cellNum + 1, value); - } - } - } - } - } - } - - var fileFullPath = Path.Combine(App.WebHostEnvironment.WebRootPath, App.GetOptions().Path);//服务器路径 - if (!Directory.Exists(fileFullPath)) - { - Directory.CreateDirectory(fileFullPath); - } - var fileName = string.Format("订舱{0}.xls", DateTime.Now.Ticks); - _logger.LogInformation("导出excel:" + Path.Combine(fileFullPath, fileName)); - var filestream = new FileStream(Path.Combine(fileFullPath, fileName), FileMode.OpenOrCreate, FileAccess.ReadWrite); - excelwork.Write(filestream); - //var result = new FileStreamResult(new FileStream(Path.Combine(fileFullPath, fileName), FileMode.Open), "application/octet-stream") { FileDownloadName = fileName }; - return fileName; - } - - - - + } }