c# - Invalid Index. Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX) when Workbooks.Open -
i trying make app open xlsx
file reading, , read , stuff it. when run app, , click button load file, getting error:
invalid index. exception hresult: 0x8002000b (disp_e_badindex)
on line of code:
excel.workbook = excelapp.workbooks.open("c:\\test.xlsx", 0, true, 5, "", "", true, microsoft.office.interop.excel.xlplatform.xlwindows, "\t", false, false, 0, true, 1, 0);
can suggest wrong here?
edit: here full code hope easier tell causes error
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using excel = microsoft.office.interop.excel; using system.reflection; namespace windowsformsapplication2 { public partial class form1 : form, idisposable { public form1() { initializecomponent(); } private void button1_click(object sender, eventargs e) { excel.application excelapp = new excel.application(); excelapp.visible = true; excel.workbook = excelapp.workbooks.open("c:/test.xlsx"); // selectes used range of excel workbook , enters in // 2 dimentional array try { // reference first sheet of workbook. excel.sheets excelsheets = a.worksheets; string currentsheet = "sheet1"; excel.worksheet excelworksheet = (excel.worksheet)excelsheets.get_item(currentsheet); // write out console debugging textbox1.text = "excelworksheet " + excelworksheet; // range of data. excel.range excelcell = (excel.range)excelworksheet.get_range("a3", missing.value); // write out console debugging textbox1.text = "excelcell " + excelcell; // write out console debugging textbox1.text = "creating string[,] array. . . "; // retrieve data range. object[,] dataarray; // write out console debugging textbox1.text = "string[,] array created. . . "; dataarray = (system.object[,])excelcell.get_value(missing.value); // write out console debugging textbox1.text = "counting rows , columns. . . "; // determine dimensions of array. int irows; int icols; irows = dataarray.getupperbound(0); icols = dataarray.getupperbound(1); // write out console debugging textbox1.text = "printing array. . . "; // print data of array. (int rowcounter = 1; rowcounter <= irows; rowcounter++) { // write out console debugging textbox1.text = ("row " + rowcounter); (int colcounter = 1; colcounter <= icols; colcounter++) { // write next value console. richtextbox1.text = "col " + colcounter + "= " + dataarray[rowcounter, colcounter].tostring() + ", "; } // write in new line. richtextbox1.text = "\n"; } } catch (exception theexception) { // create error message string errormessage; errormessage = "error: "; errormessage = string.concat(errormessage, theexception.message); errormessage = string.concat(errormessage, " line: "); errormessage = string.concat(errormessage, theexception.source); // display error message messagebox.show(errormessage, "error"); } } } }
Comments
Post a Comment