Black Rock Reporting Azure Function
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ReportGenerator.cs 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using GemBox.Spreadsheet;
  2. using BlackRockReportFunction.Models;
  3. using BlackRockReportFunction.Helpers;
  4. namespace BlackRockReportFunction.Bussines
  5. {
  6. internal class ReportGenerator
  7. {
  8. public const string DefaultFontName = "Calibri";
  9. public const int DefaultFontSize = 20;
  10. public static ExcelFile GenerateReportContent(ClockifyReport reportObject)
  11. {
  12. string licenseKey = Environment.GetEnvironmentVariable("GemBoxLicenseKey");
  13. SpreadsheetInfo.SetLicense(licenseKey);
  14. var excelFile = new ExcelFile();
  15. var ws = excelFile.Worksheets.Add("Content");
  16. var sectionStyle = new CellStyle
  17. {
  18. Font =
  19. {
  20. Name = DefaultFontName,
  21. Weight = ExcelFont.BoldWeight,
  22. Size = DefaultFontSize * 12,
  23. },
  24. VerticalAlignment = VerticalAlignmentStyle.Center,
  25. HorizontalAlignment = HorizontalAlignmentStyle.Left,
  26. };
  27. var mainDetailsStyle = new CellStyle
  28. {
  29. Font =
  30. {
  31. Name = DefaultFontName,
  32. Weight = ExcelFont.BoldWeight,
  33. Size = DefaultFontSize * 11,
  34. },
  35. VerticalAlignment = VerticalAlignmentStyle.Center,
  36. };
  37. var normalDetailsStyle = new CellStyle
  38. {
  39. Font =
  40. {
  41. Name = DefaultFontName,
  42. Weight = ExcelFont.NormalWeight,
  43. Size = DefaultFontSize * 10,
  44. },
  45. VerticalAlignment = VerticalAlignmentStyle.Center,
  46. };
  47. AddReportItems(reportObject, ws, sectionStyle, mainDetailsStyle, normalDetailsStyle);
  48. // Autofit
  49. AutoFitReport(excelFile);
  50. return excelFile;
  51. }
  52. public static void AddReportItems(ClockifyReport reportObject, ExcelWorksheet ws, CellStyle sectionStyle, CellStyle mainDetailsStyle, CellStyle normalDetailsStyle)
  53. {
  54. int row = 0;
  55. string[] sectionNames = { "User ", "Description ", "Time (h) ", "Time (decimal) ", "Amount (USD) " };
  56. decimal totalAmountSum = 0;
  57. List<string> usersDecimalHours = new List<string>();
  58. for (int i = 0; i < sectionNames.Length; i++)
  59. {
  60. ws.Cells[row, i].Style = sectionStyle;
  61. ws.Cells[row, i].Value = sectionNames[i];
  62. ws.Cells[row, i].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  63. ws.Cells[row, i].Style.Borders[IndividualBorder.Bottom].LineStyle = LineStyle.Thick;
  64. ws.Cells[row, i].Style.Borders[IndividualBorder.Bottom].LineColor = System.Drawing.Color.LightGray;
  65. }
  66. row++;
  67. foreach (var reportPerson in reportObject.reportPeople)
  68. {
  69. ws.Cells[row, 0].Style = mainDetailsStyle;
  70. ws.Cells[row, 0].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  71. ws.Cells[row, 0].Value = reportPerson.fullName;
  72. ws.Cells[row, 1].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  73. var sumOfRecordHours = Formaters.getSumOfRecordTimes(reportPerson.records.Select(record => record.recordTime).ToList());
  74. ws.Cells[row, 2].Style = mainDetailsStyle;
  75. ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  76. ws.Cells[row, 2].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  77. ws.Cells[row, 2].Value = sumOfRecordHours;
  78. ws.Cells[row, 3].Style = mainDetailsStyle;
  79. ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  80. ws.Cells[row, 3].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  81. var userDecimalHours = Formaters.getDecimalHours(sumOfRecordHours);
  82. ws.Cells[row, 3].Value = userDecimalHours;
  83. usersDecimalHours.Add(userDecimalHours);
  84. ws.Cells[row, 4].Style = mainDetailsStyle;
  85. ws.Cells[row, 4].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  86. ws.Cells[row, 4].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  87. ws.Cells[row, 4].Value = string.Format("{0} USD", reportPerson.records.Sum(record => record.amount).ToString("0.00"));
  88. totalAmountSum += reportPerson.records.Sum(record => record.amount);
  89. row++;
  90. foreach (var personRecord in reportPerson.records)
  91. {
  92. ws.Cells[row, 0].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  93. ws.Cells[row, 1].Style = normalDetailsStyle;
  94. ws.Cells[row, 1].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  95. ws.Cells[row, 1].Value = personRecord.recordDescription;
  96. ws.Cells[row, 2].Style = normalDetailsStyle;
  97. ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  98. ws.Cells[row, 2].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  99. ws.Cells[row, 2].Value = Formaters.getRecordTime(personRecord.recordTime);
  100. ws.Cells[row, 3].Style = normalDetailsStyle;
  101. ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  102. ws.Cells[row, 3].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  103. ws.Cells[row, 3].Value = Formaters.getDecimalHours(personRecord.recordTime);
  104. ws.Cells[row, 4].Style = normalDetailsStyle;
  105. ws.Cells[row, 4].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  106. ws.Cells[row, 4].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  107. ws.Cells[row, 4].Value = string.Format("{0} USD", personRecord.amount.ToString("0.00"));
  108. row++;
  109. }
  110. }
  111. ws.Cells.GetSubrangeAbsolute(row, 0, row, 1).Merged = true;
  112. ws.Cells.GetSubrangeAbsolute(row, 0, row, 1).Style = mainDetailsStyle;
  113. ws.Cells.GetSubrangeAbsolute(row, 0, row, 1).Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  114. ws.Cells.GetSubrangeAbsolute(row, 0, row, 1).Value = reportObject.reportDescription;
  115. var totalSum = Formaters.getTotalSum(
  116. reportObject.reportPeople.Select(
  117. person => Formaters.getSumOfRecordTimes(
  118. person.records.Select(record => record.recordTime).ToList()
  119. )
  120. ).ToList()
  121. );
  122. ws.Cells[row, 2].Style = mainDetailsStyle;
  123. ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  124. ws.Cells[row, 2].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  125. ws.Cells[row, 2].Value = totalSum;
  126. ws.Cells[row, 3].Style = mainDetailsStyle;
  127. ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  128. ws.Cells[row, 3].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  129. ws.Cells[row, 3].Value = Formaters.getTotalDecimalHours(usersDecimalHours);
  130. ws.Cells[row, 4].Style = mainDetailsStyle;
  131. ws.Cells[row, 4].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  132. ws.Cells[row, 4].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  133. ws.Cells[row, 4].Value = string.Format("{0} USD", totalAmountSum.ToString("0.00"));
  134. }
  135. public static void AutoFitReport(ExcelFile excelFile)
  136. {
  137. foreach (var sheet in excelFile.Worksheets)
  138. {
  139. var columnCount = sheet.CalculateMaxUsedColumns();
  140. for (int i = 0; i < columnCount; i++)
  141. {
  142. sheet.Columns[i].AutoFit(1, sheet.Rows[0], sheet.Rows[sheet.Rows.Count - 1]);
  143. }
  144. }
  145. }
  146. }
  147. }