From 174c54bc0becc8515a969639debf0aa99c67afb1 Mon Sep 17 00:00:00 2001 From: KurmithaSF4004 Date: Thu, 1 Aug 2024 17:25:32 +0530 Subject: [PATCH] Loading and Saving --- .../Edit Excel/Controllers/HomeController.cs | 44 ++++++++----------- .../Edit Excel/Edit Excel/Edit Excel.csproj | 2 +- .../Create Excel/Create Excel.csproj | 2 +- 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/Loading and Saving/Drop Box/Loading/Edit Excel/Edit Excel/Controllers/HomeController.cs b/Loading and Saving/Drop Box/Loading/Edit Excel/Edit Excel/Controllers/HomeController.cs index d92c7d12..c6dd84b0 100644 --- a/Loading and Saving/Drop Box/Loading/Edit Excel/Edit Excel/Controllers/HomeController.cs +++ b/Loading and Saving/Drop Box/Loading/Edit Excel/Edit Excel/Controllers/HomeController.cs @@ -17,42 +17,34 @@ public HomeController(ILogger logger) } public async Task EditDocument() { - try + using (ExcelEngine excelEngine = new ExcelEngine()) { - //Retrieve the document from DropBox + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Xlsx; + + //Download the document from DropBox MemoryStream stream = await GetDocumentFromDropBox(); //Set the position as '0' stream.Position = 0; - using (ExcelEngine excelEngine = new ExcelEngine()) - { - IApplication application = excelEngine.Excel; - application.DefaultVersion = ExcelVersion.Xlsx; + //Load the downloaded document + IWorkbook workbook = application.Workbooks.Open(stream); - //Load the downloaded document - IWorkbook workbook = application.Workbooks.Open(stream); + IWorksheet worksheet = workbook.Worksheets[0]; + worksheet.Range["A3"].Text = "Hello world"; - IWorksheet worksheet = workbook.Worksheets[0]; - worksheet.Range["A3"].Text = "Hello world"; + //Saving the Excel to the MemoryStream + MemoryStream outputStream = new MemoryStream(); + workbook.SaveAs(outputStream); - //Saving the Excel to the MemoryStream - MemoryStream outputStream = new MemoryStream(); - workbook.SaveAs(outputStream); - - //Set the position as '0' - outputStream.Position = 0; + //Set the position as '0' + outputStream.Position = 0; - //Download the Excel file in the browser - FileStreamResult fileStreamResult = new FileStreamResult(outputStream, "application/excel"); - fileStreamResult.FileDownloadName = "EditExcel.xlsx"; - return fileStreamResult; - } - } - catch (Exception ex) - { - Console.WriteLine($"Error: {ex.Message}"); - return Content("Error occurred while processing the file."); + //Download the Excel file in the browser + FileStreamResult fileStreamResult = new FileStreamResult(outputStream, "application/excel"); + fileStreamResult.FileDownloadName = "EditExcel.xlsx"; + return fileStreamResult; } } diff --git a/Loading and Saving/Drop Box/Loading/Edit Excel/Edit Excel/Edit Excel.csproj b/Loading and Saving/Drop Box/Loading/Edit Excel/Edit Excel/Edit Excel.csproj index b462b5d7..dd29fb3f 100644 --- a/Loading and Saving/Drop Box/Loading/Edit Excel/Edit Excel/Edit Excel.csproj +++ b/Loading and Saving/Drop Box/Loading/Edit Excel/Edit Excel/Edit Excel.csproj @@ -9,7 +9,7 @@ - + diff --git a/Loading and Saving/Drop Box/Saving/Create Excel/Create Excel/Create Excel.csproj b/Loading and Saving/Drop Box/Saving/Create Excel/Create Excel/Create Excel.csproj index 2fdc9d0e..95d79e2c 100644 --- a/Loading and Saving/Drop Box/Saving/Create Excel/Create Excel/Create Excel.csproj +++ b/Loading and Saving/Drop Box/Saving/Create Excel/Create Excel/Create Excel.csproj @@ -19,7 +19,7 @@ - +