168 lines
6.2 KiB
C#
168 lines
6.2 KiB
C#
using ClosedXML.Excel;
|
|
using DocumentFormat.OpenXml.Spreadsheet;
|
|
using Kurs.MailQueue.MailGeneration.Models;
|
|
using Volo.Abp.DependencyInjection;
|
|
|
|
namespace Kurs.MailQueue.FileGeneration;
|
|
|
|
public class XLSFileGenerator : IFileGenerator, ITransientDependency
|
|
{
|
|
string sheetPassword = "kurs123";
|
|
|
|
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 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);
|
|
var dataType = XLDataType.Text;
|
|
Enum.TryParse(col.VeriTipi, out dataType);
|
|
switch (dataType)
|
|
{
|
|
case XLDataType.Number:
|
|
cell.SetValue(Convert.ToInt32(value));
|
|
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 { }
|
|
}
|
|
}
|