Editing Excel Worksheet Cells
-
Hey all. I have been fooling around with Excel and can create Worksheets, files, etc, however, when I try to modify the contents of a cell, I get the following exception: An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll Additional information: Exception from HRESULT: 0x800A03EC. The code is: Excel.Application excel = new Excel.ApplicationClass(); Excel.Workbook workbook = null; Excel.Sheets sheets = null; Excel.Worksheet currentSheet = null; workbook = excel.Workbooks.Add( Excel.XlWBATemplate.xlWBATWorksheet ); sheets = workbook.Worksheets; currentSheet = (Excel.Worksheet)sheets[ 1 ]; ((Excel.Range)currentSheet.Cells[ 0, 0 ]).Value2 = "text"; I have researched this on the net but to no avail. My code seemed to have worked before, but it doesn't now and it's ticking me off. I have tried several variations to modify the contents of the cell, but nada. Any help would be greatly appreciated! Thanks, Josh
-
Hey all. I have been fooling around with Excel and can create Worksheets, files, etc, however, when I try to modify the contents of a cell, I get the following exception: An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll Additional information: Exception from HRESULT: 0x800A03EC. The code is: Excel.Application excel = new Excel.ApplicationClass(); Excel.Workbook workbook = null; Excel.Sheets sheets = null; Excel.Worksheet currentSheet = null; workbook = excel.Workbooks.Add( Excel.XlWBATemplate.xlWBATWorksheet ); sheets = workbook.Worksheets; currentSheet = (Excel.Worksheet)sheets[ 1 ]; ((Excel.Range)currentSheet.Cells[ 0, 0 ]).Value2 = "text"; I have researched this on the net but to no avail. My code seemed to have worked before, but it doesn't now and it's ticking me off. I have tried several variations to modify the contents of the cell, but nada. Any help would be greatly appreciated! Thanks, Josh
-
Hi, ((Excel.Range)currentSheet.Cells[ 0, 0 ]).Value2 = "text"; cell[0,0] is an readonly cell:), you cant modify value in it. use ((Excel.Range)currentSheet.Cells[ 1, 1 ]).Value2 = "text"; hope this works. Nitin...
Thanks for the response. Interesting that you can't modify 0,0. Perhaps they should mention that somewhere. Anyway, I tried using different Cell coords, however, no data appeared, but I am not that concerned since I didn't receive an error. I ended up using this to modify cell contents: Excel.Range r = currentSheet.get_Range( "C1", Type.Missing ); r.Value2 = "Some Text"; r = currentSheet.get_Range( "D1", Type.Missing ); r.Value2 = "Some Text"; This seems to work fine. Thanks!