ReportExport.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace MediaBrowser.Api.Reports
  7. {
  8. /// <summary> A report export. </summary>
  9. public class ReportExport
  10. {
  11. /// <summary> Export to CSV. </summary>
  12. /// <param name="reportResult"> The report result. </param>
  13. /// <returns> A string. </returns>
  14. public string ExportToCsv(ReportResult reportResult)
  15. {
  16. StringBuilder returnValue = new StringBuilder();
  17. returnValue.AppendLine(string.Join(";", reportResult.Headers.Select(s => s.Name.Replace(',', ' ')).ToArray()));
  18. if (reportResult.IsGrouped)
  19. foreach (ReportGroup group in reportResult.Groups)
  20. {
  21. foreach (ReportRow row in reportResult.Rows)
  22. {
  23. returnValue.AppendLine(string.Join(";", row.Columns.Select(s => s.Name.Replace(',', ' ')).ToArray()));
  24. }
  25. }
  26. else
  27. foreach (ReportRow row in reportResult.Rows)
  28. {
  29. returnValue.AppendLine(string.Join(";", row.Columns.Select(s => s.Name.Replace(',', ' ')).ToArray()));
  30. }
  31. return returnValue.ToString();
  32. }
  33. /// <summary> Export to excel. </summary>
  34. /// <param name="reportResult"> The report result. </param>
  35. /// <returns> A string. </returns>
  36. public string ExportToExcel(ReportResult reportResult)
  37. {
  38. string style = @"<style type='text/css'>
  39. BODY {
  40. font-family: Arial;
  41. font-size: 12px;
  42. }
  43. TABLE {
  44. font-family: Arial;
  45. font-size: 12px;
  46. }
  47. A {
  48. font-family: Arial;
  49. color: #144A86;
  50. font-size: 12px;
  51. cursor: pointer;
  52. text-decoration: none;
  53. font-weight: bold;
  54. }
  55. DIV {
  56. font-family: Arial;
  57. font-size: 12px;
  58. margin-bottom: 0px;
  59. }
  60. P, LI, DIV {
  61. font-size: 12px;
  62. margin-bottom: 0px;
  63. }
  64. P, UL {
  65. font-size: 12px;
  66. margin-bottom: 6px;
  67. margin-top: 0px;
  68. }
  69. H1 {
  70. font-size: 18pt;
  71. }
  72. H2 {
  73. font-weight: bold;
  74. font-size: 14pt;
  75. COLOR: #C0C0C0;
  76. }
  77. H3 {
  78. font-weight: normal;
  79. font-size: 14pt;
  80. text-indent: +1em;
  81. }
  82. H4 {
  83. font-size: 10pt;
  84. font-weight: normal;
  85. }
  86. H5 {
  87. font-size: 10pt;
  88. font-weight: normal;
  89. background: #A9A9A9;
  90. COLOR: white;
  91. display: inline;
  92. }
  93. H6 {
  94. padding: 2 1 2 5;
  95. font-size: 11px;
  96. font-weight: bold;
  97. text-decoration: none;
  98. margin-bottom: 1px;
  99. }
  100. UL {
  101. line-height: 1.5em;
  102. list-style-type: disc;
  103. }
  104. OL {
  105. line-height: 1.5em;
  106. }
  107. LI {
  108. line-height: 1.5em;
  109. }
  110. A IMG {
  111. border: 0;
  112. }
  113. table.gridtable {
  114. color: #333333;
  115. border-width: 0.1pt;
  116. border-color: #666666;
  117. border-collapse: collapse;
  118. }
  119. table.gridtable th {
  120. border-width: 0.1pt;
  121. padding: 8px;
  122. border-style: solid;
  123. border-color: #666666;
  124. background-color: #dedede;
  125. }
  126. table.gridtable td {
  127. border-width: 0.1pt;
  128. padding: 8px;
  129. border-style: solid;
  130. border-color: #666666;
  131. background-color: #ffffff;
  132. }
  133. </style>";
  134. string Html = @"<!DOCTYPE html>
  135. <html xmlns='http://www.w3.org/1999/xhtml'>
  136. <head>
  137. <meta http-equiv='X-UA-Compatible' content='IE=8, IE=9, IE=10' />
  138. <meta charset='utf-8'>
  139. <title>Emby Export</title>";
  140. Html += "\n" + style + "\n";
  141. Html += "</head>\n";
  142. Html += "<body>\n";
  143. StringBuilder returnValue = new StringBuilder();
  144. returnValue.AppendLine("<table class='gridtable'>");
  145. returnValue.AppendLine("<tr>");
  146. returnValue.AppendLine(string.Join("", reportResult.Headers.Select(s => string.Format("<th>{0}</th>", s.Name)).ToArray()));
  147. returnValue.AppendLine("</tr>");
  148. if (reportResult.IsGrouped)
  149. foreach (ReportGroup group in reportResult.Groups)
  150. {
  151. returnValue.AppendLine("<tr style='background-color: rgb(51, 51, 51);'>");
  152. returnValue.AppendLine("<th scope='rowgroup' colspan='" + reportResult.Headers.Count + "'>" + (string.IsNullOrEmpty(group.Name) ? "&nbsp;" : group.Name) + "</th>");
  153. returnValue.AppendLine("</tr>");
  154. foreach (ReportRow row in group.Rows)
  155. {
  156. ExportToExcelRow(reportResult, returnValue, row);
  157. }
  158. returnValue.AppendLine("<tr style='background-color: white;'>");
  159. returnValue.AppendLine("<th scope='rowgroup' colspan='" + reportResult.Headers.Count + "'>" + "&nbsp;" + "</th>");
  160. returnValue.AppendLine("</tr>");
  161. }
  162. else
  163. foreach (ReportRow row in reportResult.Rows)
  164. {
  165. ExportToExcelRow(reportResult, returnValue, row);
  166. }
  167. returnValue.AppendLine("</table>");
  168. Html += returnValue.ToString();
  169. Html += "</body>";
  170. Html += "</html>";
  171. return Html;
  172. }
  173. private static void ExportToExcelRow(ReportResult reportResult,
  174. StringBuilder returnValue,
  175. ReportRow row)
  176. {
  177. returnValue.AppendLine("<tr>");
  178. returnValue.AppendLine(string.Join("", row.Columns.Select(s => string.Format("<td>{0}</td>", s.Name)).ToArray()));
  179. returnValue.AppendLine("</tr>");
  180. }
  181. }
  182. }