|
| 1 | +using System.Diagnostics; |
| 2 | +using Create_Excel.Models; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using Syncfusion.XlsIO; |
| 5 | +using static Syncfusion.XlsIO.Parser.Biff_Records.Charts.ChartPicfRecord; |
| 6 | +using Syncfusion.XlsIO.Implementation.Collections; |
| 7 | +using Syncfusion.Drawing; |
| 8 | + |
| 9 | +namespace Create_Excel.Controllers |
| 10 | +{ |
| 11 | + public class HomeController : Controller |
| 12 | + { |
| 13 | + private readonly ILogger<HomeController> _logger; |
| 14 | + |
| 15 | + public HomeController(ILogger<HomeController> logger) |
| 16 | + { |
| 17 | + _logger = logger; |
| 18 | + } |
| 19 | + |
| 20 | + public IActionResult Index() |
| 21 | + { |
| 22 | + return View(); |
| 23 | + } |
| 24 | + |
| 25 | + public ActionResult CreateExcelDocument() |
| 26 | + { |
| 27 | + using (ExcelEngine excelEngine = new ExcelEngine()) |
| 28 | + { |
| 29 | + IApplication application = excelEngine.Excel; |
| 30 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 31 | + |
| 32 | + //Create a workbook |
| 33 | + IWorkbook workbook = application.Workbooks.Create(1); |
| 34 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 35 | + |
| 36 | + //Adding a picture |
| 37 | + FileStream imageStream = new FileStream("AdventureCycles-Logo.png", FileMode.Open, FileAccess.Read); |
| 38 | + IPictureShape shape = worksheet.Pictures.AddPicture(1, 1, imageStream, 20, 20); |
| 39 | + |
| 40 | + //Disable gridlines in the worksheet |
| 41 | + worksheet.IsGridLinesVisible = false; |
| 42 | + |
| 43 | + //Enter values to the cells from A3 to A5 |
| 44 | + worksheet.Range["A3"].Text = "46036 Michigan Ave"; |
| 45 | + worksheet.Range["A4"].Text = "Canton, USA"; |
| 46 | + worksheet.Range["A5"].Text = "Phone: +1 231-231-2310"; |
| 47 | + |
| 48 | + //Make the text bold |
| 49 | + worksheet.Range["A3:A5"].CellStyle.Font.Bold = true; |
| 50 | + |
| 51 | + //Merge cells |
| 52 | + worksheet.Range["D1:E1"].Merge(); |
| 53 | + |
| 54 | + //Enter text to the cell D1 and apply formatting. |
| 55 | + worksheet.Range["D1"].Text = "INVOICE"; |
| 56 | + worksheet.Range["D1"].CellStyle.Font.Bold = true; |
| 57 | + worksheet.Range["D1"].CellStyle.Font.RGBColor = Color.FromArgb(42, 118, 189); |
| 58 | + worksheet.Range["D1"].CellStyle.Font.Size = 35; |
| 59 | + |
| 60 | + //Apply alignment in the cell D1 |
| 61 | + worksheet.Range["D1"].CellStyle.HorizontalAlignment = ExcelHAlign.HAlignRight; |
| 62 | + worksheet.Range["D1"].CellStyle.VerticalAlignment = ExcelVAlign.VAlignTop; |
| 63 | + |
| 64 | + //Enter values to the cells from D5 to E8 |
| 65 | + worksheet.Range["D5"].Text = "INVOICE#"; |
| 66 | + worksheet.Range["E5"].Text = "DATE"; |
| 67 | + worksheet.Range["D6"].Number = 1028; |
| 68 | + worksheet.Range["E6"].Value = "12/31/2018"; |
| 69 | + worksheet.Range["D7"].Text = "CUSTOMER ID"; |
| 70 | + worksheet.Range["E7"].Text = "TERMS"; |
| 71 | + worksheet.Range["D8"].Number = 564; |
| 72 | + worksheet.Range["E8"].Text = "Due Upon Receipt"; |
| 73 | + |
| 74 | + //Apply RGB backcolor to the cells from D5 to E8 |
| 75 | + worksheet.Range["D5:E5"].CellStyle.Color = Color.FromArgb(42, 118, 189); |
| 76 | + worksheet.Range["D7:E7"].CellStyle.Color = Color.FromArgb(42, 118, 189); |
| 77 | + |
| 78 | + //Apply known colors to the text in cells D5 to E8 |
| 79 | + worksheet.Range["D5:E5"].CellStyle.Font.Color = ExcelKnownColors.White; |
| 80 | + worksheet.Range["D7:E7"].CellStyle.Font.Color = ExcelKnownColors.White; |
| 81 | + |
| 82 | + //Make the text as bold from D5 to E8 |
| 83 | + worksheet.Range["D5:E8"].CellStyle.Font.Bold = true; |
| 84 | + |
| 85 | + //Apply alignment to the cells from D5 to E8 |
| 86 | + worksheet.Range["D5:E8"].CellStyle.HorizontalAlignment = ExcelHAlign.HAlignCenter; |
| 87 | + worksheet.Range["D5:E5"].CellStyle.VerticalAlignment = ExcelVAlign.VAlignCenter; |
| 88 | + worksheet.Range["D7:E7"].CellStyle.VerticalAlignment = ExcelVAlign.VAlignCenter; |
| 89 | + worksheet.Range["D6:E6"].CellStyle.VerticalAlignment = ExcelVAlign.VAlignTop; |
| 90 | + |
| 91 | + //Enter value and applying formatting in the cell A7 |
| 92 | + worksheet.Range["A7"].Text = " BILL TO"; |
| 93 | + worksheet.Range["A7"].CellStyle.Color = Color.FromArgb(42, 118, 189); |
| 94 | + worksheet.Range["A7"].CellStyle.Font.Bold = true; |
| 95 | + worksheet.Range["A7"].CellStyle.Font.Color = ExcelKnownColors.White; |
| 96 | + |
| 97 | + //Apply alignment |
| 98 | + worksheet.Range["A7"].CellStyle.HorizontalAlignment = ExcelHAlign.HAlignLeft; |
| 99 | + worksheet.Range["A7"].CellStyle.VerticalAlignment = ExcelVAlign.VAlignCenter; |
| 100 | + |
| 101 | + //Enter values in the cells A8 to A12 |
| 102 | + worksheet.Range["A8"].Text = "Steyn"; |
| 103 | + worksheet.Range["A9"].Text = "Great Lakes Food Market"; |
| 104 | + worksheet.Range["A10"].Text = "20 Whitehall Rd"; |
| 105 | + worksheet.Range["A11"].Text = "North Muskegon,USA"; |
| 106 | + worksheet.Range["A12"].Text = "+1 231-654-0000"; |
| 107 | + |
| 108 | + //Create a Hyperlink for e-mail in the cell A13 |
| 109 | + IHyperLink hyperlink = worksheet.HyperLinks.Add(worksheet.Range["A13"]); |
| 110 | + hyperlink.Type = ExcelHyperLinkType.Url; |
| 111 | + hyperlink.Address = "[email protected]"; |
| 112 | + hyperlink.ScreenTip = "Send Mail"; |
| 113 | + |
| 114 | + //Merge column A and B from row 15 to 22 |
| 115 | + worksheet.Range["A15:B15"].Merge(); |
| 116 | + worksheet.Range["A16:B16"].Merge(); |
| 117 | + worksheet.Range["A17:B17"].Merge(); |
| 118 | + worksheet.Range["A18:B18"].Merge(); |
| 119 | + worksheet.Range["A19:B19"].Merge(); |
| 120 | + worksheet.Range["A20:B20"].Merge(); |
| 121 | + worksheet.Range["A21:B21"].Merge(); |
| 122 | + worksheet.Range["A22:B22"].Merge(); |
| 123 | + |
| 124 | + //Enter details of products and prices |
| 125 | + worksheet.Range["A15"].Text = " DESCRIPTION"; |
| 126 | + worksheet.Range["C15"].Text = "QTY"; |
| 127 | + worksheet.Range["D15"].Text = "UNIT PRICE"; |
| 128 | + worksheet.Range["E15"].Text = "AMOUNT"; |
| 129 | + worksheet.Range["A16"].Text = "Cabrales Cheese"; |
| 130 | + worksheet.Range["A17"].Text = "Chocos"; |
| 131 | + worksheet.Range["A18"].Text = "Pasta"; |
| 132 | + worksheet.Range["A19"].Text = "Cereals"; |
| 133 | + worksheet.Range["A20"].Text = "Ice Cream"; |
| 134 | + worksheet.Range["C16"].Number = 3; |
| 135 | + worksheet.Range["C17"].Number = 2; |
| 136 | + worksheet.Range["C18"].Number = 1; |
| 137 | + worksheet.Range["C19"].Number = 4; |
| 138 | + worksheet.Range["C20"].Number = 3; |
| 139 | + worksheet.Range["D16"].Number = 21; |
| 140 | + worksheet.Range["D17"].Number = 54; |
| 141 | + worksheet.Range["D18"].Number = 10; |
| 142 | + worksheet.Range["D19"].Number = 20; |
| 143 | + worksheet.Range["D20"].Number = 30; |
| 144 | + worksheet.Range["D23"].Text = "Total"; |
| 145 | + |
| 146 | + //Apply number format |
| 147 | + worksheet.Range["D16:E22"].NumberFormat = "$0.00"; |
| 148 | + worksheet.Range["E23"].NumberFormat = "$0.00"; |
| 149 | + |
| 150 | + //Apply incremental formula for column Amount by multiplying Qty and UnitPrice |
| 151 | + application.EnableIncrementalFormula = true; |
| 152 | + worksheet.Range["E16:E20"].Formula = "=C16*D16"; |
| 153 | + |
| 154 | + //Formula for Sum the total |
| 155 | + worksheet.Range["E23"].Formula = "=SUM(E16:E22)"; |
| 156 | + |
| 157 | + //Apply borders |
| 158 | + worksheet.Range["A16:E22"].CellStyle.Borders[ExcelBordersIndex.EdgeTop].LineStyle = ExcelLineStyle.Thin; |
| 159 | + worksheet.Range["A16:E22"].CellStyle.Borders[ExcelBordersIndex.EdgeBottom].LineStyle = ExcelLineStyle.Thin; |
| 160 | + worksheet.Range["A16:E22"].CellStyle.Borders[ExcelBordersIndex.EdgeTop].Color = ExcelKnownColors.Grey_25_percent; |
| 161 | + worksheet.Range["A16:E22"].CellStyle.Borders[ExcelBordersIndex.EdgeBottom].Color = ExcelKnownColors.Grey_25_percent; |
| 162 | + worksheet.Range["A23:E23"].CellStyle.Borders[ExcelBordersIndex.EdgeTop].LineStyle = ExcelLineStyle.Thin; |
| 163 | + worksheet.Range["A23:E23"].CellStyle.Borders[ExcelBordersIndex.EdgeBottom].LineStyle = ExcelLineStyle.Thin; |
| 164 | + worksheet.Range["A23:E23"].CellStyle.Borders[ExcelBordersIndex.EdgeTop].Color = ExcelKnownColors.Black; |
| 165 | + worksheet.Range["A23:E23"].CellStyle.Borders[ExcelBordersIndex.EdgeBottom].Color = ExcelKnownColors.Black; |
| 166 | + |
| 167 | + //Apply font setting for cells with product details |
| 168 | + worksheet.Range["A3:E23"].CellStyle.Font.FontName = "Arial"; |
| 169 | + worksheet.Range["A3:E23"].CellStyle.Font.Size = 10; |
| 170 | + worksheet.Range["A15:E15"].CellStyle.Font.Color = ExcelKnownColors.White; |
| 171 | + worksheet.Range["A15:E15"].CellStyle.Font.Bold = true; |
| 172 | + worksheet.Range["D23:E23"].CellStyle.Font.Bold = true; |
| 173 | + |
| 174 | + //Apply cell color |
| 175 | + worksheet.Range["A15:E15"].CellStyle.Color = Color.FromArgb(42, 118, 189); |
| 176 | + |
| 177 | + //Apply alignment to cells with product details |
| 178 | + worksheet.Range["A15"].CellStyle.HorizontalAlignment = ExcelHAlign.HAlignLeft; |
| 179 | + worksheet.Range["C15:C22"].CellStyle.HorizontalAlignment = ExcelHAlign.HAlignCenter; |
| 180 | + worksheet.Range["D15:E15"].CellStyle.HorizontalAlignment = ExcelHAlign.HAlignCenter; |
| 181 | + |
| 182 | + //Apply row height and column width to look good |
| 183 | + worksheet.Range["A1"].ColumnWidth = 36; |
| 184 | + worksheet.Range["B1"].ColumnWidth = 11; |
| 185 | + worksheet.Range["C1"].ColumnWidth = 8; |
| 186 | + worksheet.Range["D1:E1"].ColumnWidth = 18; |
| 187 | + worksheet.Range["A1"].RowHeight = 47; |
| 188 | + worksheet.Range["A2"].RowHeight = 15; |
| 189 | + worksheet.Range["A3:A4"].RowHeight = 15; |
| 190 | + worksheet.Range["A5"].RowHeight = 18; |
| 191 | + worksheet.Range["A6"].RowHeight = 29; |
| 192 | + worksheet.Range["A7"].RowHeight = 18; |
| 193 | + worksheet.Range["A8"].RowHeight = 15; |
| 194 | + worksheet.Range["A9:A14"].RowHeight = 15; |
| 195 | + worksheet.Range["A15:A23"].RowHeight = 18; |
| 196 | + |
| 197 | + //Saving the Excel to the MemoryStream |
| 198 | + MemoryStream stream = new MemoryStream(); |
| 199 | + workbook.SaveAs(stream); |
| 200 | + |
| 201 | + //Set the position as '0'. |
| 202 | + stream.Position = 0; |
| 203 | + |
| 204 | + //Download Excel document in the browser. |
| 205 | + return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Sample.xlsx"); |
| 206 | + } |
| 207 | + |
| 208 | + } |
| 209 | + public IActionResult Privacy() |
| 210 | + { |
| 211 | + return View(); |
| 212 | + } |
| 213 | + |
| 214 | + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] |
| 215 | + public IActionResult Error() |
| 216 | + { |
| 217 | + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments