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);
The way I set the content of the cell is like this:
xlWorkSheet.Cells[8, 2].Value = "Salary";
I am using Excel 2010.
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With