sozsoft-platform/api/modules/Sozsoft.MailQueue/FileGeneration/XLSFileGenerator.cs
2026-10-17 21:51:10 +03:00

182 lines
6.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Globalization;
using ClosedXML.Excel;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Sozsoft.MailQueue.MailGeneration.Models;
using Volo.Abp.DependencyInjection;
namespace Sozsoft.MailQueue.FileGeneration;
public class XLSFileGenerator : IFileGenerator, ITransientDependency
{
public ILogger<XLSFileGenerator> Logger { get; set; } = NullLogger<XLSFileGenerator>.Instance;
string sheetPassword = "Erp123";
public async Task<byte[]> Generate(MailTemplateTableModel table)
{
// Create the workbook
var workbook = new XLWorkbook();
if (!table.DosyaAciklama.IsNullOrWhiteSpace())
{
workbook.Properties.Comments = table.DosyaAciklama;
}
AddSheet(workbook, "ByTruck", table);
AddSheet(workbook, "ByAir", table);
AddSheet(workbook, "BySea", table);
AddSheet(workbook, "ByDHL", table);
using var stream = new MemoryStream();
workbook.SaveAs(stream);
stream.Close();
return await Task.FromResult(stream.ToArray());
}
private void AddSheet(XLWorkbook workbook, string sheetName, MailTemplateTableModel table)
{
var sheet = workbook.Worksheets.Add(sheetName);
// NumberFormats:
// 0, 0.0, 0.00, gibi
// https://github.com/ClosedXML/ClosedXML/wiki/Styles-NumberFormat
if (table.Korumalimi == "1") //Korumali mi?
{
sheet.Protect(sheetPassword, XLProtectionAlgorithm.Algorithm.SHA512, XLSheetProtectionElements.SelectEverything | XLSheetProtectionElements.AutoFilter);
}
for (int i = 0; i < table.TabloSutunlari.Count; i++)
{
var col = table.TabloSutunlari[i];
if (col.Gizlimi) sheet.Column(i + 1).Hide();
var cell = sheet.Cell(1, i + 1);
cell.Value = col.SutunBaslik;
cell.Style.Font.FontSize = 8;
cell.Style.Font.Bold = true;
cell.Style.Alignment.SetVertical(XLAlignmentVerticalValues.Center);
cell.Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center);
cell.Style.Alignment.SetWrapText(true);
cell.Style.Protection.SetLocked(true);
cell.WorksheetRow().Height = 35;
cell.WorksheetRow().SetAutoFilter(true);
ColumnFormatting(col.BaslikCss, cell);
//Alt Toplam
if (!col.AltToplam.IsNullOrEmpty())
{
var subCell = sheet.Cell(table.TabloDegerleri.Count + 2, i + 1);
var cellName = subCell.WorksheetColumn().ColumnLetter();
subCell.Style.Font.FontSize = 8;
subCell.Style.Font.Bold = true;
subCell.Style.Alignment.SetVertical(XLAlignmentVerticalValues.Center);
subCell.Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Right);
subCell.SetFormulaA1(string.Format("={0}({1}{2}:{1}{3})", col.AltToplam, cellName, 2, table.TabloDegerleri.Count + 1));
}
}
for (int i = 0; i < table.TabloDegerleri.Count; i++)
{
var row = table.TabloDegerleri[i];
for (int j = 0; j < table.TabloSutunlari.Count; j++)
{
var col = table.TabloSutunlari[j];
var cell = sheet.Cell(i + 2, j + 1);
var value = row.GetValueOrDefault(col.SutunAdi);
if (!Enum.TryParse(col.VeriTipi, out XLDataType dataType))
{
dataType = XLDataType.Text;
}
switch (dataType)
{
case XLDataType.Number:
// Int32'ye cevirmek ondaligi atardi; hucre sayisal kalir, gosterimi Excel bicim kodu verir.
cell.SetValue(Convert.ToDecimal(value, CultureInfo.InvariantCulture));
break;
case XLDataType.DateTime:
cell.SetValue(Convert.ToDateTime(value));
break;
case XLDataType.Text:
cell.SetValue(value.ToString());
break;
default:
cell.SetValue(XLCellValue.FromObject(value));
break;
}
if (!col.VeriFormati.IsNullOrWhiteSpace())
{
if (dataType == XLDataType.Number)
{
cell.Style.NumberFormat.Format = col.VeriFormati;
}
else if (dataType == XLDataType.DateTime)
{
cell.Style.DateFormat.Format = col.VeriFormati;
}
}
cell.Style.Font.FontSize = 8;
cell.Style.Protection.SetLocked(col.Korumalimi);
ColumnFormatting(col.Css, cell);
}
}
for (int i = 0; i < table.TabloSutunlari.Count; i++)
{
var col = table.TabloSutunlari[i];
sheet.Column(i + 1).Width = col.Genislik;
}
//sheet.Columns().AdjustToContents();
}
private void ColumnFormatting(Dictionary<string, string> css, IXLCell cell)
{
if (css.IsNullOrEmpty())
return;
try
{
// color
if (css.ContainsKey("color"))
{
cell.Style.Font.SetFontColor(XLColor.FromHtml(css["color"]));
}
// background-color
if (css.ContainsKey("background-color"))
{
cell.Style.Fill.SetBackgroundColor(XLColor.FromHtml(css["background-color"]));
}
// align
if (css.ContainsKey("text-align"))
{
var alignment = css["text-align"] switch
{
"left" => XLAlignmentHorizontalValues.Left,
"right" => XLAlignmentHorizontalValues.Right,
"justify" => XLAlignmentHorizontalValues.Justify,
_ => XLAlignmentHorizontalValues.Center,
};
cell.Style.Alignment.SetHorizontal(alignment);
}
// border-color
if (css.ContainsKey("border-color"))
{
cell.Style.Border.SetOutsideBorderColor(XLColor.FromHtml(css["border-color"]));
}
// border-width
if (css.ContainsKey("border-width"))
{
cell.Style.Border.SetOutsideBorder(XLBorderStyleValues.Thin);
}
}
catch (Exception ex)
{
// Geçersiz CSS değeri hücreyi biçimsiz bırakır; dosya üretimi sürer.
Logger.LogWarning(ex, "Hücre CSS'i uygulanamadı: {Css}", string.Join(";", css.Select(a => a.Key + ":" + a.Value)));
}
}
}