-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReportGenerator.cs
More file actions
132 lines (109 loc) · 4.68 KB
/
ReportGenerator.cs
File metadata and controls
132 lines (109 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
namespace ReportGenerator;
using System.Data;
using System.Globalization;
using ClosedXML.Excel;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
/// <summary>Represents a report generator.</summary>
internal class ReportGenerator
{
private const string AccountingFormat = "_-* #,##0.00\\ \"€\"_-;\\-* #,##0.00\\ \"€\"_-;_-* \"-\"??\\ \"€\"_-;_-@_-";
private const string DateFormat = "dd.MM.yyyy";
private const string ConnectionName = "SqlConnection";
private static IConfigurationRoot? _configuration;
#region Methods
/// <summary>Generates reports based on the provided report definitions.</summary>
/// <param name="reports">The list of report definitions.</param>
public void GenerateReport(List<ReportDefinition> reports)
{
foreach (var report in reports)
{
Console.WriteLine($"Starting generation of '{report.FilePath}'");
using var workbook = new XLWorkbook();
foreach (var page in report.Pages)
{
Console.WriteLine($"\tAdding page {page.Name}");
Console.WriteLine("\t\tFetching data...");
var data = GetData(page.Sql);
Console.WriteLine("\t\tAdding worksheet...");
AddSheet(workbook, data, page);
}
Console.WriteLine("\tSaving to file...");
workbook.SaveAs(report.FilePath);
Console.WriteLine("\tDone");
}
}
private static void AddSheet(IXLWorkbook workbook, DataTable dataTable, ReportPage page)
{
var worksheet = workbook.Worksheets.Add(page.Name);
// Add column headers
for (var columnIndex = 0; columnIndex < dataTable.Columns.Count; columnIndex++)
worksheet.Cell(1, columnIndex + 1).Value = dataTable.Columns[columnIndex].ColumnName;
// Add data rows
for (var rowIndex = 0; rowIndex < dataTable.Rows.Count; rowIndex++)
for (var columnIndex = 0; columnIndex < dataTable.Columns.Count; columnIndex++)
{
var currentCell = worksheet.Cell(rowIndex + 2, columnIndex + 1);
var dataValue = dataTable.Rows[rowIndex][columnIndex];
page.SpecialColumns.TryGetValue(dataTable.Columns[columnIndex].ColumnName, out var columnType);
SetCellValue(columnType, currentCell, dataValue);
SetCellType(columnType, currentCell);
}
// Format as table
var firstCell = worksheet.FirstCellUsed();
var lastCell = worksheet.LastCellUsed();
var range = worksheet.Range(firstCell.Address, lastCell.Address);
var table = range.CreateTable();
table.Theme = XLTableTheme.TableStyleMedium21;
worksheet.Columns().AdjustToContents();
}
private static void SetCellValue(ColumnType columnType, IXLCell currentCell, object dataValue)
{
switch (columnType)
{
case ColumnType.Accounting:
var numberFormat = new NumberFormatInfo { NumberDecimalSeparator = ",", NumberGroupSeparator = "." };
currentCell.Value = XLCellValue.FromObject(dataValue, numberFormat);
break;
case ColumnType.Date:
var value = dataValue.ToString();
currentCell.SetValue(string.IsNullOrEmpty(value) ? (DateTime?)null : DateTime.Parse(value));
break;
case ColumnType.Standard:
currentCell.Value = XLCellValue.FromObject(dataValue);
break;
default:
throw new ArgumentOutOfRangeException(nameof(columnType));
}
}
private static void SetCellType(ColumnType columnType, IXLCell currentCell)
{
switch (columnType)
{
case ColumnType.Accounting:
currentCell.Style.NumberFormat.Format = AccountingFormat;
break;
case ColumnType.Date:
currentCell.Style.DateFormat.SetFormat(DateFormat);
break;
case ColumnType.Standard:
break;
default:
throw new ArgumentOutOfRangeException(nameof(columnType));
}
}
private static DataTable GetData(string sql)
{
_configuration ??= new ConfigurationBuilder()
.AddUserSecrets<ReportGenerator>()
.Build();
using var connection = new SqlConnection(_configuration.GetConnectionString(ConnectionName));
connection.Open();
using var command = new SqlCommand(sql, connection);
using var adapter = new SqlDataAdapter(command);
var dataTable = new DataTable();
adapter.Fill(dataTable);
return dataTable;
}
#endregion
}