| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- using GemBox.Spreadsheet;
- using BlackRockReportFunction.Models;
- using BlackRockReportFunction.Helpers;
-
- namespace BlackRockReportFunction.Bussines
- {
- internal class ReportGenerator
- {
- public static async Task<ExcelFile> GenerateReportContent()
- {
- string licenseKey = Environment.GetEnvironmentVariable("GemBoxLicenseKey");
- SpreadsheetInfo.SetLicense(licenseKey);
-
- var testObject = new ClockifyReport
- {
- reportName = "BlackRockReport_20220615",
- reportDescription = "Total (13/06/2022 - 15/06/2022)",
- reportPeople = new List<Person>
- {
- new Person
- {
- fullName = "Nikola Jovanovic",
- records = new List<ClockifyRecord>
- {
- new ClockifyRecord
- {
- recordDescription = "Description1",
- recordTime = new TimeOnly(3,15,44),
- amount = 200
- },
- new ClockifyRecord
- {
- recordDescription = "Description1",
- recordTime = new TimeOnly(3,15,44),
- amount = 150
- }
- }
- },
- new Person
- {
- fullName = "Boris Stevanovic",
- records = new List<ClockifyRecord>
- {
- new ClockifyRecord
- {
- recordDescription = "Description1",
- recordTime = new TimeOnly(3,15,44),
- amount = 300
- },
- new ClockifyRecord
- {
- recordDescription = "Description1",
- recordTime = new TimeOnly(3,15,44),
- amount = 100
- },
- new ClockifyRecord
- {
- recordDescription = "Description1",
- recordTime = new TimeOnly(3,15,44),
- amount = 120
- }
- }
- },
- new Person
- {
- fullName = "Dunja Stevanovic",
- records = new List<ClockifyRecord>
- {
- new ClockifyRecord
- {
- recordDescription = "Description1",
- recordTime = new TimeOnly(3,15,44),
- amount = 50
- },
- new ClockifyRecord
- {
- recordDescription = "Description1",
- recordTime = new TimeOnly(3,15,44),
- amount = 500
- }
- }
- }
- }
- };
-
- var excelFile = new ExcelFile();
- var ws = excelFile.Worksheets.Add("Content");
-
- var sectionStyle = new CellStyle
- {
- Font =
- {
- Weight = ExcelFont.BoldWeight,
- Size = 250,
- },
- VerticalAlignment = VerticalAlignmentStyle.Center,
- HorizontalAlignment = HorizontalAlignmentStyle.Center,
- };
-
- var mainDetailsStyle = new CellStyle
- {
- Font =
- {
- Weight = ExcelFont.BoldWeight,
- Size = 200,
- }
- };
-
- await AddReportItems(testObject, ws, sectionStyle, mainDetailsStyle);
-
- // Autofit
- excelFile = await AutoFitReport(excelFile);
-
- return excelFile;
- }
-
- public static async Task AddReportItems(ClockifyReport reportObject, ExcelWorksheet ws, CellStyle sectionStyle, CellStyle mainDetailsStyle)
- {
- int row = 0;
- string[] sectionNames = { "User", "Description", "Time (h)", "Time (decimal)", "Amount (USD)" };
- decimal totalAmountSum = 0;
-
- for (int i = 0; i < sectionNames.Length; i++)
- {
- ws.Cells[row, i].Style = sectionStyle;
- ws.Cells[row, i].Value = sectionNames[i];
- ws.Cells[row, i].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Medium;
- ws.Cells[row, i].Style.Borders[IndividualBorder.Bottom].LineStyle = LineStyle.Thick;
- }
-
- row++;
-
- foreach (var reportPerson in reportObject.reportPeople)
- {
- ws.Cells[row, 0].Style = mainDetailsStyle;
- ws.Cells[row, 0].Value = reportPerson.fullName;
-
- var sumOfRecordHours = Formaters.getSumOfRecordTimes(reportPerson.records.Select(record => record.recordTime).ToList());
- ws.Cells[row, 2].Style = mainDetailsStyle;
- ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
- ws.Cells[row, 2].Value = sumOfRecordHours;
-
- ws.Cells[row, 3].Style = mainDetailsStyle;
- ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
- ws.Cells[row, 3].Value = Formaters.getDecimalHours(sumOfRecordHours);
-
- ws.Cells[row, 4].Style = mainDetailsStyle;
- ws.Cells[row, 4].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
- ws.Cells[row, 4].Value = string.Format("{0} USD", reportPerson.records.Sum(record => record.amount).ToString("0.00"));
- totalAmountSum += reportPerson.records.Sum(record => record.amount);
-
- row++;
-
- foreach (var personRecord in reportPerson.records)
- {
- ws.Cells[row, 1].Value = personRecord.recordDescription;
-
- ws.Cells[row, 2].Value = Formaters.getRecordTime(personRecord.recordTime);
- ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
-
- ws.Cells[row, 3].Value = Formaters.getDecimalHours(personRecord.recordTime);
- ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
-
- ws.Cells[row, 4].Value = string.Format("{0} USD", personRecord.amount.ToString("0.00"));
- ws.Cells[row, 4].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
-
- row++;
- }
- }
-
- ws.Cells.GetSubrangeAbsolute(row, 0, row, 1).Merged = true;
- ws.Cells.GetSubrangeAbsolute(row, 0, row, 1).Style = mainDetailsStyle;
- ws.Cells.GetSubrangeAbsolute(row, 0, row, 1).Value = reportObject.reportDescription;
-
- var totalSum = Formaters.getTotalSum(
- reportObject.reportPeople.Select(
- person => Formaters.getSumOfRecordTimes(
- person.records.Select(record => record.recordTime).ToList()
- )
- ).ToList()
- );
-
- ws.Cells[row, 2].Style = mainDetailsStyle;
- ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
- ws.Cells[row, 2].Value = totalSum;
-
- ws.Cells[row, 3].Style = mainDetailsStyle;
- ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
- ws.Cells[row, 3].Value = Formaters.getDecimalHours(totalSum);
-
- ws.Cells[row, 4].Style = mainDetailsStyle;
- ws.Cells[row, 4].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
- ws.Cells[row, 4].Value = string.Format("{0} USD", totalAmountSum.ToString("0.00"));
- }
-
- public static async Task<ExcelFile> AutoFitReport(ExcelFile excelFile)
- {
- var localFile = excelFile;
-
- foreach (var sheet in localFile.Worksheets)
- {
- var columnCount = sheet.CalculateMaxUsedColumns();
-
- for (int i = 0; i < columnCount; i++)
- {
- sheet.Columns[i].AutoFit(1, sheet.Rows[0], sheet.Rows[sheet.Rows.Count - 1]);
- }
- }
-
- return localFile;
- }
- }
- }
|