|
| 1 | +using Microsoft.AspNetCore.Mvc; |
| 2 | +using Pictures.Models; |
| 3 | +using Syncfusion.XlsIO; |
| 4 | +using System.Diagnostics; |
| 5 | + |
| 6 | +namespace Pictures.Controllers |
| 7 | +{ |
| 8 | + public class HomeController : Controller |
| 9 | + { |
| 10 | + private readonly ILogger<HomeController> _logger; |
| 11 | + |
| 12 | + public HomeController(ILogger<HomeController> logger) |
| 13 | + { |
| 14 | + _logger = logger; |
| 15 | + } |
| 16 | + |
| 17 | + public IActionResult AddPicture() |
| 18 | + { |
| 19 | + // Create a new Excel engine instance |
| 20 | + ExcelEngine excelEngine = new ExcelEngine(); |
| 21 | + |
| 22 | + // Access the Excel application |
| 23 | + IApplication application = excelEngine.Excel; |
| 24 | + |
| 25 | + // Set default Excel version to XLSX |
| 26 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 27 | + |
| 28 | + // Open the input workbook stream |
| 29 | + FileStream fileStream = new FileStream("Data\\Input.xlsx", FileMode.Open, FileAccess.Read); |
| 30 | + |
| 31 | + // Open the workbook from the file stream |
| 32 | + IWorkbook workbook = application.Workbooks.Open(fileStream); |
| 33 | + |
| 34 | + // Get the first worksheet |
| 35 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 36 | + |
| 37 | + // Open the image stream to be inserted |
| 38 | + FileStream imageStream = new FileStream("Data\\Image.png", FileMode.Open, FileAccess.Read); |
| 39 | + |
| 40 | + // Add the image at row 10, column 2 |
| 41 | + IPictureShape picture = worksheet.Pictures.AddPicture(10, 2, imageStream); |
| 42 | + |
| 43 | + // Set the picture width |
| 44 | + picture.Width = 320; |
| 45 | + |
| 46 | + // Set the picture height |
| 47 | + picture.Height = 180; |
| 48 | + |
| 49 | + //Return the workbook as a downloadable file |
| 50 | + return ExportWorkbook(workbook, "AddPicture.xlsx"); |
| 51 | + } |
| 52 | + |
| 53 | + public IActionResult ModifyPicture() |
| 54 | + { |
| 55 | + // Create a new Excel engine instance |
| 56 | + ExcelEngine excelEngine = new ExcelEngine(); |
| 57 | + |
| 58 | + // Access the Excel application |
| 59 | + IApplication application = excelEngine.Excel; |
| 60 | + |
| 61 | + // Set default Excel version to XLSX |
| 62 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 63 | + |
| 64 | + // Open the input workbook stream |
| 65 | + FileStream fileStream = new FileStream("Data\\Template.xlsx", FileMode.Open, FileAccess.Read); |
| 66 | + |
| 67 | + // Open the workbook from the stream |
| 68 | + IWorkbook workbook = application.Workbooks.Open(fileStream); |
| 69 | + |
| 70 | + // Get the first worksheet |
| 71 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 72 | + |
| 73 | + // Get the first picture in the worksh |
| 74 | + IPictureShape picture = worksheet.Pictures[0]; |
| 75 | + |
| 76 | + // Set vertical position |
| 77 | + picture.Top = 150; |
| 78 | + |
| 79 | + // Set horizontal position |
| 80 | + picture.Left = 200; |
| 81 | + |
| 82 | + // Set the new width of the picture |
| 83 | + picture.Width = 420; |
| 84 | + |
| 85 | + // Set the new height of the picture |
| 86 | + picture.Height = 240; |
| 87 | + |
| 88 | + //Return the workbook as a downloadable file |
| 89 | + return ExportWorkbook(workbook, "ModifyPicture.xlsx"); |
| 90 | + } |
| 91 | + |
| 92 | + public IActionResult RemovePicture() |
| 93 | + { |
| 94 | + // Create a new Excel engine instance |
| 95 | + ExcelEngine excelEngine = new ExcelEngine(); |
| 96 | + |
| 97 | + // Access the Excel application |
| 98 | + IApplication application = excelEngine.Excel; |
| 99 | + |
| 100 | + // Set default Excel version to XLSX |
| 101 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 102 | + |
| 103 | + // Open the template workbook stream |
| 104 | + FileStream fileStream = new FileStream("Data\\Template.xlsx", FileMode.Open, FileAccess.Read); |
| 105 | + |
| 106 | + // Open the workbook from the stream |
| 107 | + IWorkbook workbook = application.Workbooks.Open(fileStream); |
| 108 | + |
| 109 | + // Get the first worksheet |
| 110 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 111 | + |
| 112 | + // Get the first picture in the worksheet |
| 113 | + IPictureShape picture = worksheet.Pictures[0]; |
| 114 | + |
| 115 | + // Remove the selected picture from the worksheet |
| 116 | + picture.Remove(); |
| 117 | + |
| 118 | + |
| 119 | + // return the updated workbook as a downloadable file |
| 120 | + return ExportWorkbook(workbook, "RemovePicture.xlsx"); |
| 121 | + } |
| 122 | + |
| 123 | + private FileStreamResult ExportWorkbook(IWorkbook workbook, string fileName) |
| 124 | + { |
| 125 | + // Create an in-memory stream to hold the workbook |
| 126 | + MemoryStream stream = new MemoryStream(); |
| 127 | + |
| 128 | + // Save the workbook into the memory stream |
| 129 | + workbook.SaveAs(stream); |
| 130 | + |
| 131 | + // Reset the stream position to the beginnin |
| 132 | + stream.Position = 0; |
| 133 | + |
| 134 | + // Return the stream as a downloadable file |
| 135 | + return File(stream, "application/xlsx", fileName); |
| 136 | + } |
| 137 | + |
| 138 | + public IActionResult Index() |
| 139 | + { |
| 140 | + return View(); |
| 141 | + } |
| 142 | + |
| 143 | + public IActionResult Privacy() |
| 144 | + { |
| 145 | + return View(); |
| 146 | + } |
| 147 | + |
| 148 | + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] |
| 149 | + public IActionResult Error() |
| 150 | + { |
| 151 | + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments