Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Izvoz master-detail podataka o osobama u Excel
- /// </summary>
- /// <returns>Excel datoteka sa master-detail podacima</returns>
- public IActionResult OsobeMasterDetailExcel()
- {
- var people = _context.Person.Select(p => new
- {
- Id = p.Id,
- FirstName = p.FirstName.Trim(),
- LastName = p.LastName.Trim(),
- Oib = p.Oib.Trim(),
- Contents = _context.Content.Select(c => new
- {
- Name = c.Name.Trim(),
- Description = c.Description.Trim(),
- Link = c.Link.Trim(),
- IdCreator = c.IdCreator
- }).Where(c => c.IdCreator == p.Id).ToList()
- }).ToList();
- byte[] content;
- using (ExcelPackage excel = new ExcelPackage())
- {
- excel.Workbook.Properties.Title = "Popis osoba";
- excel.Workbook.Properties.Author = "RPPP04";
- var worksheet = excel.Workbook.Worksheets.Add("Osobe");
- int j = 1;
- for (int i = 0; i < people.Count; i++)
- {
- worksheet.Cells[j, 1].Value = "Ime";
- worksheet.Cells[j + 1, 1].Value = "Prezime";
- worksheet.Cells[j + 2, 1].Value = "Oib";
- worksheet.Cells[j, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
- worksheet.Cells[j + 1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
- worksheet.Cells[j + 2, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
- worksheet.Cells[j, 1].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Lavender);
- worksheet.Cells[j + 1, 1].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Lavender);
- worksheet.Cells[j + 2, 1].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Lavender);
- worksheet.Cells[j, 2].Value = people[i].FirstName;
- worksheet.Cells[j + 1, 2].Value = people[i].LastName;
- worksheet.Cells[j + 2, 2].Value = people[i].Oib;
- j += 3;
- worksheet.Cells[j, 1].Value = "#";
- worksheet.Cells[j, 2].Value = "Naziv sadrzaja";
- worksheet.Cells[j, 3].Value = "Opis sadrzaja";
- worksheet.Cells[j, 4].Value = "Link";
- worksheet.Cells[j, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
- worksheet.Cells[j, 2].Style.Fill.PatternType = ExcelFillStyle.Solid;
- worksheet.Cells[j, 3].Style.Fill.PatternType = ExcelFillStyle.Solid;
- worksheet.Cells[j, 4].Style.Fill.PatternType = ExcelFillStyle.Solid;
- worksheet.Cells[j, 1].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightSeaGreen);
- worksheet.Cells[j, 2].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightSeaGreen);
- worksheet.Cells[j, 3].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightSeaGreen);
- worksheet.Cells[j, 4].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightSeaGreen);
- j++;
- for (int k = 0; k < people[i].Contents.Count; k++)
- {
- worksheet.Cells[j, 1].Value = k + 1;
- worksheet.Cells[j, 2].Value = people[i].Contents[k].Name;
- worksheet.Cells[j, 3].Value = people[i].Contents[k].Description;
- worksheet.Cells[j, 4].Value = people[i].Contents[k].Link;
- j++;
- }
- worksheet.Cells[j, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
- worksheet.Cells[j, 2].Style.Fill.PatternType = ExcelFillStyle.Solid;
- worksheet.Cells[j, 3].Style.Fill.PatternType = ExcelFillStyle.Solid;
- worksheet.Cells[j, 4].Style.Fill.PatternType = ExcelFillStyle.Solid;
- worksheet.Cells[j, 1].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Lavender);
- worksheet.Cells[j, 2].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Lavender);
- worksheet.Cells[j, 3].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Lavender);
- worksheet.Cells[j, 4].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Lavender);
- j++;
- j++;
- }
- worksheet.Cells[1, 1, 200, 4].AutoFitColumns();
- content = excel.GetAsByteArray();
- }
- return File(content, ExcelContentType, "osobeSlozeniPodaci.xlsx");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement