Coursach/Course/BusinessLogic/OfficePackage/AbstractSaveToExcelImplementer.cs

96 lines
3.0 KiB
C#
Raw Normal View History

2024-05-28 19:21:01 +04:00
using BusinessLogic.OfficePackage.HelperEnums;
using BusinessLogic.OfficePackage.HelperModels;
namespace BusinessLogic.OfficePackage
{
public abstract class AbstractSaveToExcelImplementer
{
public void CreateReport(ExcelInfoImplementer info)
{
CreateExcel(info);
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "A",
RowIndex = 1,
Text = info.Title,
StyleInfo = ExcelStyleInfoType.Title
});
MergeCells(new ExcelMergeParameters
{
CellFromName = "A1",
CellToName = "C1"
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "A",
RowIndex = 2,
Text = "Детали",
StyleInfo = ExcelStyleInfoType.Title
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "B",
RowIndex = 2,
Text = "Цеха",
StyleInfo = ExcelStyleInfoType.Title
});
MergeCells(new ExcelMergeParameters
{
CellFromName = "B2",
CellToName = ColumnLetter(info.maxleng + 1) + "2"
});
uint rowIndex = 3;
foreach (var pc in info.detailWorkshops)
{
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "A",
RowIndex = rowIndex,
Text = pc.DetailName,
StyleInfo = ExcelStyleInfoType.Text
});
int place = 2;
foreach (var workshop in pc.WorkShops)
{
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = ColumnLetter(place),
RowIndex = rowIndex,
Text = workshop,
StyleInfo = ExcelStyleInfoType.TextWithBorder
});
place++;
}
rowIndex++;
}
SaveExcel(info);
}
private static string ColumnLetter(int columnIndex)
{
int dividend = columnIndex;
string columnName = String.Empty;
int modulo;
while (dividend > 0)
{
modulo = (dividend - 1) % 26;
columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
dividend = (dividend - modulo) / 26;
}
return columnName;
}
protected abstract void CreateExcel(ExcelInfoImplementer info);
protected abstract void InsertCellInWorksheet(ExcelCellParameters excelParams);
protected abstract void MergeCells(ExcelMergeParameters excelParams);
protected abstract void SaveExcel(ExcelInfoImplementer info);
}
}