using GemBox.Spreadsheet; using BlackRockReportFunction.Models; using BlackRockReportFunction.Helpers; namespace BlackRockReportFunction.Bussines { internal class ReportGenerator { public static async Task 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 { new Person { fullName = "Nikola Jovanovic", records = new List { 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 { 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 { 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 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; } } }