118 lines
4.3 KiB
C#
118 lines
4.3 KiB
C#
using System;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using Kreta.BusinessLogic.Classes;
|
|
using OfficeOpenXml;
|
|
using OfficeOpenXml.Style;
|
|
|
|
namespace Kreta.BusinessLogic.Logic
|
|
{
|
|
public static class ExportLogic
|
|
{
|
|
/// TODO devKornél(enhancement): Ha nem fog ellátni plusz funkciót, akkor csinálni egy optimálisabbat, ami csak oszlopot nevez át és töröl
|
|
public static DataTable MapDataTable(DataTable fromTable, (string nameFrom, string nameTo, Type type)[] map)
|
|
{
|
|
var toTable = new DataTable();
|
|
foreach (var (_, nameTo, type) in map)
|
|
{
|
|
toTable.Columns.Add(nameTo, type);
|
|
}
|
|
|
|
foreach (var item in fromTable.AsEnumerable())
|
|
{
|
|
toTable.Rows.Add(GetDataRow(item));
|
|
}
|
|
|
|
TryCreateHeaderIfNotExist(toTable);
|
|
|
|
return toTable;
|
|
|
|
DataRow GetDataRow(DataRow fromRow)
|
|
{
|
|
var toRow = toTable.NewRow();
|
|
foreach (var (nameFrom, nameTo, type) in map)
|
|
{
|
|
toRow[nameTo] = Convert.ChangeType(fromRow[nameFrom], type);
|
|
}
|
|
|
|
return toRow;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Kicserélésre kerül az oszlopsorrend, beállításra kerül az oszlop típusa és átneveződik az oszlop neve a 'map' paraméternek megfelelően.
|
|
/// Végül, remove-olva lesznek a sorrendezésből kimaradt ('map'-ben nem szereplő) oszlopok.
|
|
/// </summary>
|
|
/// <param name="dataTable"></param>
|
|
/// <param name="map"></param>
|
|
/// <param name="removeUnusedColumns"></param>
|
|
public static void MapDataTable(DataTable dataTable, (string fromColumn, string toColumn, Type type)[] map, bool removeUnusedColumns)
|
|
{
|
|
for (var i = 0; i < map.Length; i++)
|
|
{
|
|
dataTable.Columns[map[i].fromColumn].SetOrdinal(i);
|
|
dataTable.Columns[map[i].fromColumn].DataType = map[i].type;
|
|
dataTable.Columns[map[i].fromColumn].ColumnName = map[i].toColumn; // csak a végén nevezhető át!
|
|
}
|
|
|
|
if (removeUnusedColumns)
|
|
{
|
|
for (var i = dataTable.Columns.Count - 1; i >= map.Length; i--)
|
|
{
|
|
dataTable.Columns.RemoveAt(i);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static bool TryCreateHeaderIfNotExist(DataTable table)
|
|
{
|
|
if (table.Rows.Count < 1)
|
|
{
|
|
table.Rows.Add(table.NewRow());
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dátum oszlopok megjelenítésének korrigálása, már az export fájlnál.
|
|
/// </summary>
|
|
/// <param name="exportColumns"></param>
|
|
/// <param name="workSheet"></param>
|
|
public static void FormatDateFields((string fromColumn, string toColumn, Type type)[] exportColumns, ExcelWorksheet workSheet)
|
|
{
|
|
for (int i = 0; i < exportColumns.Length; i++)
|
|
{
|
|
if (typeof(DateTime).Equals(exportColumns[i].type))
|
|
{
|
|
workSheet.Column(i + 1).Style.Numberformat.Format = SDAFormat.ShortDate;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void SetHeaderStyles(ExcelRange range)
|
|
{
|
|
range.Style.Fill.PatternType = ExcelFillStyle.Solid;
|
|
range.Style.Fill.BackgroundColor.SetColor(Core.Constants.Export.HeaderColor);
|
|
range.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
|
|
range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
|
|
range.Style.Font.Bold = Core.Constants.Export.HeaderIsBold;
|
|
}
|
|
|
|
public static void SetRowColorAlternate(ExcelWorksheet worksheet, int rowCount)
|
|
{
|
|
//NOTE: minden masodik sort szürkével jelöljük az excelben
|
|
for (var i = 1; i <= rowCount + 1; i++)
|
|
{
|
|
if (i % 2 == 0)
|
|
{
|
|
worksheet.Row(i).Style.Fill.PatternType = ExcelFillStyle.Solid;
|
|
worksheet.Row(i).Style.Fill.BackgroundColor.SetColor(Color.LightGray);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|