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 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using GemBox.Spreadsheet;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using ClockifyReport.Models;
  6. namespace BlackRockReportFunction.Bussines
  7. {
  8. internal class ReportGenerator
  9. {
  10. public const string DefaultFontName = "DejaVu Sans";
  11. public const int DefaultFontSize = 20;
  12. public static ExcelFile GenerateReportContent(Root reportObject)
  13. {
  14. string licenseKey = Environment.GetEnvironmentVariable("GemBoxLicenseKey");
  15. SpreadsheetInfo.SetLicense(licenseKey);
  16. var excelFile = new ExcelFile();
  17. var ws = excelFile.Worksheets.Add("Content");
  18. var sectionStyle = new CellStyle
  19. {
  20. Font =
  21. {
  22. Name = DefaultFontName,
  23. Weight = ExcelFont.BoldWeight,
  24. Size = DefaultFontSize * 12,
  25. },
  26. VerticalAlignment = VerticalAlignmentStyle.Center,
  27. HorizontalAlignment = HorizontalAlignmentStyle.Left,
  28. };
  29. var mainDetailsStyle = new CellStyle
  30. {
  31. Font =
  32. {
  33. Name = DefaultFontName,
  34. Weight = ExcelFont.BoldWeight,
  35. Size = DefaultFontSize * 10,
  36. },
  37. VerticalAlignment = VerticalAlignmentStyle.Center,
  38. };
  39. var normalDetailsStyle = new CellStyle
  40. {
  41. Font =
  42. {
  43. Name = DefaultFontName,
  44. Weight = ExcelFont.NormalWeight,
  45. Size = DefaultFontSize * 10,
  46. },
  47. VerticalAlignment = VerticalAlignmentStyle.Center,
  48. };
  49. AddReportItems(reportObject, ws, sectionStyle, mainDetailsStyle, normalDetailsStyle);
  50. // Autofit
  51. AutoFitReport(excelFile);
  52. return excelFile;
  53. }
  54. public static void AddReportItems(Root reportObject, ExcelWorksheet ws, CellStyle sectionStyle, CellStyle mainDetailsStyle, CellStyle normalDetailsStyle)
  55. {
  56. TimeSpan t, time, time1;
  57. int row = 0;
  58. string[] sectionNames = { "User ", "Description ", "Time (h) ", "Time (decimal) " };
  59. decimal totalAmountSum = 0;
  60. List<string> usersDecimalHours = new List<string>();
  61. for (int i = 0; i < sectionNames.Length; i++)
  62. {
  63. ws.Cells[row, i].Style = sectionStyle;
  64. ws.Cells[row, i].Value = sectionNames[i];
  65. ws.Cells[row, i].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  66. ws.Cells[row, i].Style.Borders[IndividualBorder.Bottom].LineStyle = LineStyle.Thick;
  67. ws.Cells[row, i].Style.Borders[IndividualBorder.Bottom].LineColor = System.Drawing.Color.LightGray;
  68. }
  69. row++;
  70. foreach (var reportPerson in reportObject.groupOne)
  71. {
  72. ws.Cells[row, 0].Style = mainDetailsStyle;
  73. ws.Cells[row, 0].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  74. ws.Cells[row, 0].Value = reportPerson.name;
  75. ws.Cells[row, 1].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  76. time = TimeSpan.FromSeconds(reportPerson.duration);
  77. var sumOfRecordHours = string.Format("{0:00}:{1:D2}:{2:D2}", Math.Floor(time.TotalHours), time.Minutes, time.Seconds);
  78. ws.Cells[row, 2].Style = mainDetailsStyle;
  79. ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  80. ws.Cells[row, 2].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  81. ws.Cells[row, 2].Value = sumOfRecordHours;
  82. ws.Cells[row, 3].Style = mainDetailsStyle;
  83. ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  84. ws.Cells[row, 3].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  85. var userDecimalHours = Convert.ToDecimal(TimeSpan.FromSeconds(reportPerson.duration).TotalHours).ToString("0.00"); ;
  86. ws.Cells[row, 3].Value = userDecimalHours;
  87. usersDecimalHours.Add(userDecimalHours);
  88. row++;
  89. foreach (var personRecord in reportPerson.children)
  90. {
  91. ws.Cells[row, 0].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  92. ws.Cells[row, 1].Style = normalDetailsStyle;
  93. ws.Cells[row, 1].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  94. ws.Cells[row, 1].Value = personRecord.name;
  95. ws.Cells[row, 2].Style = normalDetailsStyle;
  96. ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  97. ws.Cells[row, 2].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  98. t = TimeSpan.FromSeconds(personRecord.duration);
  99. ws.Cells[row, 2].Value = string.Format("{0:00}:{1:D2}:{2:D2}", Math.Floor(t.TotalHours), t.Minutes, t.Seconds);
  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 = Convert.ToDecimal(TimeSpan.FromSeconds(personRecord.duration).TotalHours).ToString("0.00");
  104. row++;
  105. }
  106. }
  107. var totalSum = reportObject.totals.Select(total => total.totalTime).FirstOrDefault();
  108. ws.Cells[row, 0].Style = mainDetailsStyle;
  109. ws.Cells[row, 0].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  110. ws.Cells[row, 0].Value = "Total";
  111. ws.Cells[row, 2].Style = mainDetailsStyle;
  112. ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  113. ws.Cells[row, 2].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  114. time1 = t = TimeSpan.FromSeconds(totalSum);
  115. ws.Cells[row, 2].Value = string.Format("{0:00}:{1:D2}:{2:D2}", Math.Floor(time1.TotalHours), time1.Minutes, time1.Seconds);
  116. ws.Cells[row, 3].Style = mainDetailsStyle;
  117. ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  118. ws.Cells[row, 3].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
  119. ws.Cells[row, 3].Value = Convert.ToDecimal(TimeSpan.FromSeconds(totalSum).TotalHours).ToString("0.00"); ;
  120. }
  121. public static void AutoFitReport(ExcelFile excelFile)
  122. {
  123. foreach (var sheet in excelFile.Worksheets)
  124. {
  125. var columnCount = sheet.CalculateMaxUsedColumns();
  126. for (int i = 0; i < columnCount; i++)
  127. {
  128. sheet.Columns[i].AutoFit(1, sheet.Rows[0], sheet.Rows[sheet.Rows.Count - 1]);
  129. }
  130. }
  131. }
  132. }
  133. }