using System.Net; using System.Net.Mail; using Microsoft.AspNetCore.Mvc; using Syncfusion.Drawing; using Syncfusion.XlsIO; using SytelineSaAppEfDataModel.Dtos; using SytelineSaAppEfDataModel.Services; namespace FaKrosnoApi.Controllers; [ApiController] [Route("api/[controller]")] public class ExcelGeneratorController(IWzHeaderService wzHeaderService, IMaterialTransactionService materialTransactionService, IConfiguration configuration) : Controller { [HttpGet("generate-meyle")] public async Task GeneratePackListForMeyle(Guid packListId) { WzHeaderDto wzHeader = await wzHeaderService.GetById(packListId); MaterialTransactionDto? materialTransaction = await materialTransactionService.GetByWzNumber(wzHeader.WzRowsMeyle.First().WzNumber); using ExcelEngine excelEngine = new ExcelEngine(); IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Xlsx; IWorkbook workbook = application.Workbooks.Create(1); IWorksheet worksheet = workbook.Worksheets[0]; IStyle boldFontStyle = workbook.Styles.Add("BoldFontStyle"); boldFontStyle.Font.Bold = true; var mainHeaders = new List { "Numer zamówienia Meyle", "Meyle Numer", "Ilość w dostawie", "Numer Palety", "Nr Wz", "Nr Partii" }; worksheet["B1"].Value = "Packing List"; worksheet["B1"].CellStyle = boldFontStyle; worksheet["B3"].Value = "Supplier Name"; worksheet["B3"].CellStyle = boldFontStyle; worksheet["D3"].Value = "FA KROSNO"; worksheet["D3"].CellStyle = boldFontStyle; worksheet["B4"].Value = "Packing List nr"; worksheet["B4"].CellStyle = boldFontStyle; worksheet["B6"].Value = "Related delivery note"; worksheet["B6"].CellStyle = boldFontStyle; worksheet["D6"].Value = string.Join(", ", wzHeader.WzRowsMeyle.Select(x => x.WzNumber).Distinct()); worksheet["D6"].CellStyle = boldFontStyle; worksheet["B9"].Value = "Forwarder"; worksheet["B9"].CellStyle = boldFontStyle; worksheet["B10"].Value = "Date"; worksheet["B10"].CellStyle = boldFontStyle; worksheet["D10"].DateTime = materialTransaction?.CreateDate ?? DateTime.Now; int currentRow = 12; for (int i = 0; i < mainHeaders.Count; i++) { string columnLetter = GetColumnLetter(i); worksheet.Range[$"{columnLetter}{currentRow}"].Text = mainHeaders[i]; worksheet.Range[$"{columnLetter}{currentRow}"].CellStyle = boldFontStyle; } currentRow++; foreach (var wzRow in wzHeader.WzRowsMeyle) { worksheet.Range[$"A{currentRow}"].Text = wzRow.OrderNumber; worksheet.Range[$"B{currentRow}"].Text = wzRow.ItemNumber; worksheet.Range[$"C{currentRow}"].Number = (double)wzRow.Quantity!; worksheet.Range[$"D{currentRow}"].Number = (double)wzRow.PalletNumber!; worksheet.Range[$"E{currentRow}"].Text = wzRow.WzNumber; worksheet.Range[$"F{currentRow}"].Text = wzRow.PartNumber; currentRow++; } for (int i = 0; i < 6; i++) { worksheet.AutofitColumn(i + 1); } using MemoryStream stream = new MemoryStream(); workbook.SaveAs(stream); stream.Position = 0; SendEmail(stream, wzHeader); } private static string GetColumnLetter(int columnIndex) { string columnName = string.Empty; while (columnIndex >= 0) { columnName = (char)('A' + (columnIndex % 26)) + columnName; columnIndex = (columnIndex / 26) - 1; } return columnName; } private void SendEmail(MemoryStream stream, WzHeaderDto wzHeader) { IConfigurationSection smtpSettings = configuration.GetSection("EmailSettings"); string smtpHost = smtpSettings["SmtpServer"] ?? string.Empty; int smtpPort = int.Parse(smtpSettings["Port"] ?? "0"); string smtpUsername = smtpSettings["SenderEmail"] ?? string.Empty; string smtpPassword = smtpSettings["SenderPassword"] ?? string.Empty; string fromEmail = smtpSettings["SenderEmail"] ?? string.Empty; List toEmail = wzHeader.EmailAddresses?.Split(',')?.ToList() ?? (smtpSettings["RecipientEmail"] ?? string.Empty).Split(',').ToList(); string subject = "MEYLE: Packing List"; string body = $"W załączeniu znajduje się Packing List dla klienta MEYLE wygenerowany {DateTime.Now:dd.MM.yyyy HH:mm:ss}"; using var mailMessage = new MailMessage(); mailMessage.From = new MailAddress(fromEmail); mailMessage.Subject = subject; mailMessage.Body = body; mailMessage.IsBodyHtml = false; mailMessage.Attachments.Add(new Attachment(stream, $"PackingList_Meyle_{DateTime.Now:yyyyMMddHHmmss}.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")); toEmail.ForEach(x => mailMessage.To.Add(x)); using var smtpClient = new SmtpClient(smtpHost, smtpPort); smtpClient.EnableSsl = true; smtpClient.Credentials = new NetworkCredential(smtpUsername, smtpPassword); smtpClient.Send(mailMessage); } }