I am able to write data to excel with the help of Interop in .Net Core but is there any way by which it is possible that it will not change the existing excel template.
Actually I have an excel template and for multiple users, I want to have the different excel data but my code is writing data to my main file which I don’t want.
Please help me with this scenario.
I have used the following code to write:
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
//Microsoft Excel 16 object in references-> COM tab
using Excel = Microsoft.Office.Interop.Excel;
Application xlApp = new Application();
float Left;
float Top;
string FilePath = DefaultImagesPath;
Workbook xlWorkbook = xlApp.Workbooks.Open(FileName);
_Worksheet xlWorksheet = (_Worksheet)xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
xlWorksheet.Cells[5, 5] = "text";
//For Image
Excel.Range primaryOkImageRange = (Excel.Range)xlWorksheet.Cells[20, 15];
Left = (float)((double)primaryOkImageRange.Left);
Top = (float)((double)primaryOkImageRange.Top);
xlWorksheet.Shapes.AddPicture(FilePath, MsoTriState.msoFalse, MsoTriState.msoCTrue,
Left, Top, 50, 50);
xlApp.Visible = false;
xlApp.UserControl = false;
//cleanup
GC.Collect();
GC.WaitForPendingFinalizers();
//close and release
xlWorkbook.Close();
//quit and release
xlApp.Quit();
What i have tried to clear the existing value:
var rng = (Excel.Range)xlWorksheet.Cells[5, 5];
Left = (float)((double)rng.Left);
Top = (float)((double)rng.Top);
rng.Cells.Clear();
But this is not working.