kreta/Kreta.BusinessLogic/Helpers/Nyomtatvanyok/Excel/HianyzokEsJelenlevokSzama.cs
2024-03-13 00:33:46 +01:00

268 lines
11 KiB
C#

using System.Collections.Generic;
using System.Data;
using System.Linq;
using Kreta.BusinessLogic.Classes;
using Kreta.Core.ConnectionType;
using Kreta.Resources;
using OfficeOpenXml;
using OfficeOpenXml.Style;
namespace Kreta.BusinessLogic.Helpers.Nyomtatvanyok.Excel
{
public class HianyzokEsJelenlevokSzama : BaseNyomtatvanyExcel
{
private readonly CellStyle OsztalyHeaderStyle;
private readonly CellStyle DarabSzamaHeaderStyle;
private readonly CellStyle StartSzamHeaderStyle;
private readonly CellStyle SzamHeaderStyle;
private readonly CellStyle EndSzamHeaderStyle;
private readonly CellStyle TopRightDoubleStyle;
private readonly CellStyle TopLeftDoubleStyle;
private readonly CellStyle TopDoubleStyle;
private readonly CellStyle LeftDoubleStyle;
private readonly CellStyle RightDoubleStyle;
private readonly CellStyle ThinStyle;
private const string Gray = "#AAAAAA";
public HianyzokEsJelenlevokSzama(IConnectionType connectionType) : base(connectionType)
{
StoredProcedure = "uspGetHianyzokEsJelenlevokSzama";
ExcelPackage = new ExcelPackage();
//Stílusok
NamedCellStyles.Add(nameof(OsztalyHeaderStyle), OsztalyHeaderStyle =
new CellStyle(nameof(OsztalyHeaderStyle))
{
FontBold = true,
RightBorder = ExcelBorderStyle.Double,
BottomBorder = ExcelBorderStyle.Double,
HorizontalAlignment = ExcelHorizontalAlignment.Center,
VerticalAlignment = ExcelVerticalAlignment.Center
});
NamedCellStyles.Add(nameof(DarabSzamaHeaderStyle), DarabSzamaHeaderStyle =
new CellStyle(nameof(DarabSzamaHeaderStyle))
{
RightBorder = ExcelBorderStyle.Double,
BottomBorder = ExcelBorderStyle.Thin,
LeftBorder = ExcelBorderStyle.Double,
TopBorder = ExcelBorderStyle.Thin,
HorizontalAlignment = ExcelHorizontalAlignment.Center
});
NamedCellStyles.Add(nameof(StartSzamHeaderStyle), StartSzamHeaderStyle =
new CellStyle(nameof(StartSzamHeaderStyle))
{
FontBold = true,
BgColorHex = Gray,
RightBorder = ExcelBorderStyle.Thin,
BottomBorder = ExcelBorderStyle.Double,
LeftBorder = ExcelBorderStyle.Double,
TopBorder = ExcelBorderStyle.Thin,
HorizontalAlignment = ExcelHorizontalAlignment.Center,
VerticalAlignment = ExcelVerticalAlignment.Center
});
NamedCellStyles.Add(nameof(SzamHeaderStyle), SzamHeaderStyle = new CellStyle(nameof(SzamHeaderStyle))
{
FontBold = true,
BgColorHex = Gray,
RightBorder = ExcelBorderStyle.Thin,
BottomBorder = ExcelBorderStyle.Double,
LeftBorder = ExcelBorderStyle.Thin,
TopBorder = ExcelBorderStyle.Thin,
HorizontalAlignment = ExcelHorizontalAlignment.Center,
VerticalAlignment = ExcelVerticalAlignment.Center
});
NamedCellStyles.Add(nameof(EndSzamHeaderStyle), EndSzamHeaderStyle =
new CellStyle(nameof(EndSzamHeaderStyle))
{
FontBold = true,
BgColorHex = Gray,
RightBorder = ExcelBorderStyle.Double,
BottomBorder = ExcelBorderStyle.Double,
LeftBorder = ExcelBorderStyle.Thin,
TopBorder = ExcelBorderStyle.Thin,
HorizontalAlignment = ExcelHorizontalAlignment.Center,
VerticalAlignment = ExcelVerticalAlignment.Center
});
NamedCellStyles.Add(nameof(TopRightDoubleStyle), TopRightDoubleStyle =
new CellStyle(nameof(TopRightDoubleStyle))
{
RightBorder = ExcelBorderStyle.Double,
BottomBorder = ExcelBorderStyle.Thin,
LeftBorder = ExcelBorderStyle.Thin,
TopBorder = ExcelBorderStyle.Double,
WrapText = true
});
NamedCellStyles.Add(nameof(TopLeftDoubleStyle), TopLeftDoubleStyle =
new CellStyle(nameof(TopLeftDoubleStyle))
{
RightBorder = ExcelBorderStyle.Thin,
BottomBorder = ExcelBorderStyle.Thin,
LeftBorder = ExcelBorderStyle.Double,
TopBorder = ExcelBorderStyle.Double
});
NamedCellStyles.Add(nameof(TopDoubleStyle), TopDoubleStyle = new CellStyle(nameof(TopDoubleStyle))
{
RightBorder = ExcelBorderStyle.Thin,
BottomBorder = ExcelBorderStyle.Thin,
LeftBorder = ExcelBorderStyle.Thin,
TopBorder = ExcelBorderStyle.Double
});
NamedCellStyles.Add(nameof(LeftDoubleStyle), LeftDoubleStyle = new CellStyle(nameof(LeftDoubleStyle))
{
RightBorder = ExcelBorderStyle.Thin,
BottomBorder = ExcelBorderStyle.Thin,
LeftBorder = ExcelBorderStyle.Double,
TopBorder = ExcelBorderStyle.Thin
});
NamedCellStyles.Add(nameof(RightDoubleStyle), RightDoubleStyle = new CellStyle(nameof(RightDoubleStyle))
{
RightBorder = ExcelBorderStyle.Double,
BottomBorder = ExcelBorderStyle.Thin,
LeftBorder = ExcelBorderStyle.Thin,
TopBorder = ExcelBorderStyle.Thin,
WrapText = true
});
NamedCellStyles.Add(nameof(ThinStyle), ThinStyle = new CellStyle(nameof(ThinStyle))
{
RightBorder = ExcelBorderStyle.Thin,
BottomBorder = ExcelBorderStyle.Thin,
LeftBorder = ExcelBorderStyle.Thin,
TopBorder = ExcelBorderStyle.Thin
});
//Munkalap tulajdonságok beállítása
SetWorkbookProperties();
}
public override void CreateSheet(Dictionary<string, string> dbParameters, int sheetIndex = 1)
{
DbParameters = dbParameters;
// Adatforrás összeállítása
SetExcelDataSet();
var table = DataSet.Tables[0];
var rows = table.Rows.Cast<DataRow>();
var groupByHonapRows = rows.GroupBy(g => g["Honap"])
.Select(s =>
new
{
Honap = SDAConvert.ToString(s.Key),
Data = s
}).ToList();
var index = 1;
foreach (var honapGroup in groupByHonapRows)
{
// Munkalap összeállítása
var worksheet = CreateSheet(honapGroup.Honap, index, eOrientation.Landscape, true, 2);
worksheet.Row(2).Height = 30;
worksheet.Column(1).Width = 35;
// headers
AddLabelRange(worksheet, 1, 1, 2, 1, ExcelNyomtatvanyResource.Osztaly, OsztalyHeaderStyle);
AddLabelRange(worksheet, 1, 2, 1, 32, ExcelNyomtatvanyResource.HianyzoTanulokSzama,
DarabSzamaHeaderStyle);
AddLabelRange(worksheet, 1, 2 + 31, 1, 32 + 31, ExcelNyomtatvanyResource.JelenlevoTanulokSzama,
DarabSzamaHeaderStyle);
// subheaders
for (var i = 1; i < 32; i++)
{
worksheet.Column(1 + i).Width = 6;
worksheet.Column(1 + i + 31).Width = 6;
AddLabelCell(worksheet, 2, 1 + i, i.ToString(),
i == 1 ? StartSzamHeaderStyle : i == 31 ? EndSzamHeaderStyle : SzamHeaderStyle);
AddLabelCell(worksheet, 2, 1 + i + 31, i.ToString(),
i == 1 ? StartSzamHeaderStyle : i == 31 ? EndSzamHeaderStyle : SzamHeaderStyle);
}
var rowIndex = 3;
var groupByOsztalyRows = honapGroup.Data.GroupBy(g => g["OsztalyNev"])
.Select(s =>
new
{
Osztaly = SDAConvert.ToString(s.Key),
Data = s
}).ToList();
foreach (var osztalyGroup in groupByOsztalyRows)
{
var firstRow = rowIndex == 3;
AddLabelCell(worksheet, rowIndex, 1, SDAConvert.ToString(osztalyGroup.Osztaly),
firstRow ? TopRightDoubleStyle : RightDoubleStyle);
for (var j = 1; j < 32; j++)
{
var value = osztalyGroup.Data.Where(x =>
SDAConvert.ToBooleanFromTF(x["IsTanitasiNap"]) && SDAConvert.ToInt32(x["Nap"]) == j)
.Select(s =>
new
{
Hianyzas = SDAConvert.ToNullableInt32(s["Hianyzas"]),
Letszam = SDAConvert.ToNullableInt32(s["Letszam"])
}).FirstOrDefault();
var hianyzo = value?.Letszam == null ? null : value.Hianyzas;
var jelenlevo = hianyzo == null
? null
: value.Letszam - hianyzo;
var style = j == 1
? firstRow ? TopLeftDoubleStyle : LeftDoubleStyle
: j == 31
? firstRow ? TopRightDoubleStyle : RightDoubleStyle
: firstRow
? TopDoubleStyle
: ThinStyle;
AddNumberCell(worksheet, rowIndex, 1 + j, hianyzo, style);
AddNumberCell(worksheet, rowIndex, 1 + j + 31, jelenlevo, style);
}
rowIndex++;
}
AddLabelCell(worksheet, rowIndex, 1, AdatszolgaltatasokResource.Osszesen, RightDoubleStyle);
// szumma
for (var k = 1; k < 32; k++)
{
var style = k == 1
? LeftDoubleStyle
: k == 31
? RightDoubleStyle
: ThinStyle;
AddAggregateFormulaCell(worksheet, rowIndex, 1 + k, "SUM", 3, 1 + k, rowIndex - 1, 1 + k, true,
style);
AddAggregateFormulaCell(worksheet, rowIndex, 1 + k + 31, "SUM", 3, 1 + k + 31, rowIndex - 1,
1 + k + 31, true, style);
}
index++;
}
}
}
}