Black Rock Reporting Azure Function
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReportGenerator.cs 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Spire.Xls;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using ClockifyReport.Models;
  6. using System.Drawing;
  7. namespace BlackRockReportFunction.Bussines
  8. {
  9. internal class ReportGenerator
  10. {
  11. public const string DefaultFontName = "DejaVu Sans";
  12. public const int DefaultFontSize = 20;
  13. public static Workbook GenerateReportContent(Root reportObject)
  14. {
  15. var excelFile = new Workbook();
  16. excelFile.Worksheets.Clear();
  17. var ws = excelFile.Worksheets.Add("Content");
  18. AddReportItems(reportObject, ws);
  19. // Autofit
  20. AutoFitReport(excelFile);
  21. return excelFile;
  22. }
  23. public static void AddReportItems(Root reportObject, Worksheet ws)
  24. {
  25. TimeSpan t, time, time1;
  26. int row = 1;
  27. string[] sectionNames = { "User ", "Description ", "Time (h) ", "Time (decimal) " };
  28. List<string> usersDecimalHours = new List<string>();
  29. for (int i = 0; i < sectionNames.Length; i++)
  30. {
  31. ws.Range[row, i + 1].Style.Font.FontName = "DejaVu Sans";
  32. ws.Range[row, i + 1].Style.Font.Size = 12;
  33. ws.Range[row, i + 1].Value = sectionNames[i];
  34. ws.Range[row, i + 1].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
  35. ws.Range[row, i + 1].Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thick;
  36. ws.Range[row, i + 1].Style.Borders[BordersLineType.EdgeBottom].Color = Color.LightGray;
  37. }
  38. row++;
  39. foreach (var reportPerson in reportObject.groupOne)
  40. {
  41. ws.Range[row, 1].Style.Font.FontName = "DejaVu Sans";
  42. ws.Range[row, 1].Style.Font.Size = 10;
  43. ws.Range[row, 1].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
  44. ws.Range[row, 1].Value = reportPerson.name;
  45. ws.Range[row, 2].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
  46. time = TimeSpan.FromSeconds(reportPerson.duration);
  47. var sumOfRecordHours = string.Format("{0:00}:{1:D2}:{2:D2}", Math.Floor(time.TotalHours), time.Minutes, time.Seconds);
  48. ws.Range[row, 3].Style.Font.FontName = "DejaVu Sans";
  49. ws.Range[row, 3].Style.Font.Size = 10;
  50. ws.Range[row, 3].Style.HorizontalAlignment = HorizontalAlignType.Right;
  51. ws.Range[row, 3].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
  52. ws.Range[row, 3].Value = sumOfRecordHours;
  53. ws.Range[row, 4].Style.Font.FontName = "DejaVu Sans";
  54. ws.Range[row, 4].Style.Font.Size = 10;
  55. ws.Range[row, 4].Style.HorizontalAlignment = HorizontalAlignType.Right;
  56. ws.Range[row, 4].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
  57. var userDecimalHours = Convert.ToDecimal(TimeSpan.FromSeconds(reportPerson.duration).TotalHours).ToString("0.00"); ;
  58. ws.Range[row, 4].Value = userDecimalHours;
  59. usersDecimalHours.Add(userDecimalHours);
  60. row++;
  61. foreach (var personRecord in reportPerson.children)
  62. {
  63. ws.Range[row, 1].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
  64. ws.Range[row, 2].Style.Font.FontName = "DejaVu Sans";
  65. ws.Range[row, 2].Style.Font.Size = 10;
  66. ws.Range[row, 2].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
  67. ws.Range[row, 2].Value = personRecord.name;
  68. ws.Range[row, 3].Style.Font.FontName = "DejaVu Sans";
  69. ws.Range[row, 3].Style.Font.Size = 10;
  70. ws.Range[row, 3].Style.HorizontalAlignment = HorizontalAlignType.Right;
  71. ws.Range[row, 3].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
  72. t = TimeSpan.FromSeconds(personRecord.duration);
  73. ws[row, 3].Value = string.Format("{0:00}:{1:D2}:{2:D2}", Math.Floor(t.TotalHours), t.Minutes, t.Seconds);
  74. ws.Range[row, 4].Style.Font.FontName = "DejaVu Sans";
  75. ws.Range[row, 4].Style.Font.Size = 10;
  76. ws.Range[row, 4].Style.HorizontalAlignment = HorizontalAlignType.Right;
  77. ws.Range[row, 4].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
  78. ws.Range[row, 4].Value = Convert.ToDecimal(TimeSpan.FromSeconds(personRecord.duration).TotalHours).ToString("0.00");
  79. row++;
  80. }
  81. }
  82. var totalSum = reportObject.totals.Select(total => total.totalTime).FirstOrDefault();
  83. ws.Range[row, 1].Style.Font.FontName = "DejaVu Sans";
  84. ws.Range[row, 1].Style.Font.Size = 10;
  85. ws.Range[row, 1].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
  86. ws.Range[row, 1].Value = "Total";
  87. ws.Range[row, 3].Style.Font.FontName = "DejaVu Sans";
  88. ws.Range[row, 3].Style.Font.Size = 10;
  89. ws.Range[row, 3].Style.HorizontalAlignment = HorizontalAlignType.Right;
  90. ws.Range[row, 3].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
  91. time1 = t = TimeSpan.FromSeconds(totalSum);
  92. ws.Range[row, 3].Value = string.Format("{0:00}:{1:D2}:{2:D2}", Math.Floor(time1.TotalHours), time1.Minutes, time1.Seconds);
  93. ws.Range[row, 4].Style.Font.FontName = "DejaVu Sans";
  94. ws.Range[row, 4].Style.Font.Size = 10;
  95. ws.Range[row, 4].Style.HorizontalAlignment = HorizontalAlignType.Right;
  96. ws.Range[row, 4].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
  97. ws.Range[row, 4].Value = Convert.ToDecimal(TimeSpan.FromSeconds(totalSum).TotalHours).ToString("0.00");
  98. }
  99. public static void AutoFitReport(Workbook excelFile)
  100. {
  101. foreach (var sheet in excelFile.Worksheets)
  102. {
  103. var columnCount = sheet.Columns.Count();
  104. for (int i = 1; i < columnCount; i++)
  105. {
  106. sheet.AutoFitColumn(i);
  107. }
  108. var rowCount = sheet.Rows.Count();
  109. for (int i = 1; i < rowCount; i++)
  110. {
  111. sheet.AutoFitRow(i);
  112. }
  113. }
  114. }
  115. }
  116. }