How to copy, Edit ,save an excel file in a specific Location in C# using excel interop? i have following method which will edit the existing same excel.But i want to copy the original excel file and edit 2 colums and save that as a diffrent location with any name. so i should have the original one and new one with edited.Please suggest an efficient way My code which will edit the existing one and save as itself below.only 2 column values i have to edit
private void EditExcel(string fileName) { Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fileName); Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1]; // excel sheet Excel.Range xlRange = xlWorksheet.UsedRange; int lastUsedRow = xlWorksheet.Cells.Find("*", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlPrevious, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Row; for (int i = 2; i <= lastUsedRow; i++) { int j = 0 + i; xlWorksheet.Cells[i, 1] = "new value"; xlWorksheet.Cells[i, 2] = "new value"; xlWorksheet.Cells[i, 3] = xlWorksheet.Cells[i, 3] } //cleanup GC.Collect(); GC.WaitForPendingFinalizers(); //release com objects to fully kill excel process from running in the background Marshal.ReleaseComObject(xlRange); Marshal.ReleaseComObject(xlWorksheet); //close and release xlWorkbook.Close(); Marshal.ReleaseComObject(xlWorkbook); //quit and release xlApp.Quit(); Marshal.ReleaseComObject(xlApp); }
xlWorksheet.Cells[i, 3] = xlWorksheet.Cells[i, 3]
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)