using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MediaBrowser.Api.Reports { /// A report export. public class ReportExport { /// Export to CSV. /// The report result. /// A string. public string ExportToCsv(ReportResult reportResult) { StringBuilder returnValue = new StringBuilder(); returnValue.AppendLine(string.Join(";", reportResult.Headers.Select(s => s.Name.Replace(',', ' ')).ToArray())); if (reportResult.IsGrouped) foreach (ReportGroup group in reportResult.Groups) { foreach (ReportRow row in reportResult.Rows) { returnValue.AppendLine(string.Join(";", row.Columns.Select(s => s.Name.Replace(',', ' ')).ToArray())); } } else foreach (ReportRow row in reportResult.Rows) { returnValue.AppendLine(string.Join(";", row.Columns.Select(s => s.Name.Replace(',', ' ')).ToArray())); } return returnValue.ToString(); } /// Export to excel. /// The report result. /// A string. public string ExportToExcel(ReportResult reportResult) { string style = @""; string Html = @" Emby Export"; Html += "\n" + style + "\n"; Html += "\n"; Html += "\n"; StringBuilder returnValue = new StringBuilder(); returnValue.AppendLine(""); returnValue.AppendLine(""); returnValue.AppendLine(string.Join("", reportResult.Headers.Select(s => string.Format("", s.Name)).ToArray())); returnValue.AppendLine(""); if (reportResult.IsGrouped) foreach (ReportGroup group in reportResult.Groups) { returnValue.AppendLine(""); returnValue.AppendLine(""); returnValue.AppendLine(""); foreach (ReportRow row in group.Rows) { ExportToExcelRow(reportResult, returnValue, row); } returnValue.AppendLine(""); returnValue.AppendLine(""); returnValue.AppendLine(""); } else foreach (ReportRow row in reportResult.Rows) { ExportToExcelRow(reportResult, returnValue, row); } returnValue.AppendLine("
{0}
" + (string.IsNullOrEmpty(group.Name) ? " " : group.Name) + "
" + " " + "
"); Html += returnValue.ToString(); Html += ""; Html += ""; return Html; } private static void ExportToExcelRow(ReportResult reportResult, StringBuilder returnValue, ReportRow row) { returnValue.AppendLine(""); returnValue.AppendLine(string.Join("", row.Columns.Select(s => string.Format("{0}", s.Name)).ToArray())); returnValue.AppendLine(""); } } }