.net - C# Export to CSV problems -
i using method accepts datatable , transform csv. problem is, (1) when try call method, succeeding process/commands never executed. (2) list box contents calling form cleared out after calling method. can me out on this? have included method below reference.
public void datatabletocsv(string filename, datatable dt) { response.clear(); response.addheader("content-disposition", string.format("attachment;filename={0}.csv", filename + datetime.now.tostring().replace(" ", "").replace(":", "").replace(" ", "").replace("/", ""))); response.charset = ""; response.contenttype = "application/vnd.csv"; system.io.stringwriter swriter; system.text.stringbuilder sb = new system.text.stringbuilder(); swriter = new system.io.stringwriter(sb); string str; (int k = 0; k <= (dt.columns.count - 1); k++) { swriter.write(dt.columns[k].columnname + ","); } swriter.writeline(","); (int = 0; <= (dt.rows.count - 1); i++) { (int j = 0; j <= (dt.columns.count - 1); j++) { str = (dt.rows[i][j].tostring().replace(",", "")); if (str == " ") str = ""; str = (str + ","); swriter.write(str); } swriter.writeline(); } swriter.close(); response.write(sb.tostring()); response.end(); }
regarding (1) - response.end() throws threadabortexception internally, terminates normal page life-cycle , 'jumps ahead' endrequest event. following call datatabletocsv() not called.
hope helps..
Comments
Post a Comment