Reputation: 3689
reading from Excel cells works perfect. But i have problems with writing new data to worksheet3 and cells[8,2].. How to fix this code?
I'm getting error:
System.Runtime.InteropServices.COMException: File not available.
But i can read from this file using other button.
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open("C:\\Base.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3);
// range = xlWorkSheet.UsedRange;
// Object[,] saRet;
// saRet = (System.Object[,])range.get_Value(Missing.Value);
xlWorkSheet.Cells[8, 2] = "Salary";
xlWorkBook.Close(true, null, null);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
Upvotes: 8
Views: 53801
Reputation: 1
Take a look at this line in your code:
xlWorkBook = xlApp.Workbooks.Open("C:\\Base.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
The third parameter defines is your file shoul be opened as read-only. You set it to true, that meens you can not modify your file. I think that is why you have an error "file not available".
Upvotes: 0
Reputation: 54
You are opening the excel file in readonly mode.
xlApp.Workbooks.Open("C:\\Base.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
And you are setting editable to false. This is probably the reason why you cannot edit the file...
xlApp.Workbooks.Open("C:\\Base.xls", 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", true, false, 0, true, 1, 0);
Upvotes: 1
Reputation: 11492
The way I set the content of the cell is like this:
xlWorkSheet.Cells[8, 2].Value = "Salary";
I am using Excel 2010.
Upvotes: 8
Reputation: 2072
Your code appears to be fine. The problem could be with the file.
Open file in Excel. Do you see any write protection message? Make sure that you are able to manually modify its content and can save the file. Give a try.
Upvotes: -1
Reputation: 124686
You can't set a range to a string:
xlWorkSheet.Cells[8, 2] = "Salary";
Try something like:
xlRange = (Excel.Range) xlWorkSheet.Cells[8, 2];
xlRange.Value = "Salary";
Upvotes: 1