|
| 1 | +using System.Diagnostics; |
| 2 | +using Comments.Models; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using Syncfusion.XlsIO; |
| 5 | + |
| 6 | +namespace Comments.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 AddComment() |
| 18 | + { |
| 19 | + // Initialize the Excel engine |
| 20 | + ExcelEngine excelEngine = new ExcelEngine(); |
| 21 | + IApplication application = excelEngine.Excel; |
| 22 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 23 | + |
| 24 | + // Open the Excel workbook from the specified template |
| 25 | + IWorkbook workbook = application.Workbooks.Open("Data\\CommentsTemplate.xlsx", ExcelOpenType.Automatic); |
| 26 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 27 | + |
| 28 | + // Add a threaded comment to cell H16 |
| 29 | + IThreadedComment threadedComment = worksheet.Range["H16"].AddThreadedComment( |
| 30 | + "What is the reason for the higher total amount of \"desk\" in the west region?", |
| 31 | + "User1", |
| 32 | + DateTime.Now |
| 33 | + ); |
| 34 | + |
| 35 | + // Export the modified workbook as a downloadable Excel file |
| 36 | + return ExportWorkbook(workbook, "AddComment.xlsx"); |
| 37 | + } |
| 38 | + |
| 39 | + public IActionResult ReplyComment() |
| 40 | + { |
| 41 | + // Initialize the Excel engine |
| 42 | + ExcelEngine excelEngine = new ExcelEngine(); |
| 43 | + IApplication application = excelEngine.Excel; |
| 44 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 45 | + |
| 46 | + // Open the workbook containing the comment to reply to |
| 47 | + IWorkbook workbook = application.Workbooks.Open("Data\\ReplyInput.xlsx", ExcelOpenType.Automatic); |
| 48 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 49 | + |
| 50 | + // Access the collection of threaded comments |
| 51 | + IThreadedComments threadedComments = worksheet.ThreadedComments; |
| 52 | + |
| 53 | + // Add a reply to the first threaded comment |
| 54 | + threadedComments[0].AddReply( |
| 55 | + "The unit cost of desk is higher compared to other items in the west region. As a result, the total amount is elevated.", |
| 56 | + "User2", |
| 57 | + DateTime.Now |
| 58 | + ); |
| 59 | + |
| 60 | + // Export the modified workbook |
| 61 | + return ExportWorkbook(workbook, "ReplyComment.xlsx"); |
| 62 | + } |
| 63 | + |
| 64 | + public IActionResult ResolveComment() |
| 65 | + { |
| 66 | + // Initialize the Excel engine |
| 67 | + ExcelEngine excelEngine = new ExcelEngine(); |
| 68 | + IApplication application = excelEngine.Excel; |
| 69 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 70 | + |
| 71 | + // Open the workbook containing the comment to resolve |
| 72 | + IWorkbook workbook = application.Workbooks.Open("Data\\ResolveInput.xlsx", ExcelOpenType.Automatic); |
| 73 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 74 | + |
| 75 | + // Access the threaded comments |
| 76 | + IThreadedComments threadedComments = worksheet.ThreadedComments; |
| 77 | + |
| 78 | + // Mark the first comment as resolved |
| 79 | + threadedComments[0].IsResolved = true; |
| 80 | + |
| 81 | + // Export the updated workbook |
| 82 | + return ExportWorkbook(workbook, "ResolveComment.xlsx"); |
| 83 | + } |
| 84 | + |
| 85 | + public IActionResult DeleteComment() |
| 86 | + { |
| 87 | + // Initialize the Excel engine |
| 88 | + ExcelEngine excelEngine = new ExcelEngine(); |
| 89 | + IApplication application = excelEngine.Excel; |
| 90 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 91 | + |
| 92 | + // Open the workbook containing the comment to delete |
| 93 | + IWorkbook workbook = application.Workbooks.Open("Data\\DeleteInput.xlsx", ExcelOpenType.Automatic); |
| 94 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 95 | + |
| 96 | + // Access the threaded comments |
| 97 | + IThreadedComments threadedComments = worksheet.ThreadedComments; |
| 98 | + |
| 99 | + // Delete the first threaded comment |
| 100 | + threadedComments[0].Delete(); |
| 101 | + |
| 102 | + // Export the updated workbook |
| 103 | + return ExportWorkbook(workbook, "DeleteComment.xlsx"); |
| 104 | + } |
| 105 | + |
| 106 | + public IActionResult ClearComment() |
| 107 | + { |
| 108 | + // Initialize the Excel engine |
| 109 | + ExcelEngine excelEngine = new ExcelEngine(); |
| 110 | + IApplication application = excelEngine.Excel; |
| 111 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 112 | + |
| 113 | + // Open the workbook containing comments to clear |
| 114 | + IWorkbook workbook = application.Workbooks.Open("Data\\ClearInput.xlsx", ExcelOpenType.Automatic); |
| 115 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 116 | + |
| 117 | + // Access all threaded comments |
| 118 | + IThreadedComments threadedComments = worksheet.ThreadedComments; |
| 119 | + |
| 120 | + // Clear all threaded comments from the worksheet |
| 121 | + threadedComments.Clear(); |
| 122 | + |
| 123 | + // Export the cleaned workbook |
| 124 | + return ExportWorkbook(workbook, "ClearComment.xlsx"); |
| 125 | + } |
| 126 | + |
| 127 | + private FileStreamResult ExportWorkbook(IWorkbook workbook, string fileName) |
| 128 | + { |
| 129 | + |
| 130 | + // Create a memory stream to hold the Excel file content |
| 131 | + MemoryStream stream = new MemoryStream(); |
| 132 | + |
| 133 | + // Save the workbook to the memory stream |
| 134 | + workbook.SaveAs(stream); |
| 135 | + |
| 136 | + // Reset the stream position to the beginning |
| 137 | + stream.Position = 0; |
| 138 | + |
| 139 | + // Return the stream as a downloadable Excel file with the specified filename |
| 140 | + return File(stream, "application/xlsx", fileName); |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + public IActionResult Index() |
| 145 | + { |
| 146 | + return View(); |
| 147 | + } |
| 148 | + |
| 149 | + public IActionResult Privacy() |
| 150 | + { |
| 151 | + return View(); |
| 152 | + } |
| 153 | + |
| 154 | + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] |
| 155 | + public IActionResult Error() |
| 156 | + { |
| 157 | + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments