public ExportResult GeneratePersonCsomor(List <CsomorPerson> persons) { persons = persons.OrderBy(x => x.Name).ToList(); const string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; string name = $"{DateHelper.ToFileName(DateTime.Now)}_persons.xlsx"; using (var workbook = new XLWorkbook()) { var worksheet = workbook.Worksheets.Add("Persons"); // Header for (int i = 0; i < persons.Count; i++) { worksheet.Cell(1, i + 2).Value = persons[i].Name; } var groups = persons.SelectMany(x => x.Tables).GroupBy(x => x.Date).OrderBy(x => x.Key); // Date Col int rowNo = 2; foreach (var row in groups) { worksheet.Cell(rowNo, 1).Value = WriteHelper.HourInterval(row.Key, 1); int colNo = 2; foreach (var col in row) { worksheet.Cell(rowNo, colNo).Value = col.Work != null ? col.Work.Name : "-"; colNo++; } rowNo++; } var stream = new MemoryStream(); workbook.SaveAs(stream); return(new ExportResult { Content = stream.ToArray(), FileName = name, ContentType = contentType }); } }
public ExportResult GenerateWorkCsomor(List <CsomorWork> works) { works = works.OrderBy(x => x.Name).ToList(); const string contentType = "application/pdf"; string name = $"{DateHelper.ToFileName(DateTime.Now)}_works.pdf"; var globalSettings = new GlobalSettings { ColorMode = ColorMode.Color, Orientation = Orientation.Landscape, PaperSize = DinkToPdf.PaperKind.A4, Margins = new MarginSettings { Top = 10 }, DocumentTitle = "Works" }; var sb = new StringBuilder(); sb.Append(@"<html><head></head><body>"); sb.Append(@"<div class='header'>Work Table</div>"); sb.Append(@"<table><tr><th class='empty'></th>"); for (int i = 0; i < works.Count; i++) { sb.AppendFormat(@"<th>{0}</th>", works[i].Name); } sb.Append(@"</tr>"); // Date Col foreach (var row in works.SelectMany(x => x.Tables).GroupBy(x => x.Date).OrderBy(x => x.Key)) { sb.Append(@"<tr>"); sb.AppendFormat(@"<th>{0}</th>", WriteHelper.HourInterval(row.Key, 1)); foreach (var col in row) { sb.AppendFormat(@"<td>{0}</td>", col.Person != null ? col.Person.Name : "-"); } sb.Append(@"</tr>"); } sb.Append(@"</table>"); sb.Append(@"</body></html>"); var objectSettings = new ObjectSettings { PagesCount = true, HtmlContent = sb.ToString(), WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "styles.css") }, HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true }, FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Center = "Generated Content" } }; var pdf = new HtmlToPdfDocument() { GlobalSettings = globalSettings, Objects = { objectSettings } }; byte[] file = this._converter.Convert(pdf); return(new ExportResult { Content = file, ContentType = contentType, FileName = name }); }