I'm trying to modify and save data in Excel. With the code below, I'm accessing the sheet, performing modifications, then saving the file. I'm unable to save the file. Here is my code:
Application excel = new Application();
excel.Visible=true;
Workbook wb = (Workbook)excel.Workbooks.Open(@"C:\Users\dnyanesh.wagh\Desktop\BookExcel1.xlsx");
Worksheet ws = (Worksheet)wb.Worksheets[1];
ws.Cells[1, 1] = "sagar";
ws.Cells[2, 1] = "sagar";
ws.Cells[3, 1] = "sagar";
wb.Save();
wb.close();
I'm receiving this error: "the file named 'BookExcel1.xlsx' already exists in this location. Do you want to replace it?"
So I changed the code to:
Workbook wb = (Workbook)excel.Workbooks.Open(@"C:\Users\dnyanesh.wagh\Desktop\BookExcel1.xlsx",0, false, 5, "", "",
false, XlPlatform.xlWindows, "", true, false,
0, true, false, false););
Then error is: "BookExcel1.xlsx is being modified by user_name.open as read only". If I click the 'cancel' button, I receive the exception above with "Exception from HRESULT: 0x800A03EC"
I have also tried:
wb.SaveAs(@"C:\Users\dnyanesh.wagh\Desktop\BookExcel1.xlsx");
wb.Close(true,null,null);
From that I receive the same error, with the above file showing the modifications.
Can anybody tell me how can I save the file with modifications?