Error Exporting to Excel C# -


i'm getting error when i'm exporting excel c#, can't find code wrong , solution problem

error :

an unhandled exception of type 'system.runtime.interopservices.comexception' occurred in gestãosi.exe

additional information: Índice inválido. (excepção de hresult: 0x8002000b (disp_e_badindex))

the error appear when code running

// add workbook. obook = oexcel_12.workbooks.add(omissing);  // worksheets collection  osheetscoll = oexcel_12.worksheets;  // worksheet "sheet1" osheet = (excel_12.worksheet)osheetscoll.get_item("sheet1"); 

here code

 public static void exportdatagridviewto_excel12(datagridview itemdatagridview)  {         excel_12.application oexcel_12 = null;                //excel_12 application         excel_12.workbook obook = null;                       // excel_12 workbook         excel_12.sheets osheetscoll = null;                   // excel_12 worksheets collection         excel_12.worksheet osheet = null;                     // excel_12 worksheet         excel_12.range orange = null;                         // cell or range in worksheet         object omissing = system.reflection.missing.value;          // create instance of excel_12.         oexcel_12 = new excel_12.application();          // make excel_12 visible user.         oexcel_12.visible = true;          // set usercontrol property excel_12 won't shut down.         oexcel_12.usercontrol = true;          // system.globalization.cultureinfo ci = new system.globalization.cultureinfo("en-us");          // add workbook.         obook = oexcel_12.workbooks.add(omissing);          // worksheets collection          osheetscoll = oexcel_12.worksheets;          // worksheet "sheet1"         osheet = (excel_12.worksheet)osheetscoll.get_item("sheet1");          // export titles         (int j = 0; j < itemdatagridview.columns.count; j++)         {             orange = (excel_12.range)osheet.cells[1, j + 1];             orange.value2 = itemdatagridview.columns[j].headertext;         }          // export data         (int = 0; < itemdatagridview.rows.count - 1; i++)         {             (int j = 0; j < itemdatagridview.columns.count; j++)             {                 orange = (excel_12.range)osheet.cells[i + 2, j + 1];                 orange.value2 = itemdatagridview[j, i].value;             }         }          // release variables.         //obook.close(false, omissing, omissing);         obook = null;          //oexcel_12.quit();         oexcel_12 = null;          // collect garbage.         gc.collect();     } 

this work me..

osheet = obook.worksheets.get_item(index); 

Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -