I am trying to modify and save data in Excel. Using the code below, I access the worksheet by making changes and then saving the file. I can not 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 get this error: "a file named" BookExcel1.xlsx "already exists in this place. 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 an error occurs: "BookExcel1.xlsx changes by username .open as read only." If I click the cancel button, I get the above exception with "Exception from HRESULT: 0x800A03EC"
I also tried:
wb.SaveAs(@"C:\Users\dnyanesh.wagh\Desktop\BookExcel1.xlsx"); wb.Close(true,null,null);
From this I get the same error, with the above file showing the changes.
Can someone tell me how can I save a file with changes?