|
1 |
| -using Inflow.Domain.DTOs.Category; |
| 1 | +using ClosedXML.Excel; |
| 2 | +using Inflow.Domain.DTOs.Category; |
2 | 3 | using Inflow.Domain.DTOs.Product;
|
3 | 4 | using Inflow.Domain.Interfaces.Services;
|
4 | 5 | using Inflow.Domain.ResourceParameters;
|
5 | 6 | using Inflow.ResourceParameters;
|
6 | 7 | using Microsoft.AspNetCore.Mvc;
|
| 8 | +using Syncfusion.Drawing; |
| 9 | +using Syncfusion.Pdf; |
| 10 | +using Syncfusion.Pdf.Grid; |
| 11 | +using System.Data; |
7 | 12 |
|
8 | 13 | namespace Inflow.Controllers
|
9 | 14 | {
|
| 15 | + |
10 | 16 | [Route("api/categories")]
|
11 | 17 | [ApiController]
|
12 | 18 | //[Authorize]
|
@@ -38,34 +44,41 @@ public ActionResult<CategoryDto> Get(int id)
|
38 | 44 | return Ok(category);
|
39 | 45 | }
|
40 | 46 |
|
41 |
| - //[HttpGet("export")] |
42 |
| - //public ActionResult ExportCustomers() |
43 |
| - //{ |
44 |
| - // var category = _categoryService.GetAllCategories(); |
| 47 | + [HttpGet("export/xls")] |
| 48 | + public ActionResult ExportCustomers() |
| 49 | + { |
| 50 | + var categories = _categoryService.GetAllCategories(); |
| 51 | + byte[] data = GenerateExcle(categories); |
| 52 | + |
| 53 | + return File(data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Categories.xlsx"); |
| 54 | + } |
| 55 | + |
| 56 | + [HttpGet("export/pdf")] |
| 57 | + public IActionResult CreatePDFDocument() |
| 58 | + { |
| 59 | + PdfDocument document = new PdfDocument(); |
| 60 | + PdfPage page = document.Pages.Add(); |
| 61 | + |
| 62 | + PdfGrid pdfGrid = new PdfGrid(); |
45 | 63 |
|
46 |
| - // using XLWorkbook wb = new XLWorkbook(); |
47 |
| - // var sheet1 = wb.AddWorksheet(GetCategoriesDataTable(category), "Categories"); |
| 64 | + var categories = _categoryService.GetAllCategories(); |
| 65 | + List<object> data = ConvertCategoriesToData(categories); |
48 | 66 |
|
49 |
| - // sheet1.Column(1).Style.Font.FontColor = XLColor.Red; |
| 67 | + pdfGrid.DataSource = data; |
50 | 68 |
|
51 |
| - // sheet1.Columns(2, 4).Style.Font.FontColor = XLColor.Blue; |
| 69 | + pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1); |
52 | 70 |
|
53 |
| - // sheet1.Row(1).CellsUsed().Style.Fill.BackgroundColor = XLColor.Black; |
54 |
| - // //sheet1.Row(1).Cells(1,3).Style.Fill.BackgroundColor = XLColor.Yellow; |
55 |
| - // sheet1.Row(1).Style.Font.FontColor = XLColor.White; |
| 71 | + pdfGrid.Draw(page, new PointF(10, 10)); |
56 | 72 |
|
57 |
| - // sheet1.Row(1).Style.Font.Bold = true; |
58 |
| - // sheet1.Row(1).Style.Font.Shadow = true; |
59 |
| - // sheet1.Row(1).Style.Font.Underline = XLFontUnderlineValues.Single; |
60 |
| - // sheet1.Row(1).Style.Font.VerticalAlignment = XLFontVerticalTextAlignmentValues.Superscript; |
61 |
| - // sheet1.Row(1).Style.Font.Italic = true; |
| 73 | + MemoryStream stream = new MemoryStream(); |
| 74 | + document.Save(stream); |
| 75 | + stream.Position = 0; |
62 | 76 |
|
63 |
| - // sheet1.Rows(2, 3).Style.Font.FontColor = XLColor.AshGrey; |
| 77 | + string contentType = "application/pdf"; |
| 78 | + string fileName = "categories.pdf"; |
64 | 79 |
|
65 |
| - // using MemoryStream ms = new MemoryStream(); |
66 |
| - // wb.SaveAs(ms); |
67 |
| - // return File(ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Categories.xlsx"); |
68 |
| - //} |
| 80 | + return File(stream, contentType, fileName); |
| 81 | + } |
69 | 82 |
|
70 | 83 | [HttpGet("{id}/products")]
|
71 | 84 | public ActionResult<ProductDto> GetProductsByCategoryId(
|
@@ -106,21 +119,55 @@ public ActionResult Delete(int id)
|
106 | 119 |
|
107 | 120 | return NoContent();
|
108 | 121 | }
|
| 122 | + private static byte[] GenerateExcle(IEnumerable<CategoryDto> categoryDtos) |
| 123 | + { |
| 124 | + using XLWorkbook wb = new(); |
| 125 | + var sheet1 = wb.AddWorksheet(GetCategoriesDataTable(categoryDtos), "Categories"); |
| 126 | + |
| 127 | + sheet1.Columns(1, 3).Style.Font.FontColor = XLColor.Black; |
| 128 | + sheet1.Row(1).CellsUsed().Style.Fill.BackgroundColor = XLColor.Black; |
| 129 | + sheet1.Row(1).Style.Font.FontColor = XLColor.White; |
| 130 | + |
| 131 | + sheet1.Column(1).Width = 5; |
| 132 | + sheet1.Columns(2, 3).Width = 12; |
| 133 | + |
| 134 | + sheet1.Row(1).Style.Font.FontSize = 15; |
| 135 | + sheet1.Row(1).Style.Font.Bold = true; |
| 136 | + sheet1.Row(1).Style.Font.Shadow = true; |
| 137 | + sheet1.Row(1).Style.Font.VerticalAlignment = XLFontVerticalTextAlignmentValues.Superscript; |
| 138 | + sheet1.Row(1).Style.Font.Italic = false; |
| 139 | + |
| 140 | + using MemoryStream ms = new(); |
| 141 | + wb.SaveAs(ms); |
| 142 | + |
| 143 | + return ms.ToArray(); |
| 144 | + } |
| 145 | + private List<object> ConvertCategoriesToData(IEnumerable<CategoryDto> categories) |
| 146 | + { |
| 147 | + List<object> data = new List<object>(); |
| 148 | + |
| 149 | + foreach (var category in categories) |
| 150 | + { |
| 151 | + data.Add(new { ID = category.Id, category.Name, category.NumberOfProducts }); |
| 152 | + } |
| 153 | + |
| 154 | + return data; |
| 155 | + } |
| 156 | + private static DataTable GetCategoriesDataTable(IEnumerable<CategoryDto> categories) |
| 157 | + { |
| 158 | + DataTable table = new DataTable(); |
| 159 | + table.TableName = "Categories Data"; |
| 160 | + table.Columns.Add("Id", typeof(int)); |
| 161 | + table.Columns.Add("Name", typeof(string)); |
| 162 | + table.Columns.Add("Number of Products", typeof(int)); |
| 163 | + |
| 164 | + foreach (var category in categories) |
| 165 | + { |
| 166 | + table.Rows.Add(category.Id, category.Name, category.NumberOfProducts); |
| 167 | + } |
| 168 | + |
| 169 | + return table; |
| 170 | + } |
109 | 171 |
|
110 |
| - //private DataTable GetCategoriesDataTable(IEnumerable<CategoryDto> categories) |
111 |
| - //{ |
112 |
| - // DataTable table = new DataTable(); |
113 |
| - // table.TableName = "Categories Data"; |
114 |
| - // table.Columns.Add("Id", typeof(int)); |
115 |
| - // table.Columns.Add("Name", typeof(string)); |
116 |
| - // table.Columns.Add("NumberOfProducts", typeof(string)); |
117 |
| - |
118 |
| - // foreach (var category in categories) |
119 |
| - // { |
120 |
| - // table.Rows.Add(category.Id, category.Name); |
121 |
| - // } |
122 |
| - |
123 |
| - // return table; |
124 |
| - //} |
125 | 172 | }
|
126 | 173 | }
|
0 commit comments