Black Rock Reporting Azure Function
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ReportGenerator.cs 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using GemBox.Spreadsheet;
  7. using BlackRockReportFunction.Models;
  8. using BlackRockReportFunction.Helpers;
  9. using System.Windows.Forms;
  10. namespace BlackRockReportFunction.Bussines
  11. {
  12. internal class ReportGenerator
  13. {
  14. public static async Task<ExcelFile> GenerateReportContent()
  15. {
  16. string licenseKey = "E0YU-JKLB-WFCE-N52P";
  17. SpreadsheetInfo.SetLicense(licenseKey);
  18. var testObject = new ClockifyReport
  19. {
  20. reportName = "BlackRockReport_20220615",
  21. reportDescription = "Total (13/06/2022 - 15/06/2022)",
  22. reportPeople = new List<Person>
  23. {
  24. new Person
  25. {
  26. fullName = "Nikola Jovanovic",
  27. records = new List<ClockifyRecord>
  28. {
  29. new ClockifyRecord
  30. {
  31. recordDescription = "Description1",
  32. recordTime = new TimeOnly(3,15,44),
  33. amount = 200
  34. },
  35. new ClockifyRecord
  36. {
  37. recordDescription = "Description1",
  38. recordTime = new TimeOnly(3,15,44),
  39. amount = 150
  40. }
  41. }
  42. },
  43. new Person
  44. {
  45. fullName = "Boris Stevanovic",
  46. records = new List<ClockifyRecord>
  47. {
  48. new ClockifyRecord
  49. {
  50. recordDescription = "Description1",
  51. recordTime = new TimeOnly(3,15,44),
  52. amount = 300
  53. },
  54. new ClockifyRecord
  55. {
  56. recordDescription = "Description1",
  57. recordTime = new TimeOnly(3,15,44),
  58. amount = 100
  59. },
  60. new ClockifyRecord
  61. {
  62. recordDescription = "Description1",
  63. recordTime = new TimeOnly(3,15,44),
  64. amount = 120
  65. }
  66. }
  67. },
  68. new Person
  69. {
  70. fullName = "Dunja Stevanovic",
  71. records = new List<ClockifyRecord>
  72. {
  73. new ClockifyRecord
  74. {
  75. recordDescription = "Description1",
  76. recordTime = new TimeOnly(3,15,44),
  77. amount = 50
  78. },
  79. new ClockifyRecord
  80. {
  81. recordDescription = "Description1",
  82. recordTime = new TimeOnly(3,15,44),
  83. amount = 500
  84. }
  85. }
  86. }
  87. }
  88. };
  89. var excelFile = new ExcelFile();
  90. var ws = excelFile.Worksheets.Add("Content");
  91. var sectionStyle = new CellStyle
  92. {
  93. Font =
  94. {
  95. Weight = ExcelFont.BoldWeight,
  96. Size = 250,
  97. },
  98. VerticalAlignment = VerticalAlignmentStyle.Center,
  99. HorizontalAlignment = HorizontalAlignmentStyle.Center,
  100. };
  101. var mainDetailsStyle = new CellStyle
  102. {
  103. Font =
  104. {
  105. Weight = ExcelFont.BoldWeight,
  106. Size = 200,
  107. }
  108. };
  109. await AddReportItems(testObject, ws, sectionStyle, mainDetailsStyle);
  110. // Autofit
  111. excelFile = await AutoFitReport(excelFile);
  112. return excelFile;
  113. }
  114. public static async Task AddReportItems(ClockifyReport reportObject, ExcelWorksheet ws, CellStyle sectionStyle, CellStyle mainDetailsStyle)
  115. {
  116. int row = 0;
  117. string[] sectionNames = { "User", "Description", "Time (h)", "Time (decimal)", "Amount (USD)" };
  118. decimal totalAmountSum = 0;
  119. for (int i = 0; i < sectionNames.Length; i++)
  120. {
  121. ws.Cells[row, i].Style = sectionStyle;
  122. ws.Cells[row, i].Value = sectionNames[i];
  123. ws.Cells[row, i].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Medium;
  124. ws.Cells[row, i].Style.Borders[IndividualBorder.Bottom].LineStyle = LineStyle.Thick;
  125. }
  126. row++;
  127. foreach (var reportPerson in reportObject.reportPeople)
  128. {
  129. ws.Cells[row, 0].Style = mainDetailsStyle;
  130. ws.Cells[row, 0].Value = reportPerson.fullName;
  131. var sumOfRecordHours = Formaters.getSumOfRecordTimes(reportPerson.records.Select(record => record.recordTime).ToList());
  132. ws.Cells[row, 2].Style = mainDetailsStyle;
  133. ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  134. ws.Cells[row, 2].Value = sumOfRecordHours;
  135. ws.Cells[row, 3].Style = mainDetailsStyle;
  136. ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  137. ws.Cells[row, 3].Value = Formaters.getDecimalHours(sumOfRecordHours);
  138. ws.Cells[row, 4].Style = mainDetailsStyle;
  139. ws.Cells[row, 4].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  140. ws.Cells[row, 4].Value = string.Format("{0} USD", reportPerson.records.Sum(record => record.amount).ToString("0.00"));
  141. totalAmountSum += reportPerson.records.Sum(record => record.amount);
  142. row++;
  143. foreach (var personRecord in reportPerson.records)
  144. {
  145. ws.Cells[row, 1].Value = personRecord.recordDescription;
  146. ws.Cells[row, 2].Value = Formaters.getRecordTime(personRecord.recordTime);
  147. ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  148. ws.Cells[row, 3].Value = Formaters.getDecimalHours(personRecord.recordTime);
  149. ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  150. ws.Cells[row, 4].Value = string.Format("{0} USD", personRecord.amount.ToString("0.00"));
  151. ws.Cells[row, 4].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  152. row++;
  153. }
  154. }
  155. ws.Cells.GetSubrangeAbsolute(row, 0, row, 1).Merged = true;
  156. ws.Cells.GetSubrangeAbsolute(row, 0, row, 1).Style = mainDetailsStyle;
  157. ws.Cells.GetSubrangeAbsolute(row, 0, row, 1).Value = reportObject.reportDescription;
  158. var totalSum = Formaters.getTotalSum(
  159. reportObject.reportPeople.Select(
  160. person => Formaters.getSumOfRecordTimes(
  161. person.records.Select(record => record.recordTime).ToList()
  162. )
  163. ).ToList()
  164. );
  165. ws.Cells[row, 2].Style = mainDetailsStyle;
  166. ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  167. ws.Cells[row, 2].Value = totalSum;
  168. ws.Cells[row, 3].Style = mainDetailsStyle;
  169. ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  170. ws.Cells[row, 3].Value = Formaters.getDecimalHours(totalSum);
  171. ws.Cells[row, 4].Style = mainDetailsStyle;
  172. ws.Cells[row, 4].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  173. ws.Cells[row, 4].Value = string.Format("{0} USD", totalAmountSum.ToString("0.00"));
  174. }
  175. public static async Task<ExcelFile> AutoFitReport(ExcelFile excelFile)
  176. {
  177. var localFile = excelFile;
  178. foreach (var sheet in localFile.Worksheets)
  179. {
  180. //var columnCount = sheet.CalculateMaxUsedColumns() - 1;
  181. //for (int i = 0; i < columnCount; i++)
  182. //{
  183. // sheet.Columns[i].AutoFit(1, sheet.Rows[0], sheet.Rows[sheet.Rows.Count - 1]);
  184. //}
  185. }
  186. return localFile;
  187. }
  188. }
  189. }