c# - Issues with creating Excel file and inserting data into sheet -
i'm trying create excel file , insert data excel file. creation of excel file doesnt fail, when try insert data sheet in excel file error:
the microsoft office access database engine not find object 'sheet1$'. make sure object exists , spell name , path name correctly.
this code:
private void exporttoexcel_click(object sender, eventargs e) { string excelpath = environment.getfolderpath(environment.specialfolder.desktop) + @"\trackitexportfile - " + datetime.today.toshortdatestring(); file.exists(excelpath); microsoft.office.interop.excel.application excel = null; microsoft.office.interop.excel._workbook workbook = null; microsoft.office.interop.excel.sheets sheet = null; microsoft.office.interop.excel._worksheet newsheet = null; object misvalue = missing.value; excel = new microsoft.office.interop.excel.application(); excel.visible = true; workbook = (microsoft.office.interop.excel._workbook)(excel.workbooks.add(missing.value)); sheet = workbook.sheets microsoft.office.interop.excel.sheets; newsheet = (microsoft.office.interop.excel.worksheet)sheet.add(sheet[1], type.missing, type.missing, type.missing); workbook.saveas(excelpath, microsoft.office.interop.excel.xlfileformat.xlworkbookdefault, misvalue, misvalue, misvalue, misvalue, microsoft.office.interop.excel.xlsaveasaccessmode.xlexclusive, misvalue, misvalue, misvalue, misvalue, misvalue); workbook.close(); excel.quit(); releaseobject(workbook); releaseobject(excel); releaseobject(newsheet); releaseobject(sheet); insertdata(excelpath); } public void insertdata(string filename) { system.data.oledb.oledbconnection connection; system.data.oledb.oledbcommand mycommand = new system.data.oledb.oledbcommand(); string sql = null; connection = new system.data.oledb.oledbconnection("provider=microsoft.ace.oledb.12.0;data source=" + filename + ";extended properties=\"excel 12.0;readonly=false;hdr=yes;\""); connection.open(); mycommand.connection = connection; sql = "insert [sheet1$](a1) values ('" + ds.tables[0].columns[0] + "')"; mycommand.commandtext = sql; mycommand.executenonquery(); connection.close(); }
am creating file wrong?
Comments
Post a Comment