2
0

ReportExport.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System.Linq;
  2. using System.Text;
  3. namespace MediaBrowser.Api.Reports
  4. {
  5. /// <summary> A report export. </summary>
  6. public class ReportExport
  7. {
  8. /// <summary> Export to CSV. </summary>
  9. /// <param name="reportResult"> The report result. </param>
  10. /// <returns> A string. </returns>
  11. public string ExportToCsv(ReportResult reportResult)
  12. {
  13. StringBuilder returnValue = new StringBuilder();
  14. returnValue.AppendLine(string.Join(";", reportResult.Headers.Select(s => s.Name.Replace(',', ' ')).ToArray()));
  15. if (reportResult.IsGrouped)
  16. foreach (ReportGroup group in reportResult.Groups)
  17. {
  18. foreach (ReportRow row in reportResult.Rows)
  19. {
  20. returnValue.AppendLine(string.Join(";", row.Columns.Select(s => s.Name.Replace(',', ' ')).ToArray()));
  21. }
  22. }
  23. else
  24. foreach (ReportRow row in reportResult.Rows)
  25. {
  26. returnValue.AppendLine(string.Join(";", row.Columns.Select(s => s.Name.Replace(',', ' ')).ToArray()));
  27. }
  28. return returnValue.ToString();
  29. }
  30. /// <summary> Export to excel. </summary>
  31. /// <param name="reportResult"> The report result. </param>
  32. /// <returns> A string. </returns>
  33. public string ExportToExcel(ReportResult reportResult)
  34. {
  35. string style = @"<style type='text/css'>
  36. BODY {
  37. font-family: Arial;
  38. font-size: 12px;
  39. }
  40. TABLE {
  41. font-family: Arial;
  42. font-size: 12px;
  43. }
  44. A {
  45. font-family: Arial;
  46. color: #144A86;
  47. font-size: 12px;
  48. cursor: pointer;
  49. text-decoration: none;
  50. font-weight: bold;
  51. }
  52. DIV {
  53. font-family: Arial;
  54. font-size: 12px;
  55. margin-bottom: 0px;
  56. }
  57. P, LI, DIV {
  58. font-size: 12px;
  59. margin-bottom: 0px;
  60. }
  61. P, UL {
  62. font-size: 12px;
  63. margin-bottom: 6px;
  64. margin-top: 0px;
  65. }
  66. H1 {
  67. font-size: 18pt;
  68. }
  69. H2 {
  70. font-weight: bold;
  71. font-size: 14pt;
  72. COLOR: #C0C0C0;
  73. }
  74. H3 {
  75. font-weight: normal;
  76. font-size: 14pt;
  77. text-indent: +1em;
  78. }
  79. H4 {
  80. font-size: 10pt;
  81. font-weight: normal;
  82. }
  83. H5 {
  84. font-size: 10pt;
  85. font-weight: normal;
  86. background: #A9A9A9;
  87. COLOR: white;
  88. display: inline;
  89. }
  90. H6 {
  91. padding: 2 1 2 5;
  92. font-size: 11px;
  93. font-weight: bold;
  94. text-decoration: none;
  95. margin-bottom: 1px;
  96. }
  97. UL {
  98. line-height: 1.5em;
  99. list-style-type: disc;
  100. }
  101. OL {
  102. line-height: 1.5em;
  103. }
  104. LI {
  105. line-height: 1.5em;
  106. }
  107. A IMG {
  108. border: 0;
  109. }
  110. table.gridtable {
  111. color: #333333;
  112. border-width: 0.1pt;
  113. border-color: #666666;
  114. border-collapse: collapse;
  115. }
  116. table.gridtable th {
  117. border-width: 0.1pt;
  118. padding: 8px;
  119. border-style: solid;
  120. border-color: #666666;
  121. background-color: #dedede;
  122. }
  123. table.gridtable tr {
  124. background-color: #ffffff;
  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 Reports 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>");
  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>");
  159. returnValue.AppendLine("<th style='background-color: #ffffff;' 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. }