ReportExport.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 tr {
  127. background-color: #ffffff;
  128. }
  129. table.gridtable td {
  130. border-width: 0.1pt;
  131. padding: 8px;
  132. border-style: solid;
  133. border-color: #666666;
  134. background-color: #ffffff;
  135. }
  136. </style>";
  137. string Html = @"<!DOCTYPE html>
  138. <html xmlns='http://www.w3.org/1999/xhtml'>
  139. <head>
  140. <meta http-equiv='X-UA-Compatible' content='IE=8, IE=9, IE=10' />
  141. <meta charset='utf-8'>
  142. <title>Emby Reports Export</title>";
  143. Html += "\n" + style + "\n";
  144. Html += "</head>\n";
  145. Html += "<body>\n";
  146. StringBuilder returnValue = new StringBuilder();
  147. returnValue.AppendLine("<table class='gridtable'>");
  148. returnValue.AppendLine("<tr>");
  149. returnValue.AppendLine(string.Join("", reportResult.Headers.Select(s => string.Format("<th>{0}</th>", s.Name)).ToArray()));
  150. returnValue.AppendLine("</tr>");
  151. if (reportResult.IsGrouped)
  152. foreach (ReportGroup group in reportResult.Groups)
  153. {
  154. returnValue.AppendLine("<tr>");
  155. returnValue.AppendLine("<th scope='rowgroup' colspan='" + reportResult.Headers.Count + "'>" + (string.IsNullOrEmpty(group.Name) ? "&nbsp;" : group.Name) + "</th>");
  156. returnValue.AppendLine("</tr>");
  157. foreach (ReportRow row in group.Rows)
  158. {
  159. ExportToExcelRow(reportResult, returnValue, row);
  160. }
  161. returnValue.AppendLine("<tr>");
  162. returnValue.AppendLine("<th style='background-color: #ffffff;' scope='rowgroup' colspan='" + reportResult.Headers.Count + "'>" + "&nbsp;" + "</th>");
  163. returnValue.AppendLine("</tr>");
  164. }
  165. else
  166. foreach (ReportRow row in reportResult.Rows)
  167. {
  168. ExportToExcelRow(reportResult, returnValue, row);
  169. }
  170. returnValue.AppendLine("</table>");
  171. Html += returnValue.ToString();
  172. Html += "</body>";
  173. Html += "</html>";
  174. return Html;
  175. }
  176. private static void ExportToExcelRow(ReportResult reportResult,
  177. StringBuilder returnValue,
  178. ReportRow row)
  179. {
  180. returnValue.AppendLine("<tr>");
  181. returnValue.AppendLine(string.Join("", row.Columns.Select(s => string.Format("<td>{0}</td>", s.Name)).ToArray()));
  182. returnValue.AppendLine("</tr>");
  183. }
  184. }
  185. }