Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. /// <summary>
  2. /// Izvoz master-detail podataka o osobama u Excel
  3. /// </summary>
  4. /// <returns>Excel datoteka sa master-detail podacima</returns>
  5. public IActionResult OsobeMasterDetailExcel()
  6. {
  7. var people = _context.Person.Select(p => new
  8. {
  9. Id = p.Id,
  10. FirstName = p.FirstName.Trim(),
  11. LastName = p.LastName.Trim(),
  12. Oib = p.Oib.Trim(),
  13. Contents = _context.Content.Select(c => new
  14. {
  15. Name = c.Name.Trim(),
  16. Description = c.Description.Trim(),
  17. Link = c.Link.Trim(),
  18. IdCreator = c.IdCreator
  19. }).Where(c => c.IdCreator == p.Id).ToList()
  20. }).ToList();
  21.  
  22. byte[] content;
  23. using (ExcelPackage excel = new ExcelPackage())
  24. {
  25. excel.Workbook.Properties.Title = "Popis osoba";
  26. excel.Workbook.Properties.Author = "RPPP04";
  27. var worksheet = excel.Workbook.Worksheets.Add("Osobe");
  28.  
  29. int j = 1;
  30. for (int i = 0; i < people.Count; i++)
  31. {
  32. worksheet.Cells[j, 1].Value = "Ime";
  33. worksheet.Cells[j + 1, 1].Value = "Prezime";
  34. worksheet.Cells[j + 2, 1].Value = "Oib";
  35.  
  36. worksheet.Cells[j, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
  37. worksheet.Cells[j + 1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
  38. worksheet.Cells[j + 2, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
  39. worksheet.Cells[j, 1].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Lavender);
  40. worksheet.Cells[j + 1, 1].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Lavender);
  41. worksheet.Cells[j + 2, 1].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Lavender);
  42.  
  43. worksheet.Cells[j, 2].Value = people[i].FirstName;
  44. worksheet.Cells[j + 1, 2].Value = people[i].LastName;
  45. worksheet.Cells[j + 2, 2].Value = people[i].Oib;
  46.  
  47. j += 3;
  48.  
  49. worksheet.Cells[j, 1].Value = "#";
  50. worksheet.Cells[j, 2].Value = "Naziv sadrzaja";
  51. worksheet.Cells[j, 3].Value = "Opis sadrzaja";
  52. worksheet.Cells[j, 4].Value = "Link";
  53.  
  54. worksheet.Cells[j, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
  55. worksheet.Cells[j, 2].Style.Fill.PatternType = ExcelFillStyle.Solid;
  56. worksheet.Cells[j, 3].Style.Fill.PatternType = ExcelFillStyle.Solid;
  57. worksheet.Cells[j, 4].Style.Fill.PatternType = ExcelFillStyle.Solid;
  58.  
  59. worksheet.Cells[j, 1].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightSeaGreen);
  60. worksheet.Cells[j, 2].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightSeaGreen);
  61. worksheet.Cells[j, 3].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightSeaGreen);
  62. worksheet.Cells[j, 4].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightSeaGreen);
  63.  
  64. j++;
  65. for (int k = 0; k < people[i].Contents.Count; k++)
  66. {
  67. worksheet.Cells[j, 1].Value = k + 1;
  68. worksheet.Cells[j, 2].Value = people[i].Contents[k].Name;
  69. worksheet.Cells[j, 3].Value = people[i].Contents[k].Description;
  70. worksheet.Cells[j, 4].Value = people[i].Contents[k].Link;
  71. j++;
  72. }
  73.  
  74. worksheet.Cells[j, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
  75. worksheet.Cells[j, 2].Style.Fill.PatternType = ExcelFillStyle.Solid;
  76. worksheet.Cells[j, 3].Style.Fill.PatternType = ExcelFillStyle.Solid;
  77. worksheet.Cells[j, 4].Style.Fill.PatternType = ExcelFillStyle.Solid;
  78.  
  79. worksheet.Cells[j, 1].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Lavender);
  80. worksheet.Cells[j, 2].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Lavender);
  81. worksheet.Cells[j, 3].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Lavender);
  82. worksheet.Cells[j, 4].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Lavender);
  83. j++;
  84. j++;
  85. }
  86. worksheet.Cells[1, 1, 200, 4].AutoFitColumns();
  87.  
  88. content = excel.GetAsByteArray();
  89. }
  90.  
  91. return File(content, ExcelContentType, "osobeSlozeniPodaci.xlsx");
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement