c# - Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD)) -
this question has answer here:
the error appeared when exporting data in datagrid view excel sheet:
error (old format or invalid type library. (exception hresult: 0x80028018 (type_e_invdataread)))
on line:
microsoft.office.interop.excel._workbook workbook = app.workbooks.add(type.missing);
how fix problem?
my full code:
private void button1_click(object sender, eventargs e) { system.globalization.cultureinfo oldci = system.threading.thread.currentthread.currentculture; system.threading.thread.currentthread.currentculture = new system.globalization.cultureinfo("en-us"); // creating excel application microsoft.office.interop.excel._application app = new microsoft.office.interop.excel.application(); system.threading.thread.currentthread.currentculture = oldci; // creating new workbook within excel application microsoft.office.interop.excel._workbook workbook = app.workbooks.add(type.missing); // creating new excel sheet in workbook microsoft.office.interop.excel._worksheet worksheet = null; // see excel sheet behind program //funny app.visible = true; // reference of first sheet. default name sheet1. // store reference worksheet try { // fixed:(microsoft.office.interop.excel.worksheet) worksheet = (microsoft.office.interop.excel.worksheet)workbook.sheets["sheet1"]; worksheet = (microsoft.office.interop.excel.worksheet)workbook.activesheet; // changing name of active sheet worksheet.name = "exported ketoan"; // storing header part in excel (int = 1; < dgdata.columns.count + 1; i++) { worksheet.cells[1, i] = dgdata.columns[i - 1].headertext; } // storing each row , column value excel sheet (int = 0; < dgdata.rows.count - 1; i++) { (int j = 0; j < dgdata.columns.count; j++) { worksheet.cells[i + 2, j + 1] = dgdata.rows[i].cells[j].value.tostring(); } } // save application string filename = string.empty; savefiledialog savefileexcel = new savefiledialog(); savefileexcel.filter = "excel files |*.xls|all files (*.*)|*.*"; savefileexcel.filterindex = 2; savefileexcel.restoredirectory = true; if (savefileexcel.showdialog() == dialogresult.ok) { filename = savefileexcel.filename; //fixed-old code: 11 para->add 1:type.missing workbook.saveas(filename, type.missing, type.missing, type.missing, type.missing, type.missing, microsoft.office.interop.excel.xlsaveasaccessmode.xlexclusive, type.missing, type.missing, type.missing, type.missing, type.missing); } else return; // exit application //app.quit(); } catch (system.exception ex) { } { app.quit(); workbook = null; app = null; } }
your code works fine on office 2007 + vs 2010. versions using? mayby have choosen wrong version of interop refernece: office 2007 = 12.0.0.0, office 2010 = 14.0.0.0
you can @ http://support.microsoft.com/default.aspx?scid=kb;en-us;320369 might solve problem.
Comments
Post a Comment