Exception Occurs While Opening a Downloaded Excel Sheet(.xls) In Asp.Net -
i download excel sheet using following code.
the above code starting code of logic.after downloaded, open excel sheet.it shows warning this
because of warning,when im trying upload same page mysql database using asp.net.it shows exception "page not in correct format".
this entire logic downloading excel sheet
protected void download_click(object sender, eventargs e) { exporttoexcel(sqldatasource1, "studentmarks"); } public void exporttoexcel(sqldatasource datasrc, string filename) { //add response header response.clear(); response.addheader("content-disposition", string.format("attachment;filename={0}.xls", filename)); response.charset = ""; response.contenttype = "application/ms-excel"; //get data database mysqlconnection cn = new mysqlconnection(datasrc.connectionstring); // string query = datasrc.selectcommand.replace("\r\n", " ").replace("\t", " "); mysqlcommand cmd9 = new mysqlcommand("select subject class_subject standard='" + dropdownlist1.selecteditem.value + "';", cn); // cn.open(); //dataset ds9=new dataset(); mysqldataadapter da9 = new mysqldataadapter(cmd9); // da9.fill(ds9); datatable dt = new datatable(); da9.fill(dt); stringbuilder sb9 = new stringbuilder(); (int count = 0; count < dt.rows.count; count++) { // string headerrowtext = gridview4.headerrow.cells[i].text; sb9.append("'"); sb9.append(dt.rows[count][0].tostring()); sb9.append("'"); if (count < dt.rows.count - 1) //if (count < count3 - 1) { sb9.append(","); } } label2.text = sb9.tostring(); //mysqlcommand fyearcmd = new mysqlcommand("select fyear student_data standard='" + dropdownlist1.selecteditem.value + "' , completed_status='running';", cn); //mysqldatareader fyeardr = fyearcmd.executereader(); string query = "select admission_num,name,'standard','fyear','type_of_exam'," + sb9.tostring() + " student_data standard='" + dropdownlist1.selecteditem.value + "' , fyear='" + dropdownlist4.selecteditem.value + "';"; mysqlcommand cmd = new mysqlcommand(query, cn); cmd.commandtimeout = 999999; cmd.commandtype = commandtype.text; try { cn.open(); mysqldatareader dr = cmd.executereader(); stringbuilder sb = new stringbuilder(); //session["fieldcount"] = dr.fieldcount.tostring(); //label1.text = dr.fieldcount.tostring(); //add header int count3 = 4; (int count = 0; count < dr.fieldcount; count++) //for (int count = 0; count < count3; count++) { if (dr.getname(count) != null) sb.append(dr.getname(count)); if (count < dr.fieldcount - 1) //if (count < count3 - 1) { sb.append("\t"); } } response.write(sb.tostring() + "\n"); response.flush(); //append data while (dr.read()) { sb = new stringbuilder(); //for (int col = 0; col < dr.fieldcount - 1; col++) (int col = 0; col <= count3; col++) { if (col < (count3 - 2)) { if (!dr.isdbnull(col)) sb.append(dr.getvalue(col).tostring().replace(",", " ")); sb.append("\t"); } if (col == (count3 - 2)) { if (!dr.isdbnull(col)) sb.append(dropdownlist1.selecteditem.text); sb.append("\t"); } if (col == (count3 - 1)) { if (!dr.isdbnull(col)) { //sb.append(dr.getvalue(col).tostring().replace(",", " ")); sb.append(dropdownlist4.selecteditem.text); } sb.append("\t"); } if (col == count3) { if (!dr.isdbnull(col)) { //sb.append(dr.getvalue(col).tostring().replace(",", " ")); sb.append(dropdownlist3.selecteditem.text); } sb.append("\t"); } } //if (!dr.isdbnull(dr.fieldcount - 1)) // sb.append(dr.getvalue(dr.fieldcount - 1).tostring().replace(",", " ")); response.write(sb.tostring() + "\n"); response.flush(); } dr.dispose(); } catch (exception ex) { response.write(ex.message); } { cmd.connection.close(); cn.close(); } response.end(); }
so please give me suggestion how download excel sheet out showing errors while opening.
you generating csv file, not excel file. try change header:
response.contenttype = "text/csv"; response.addheader("content-disposition", "attachment;filename=myfilename.csv");
normally excel default opening csv files.
Comments
Post a Comment