Http://social.microsoft.com/Forums/zh-CN/295/thread/14a833f5-95bf-48ef-b6cf-c6028f338561
String name = system. configuration. configurationsettings. deleettings ["DownLoadURL"]. tostring ();
Filestream FS = new filestream (name, filemode. Create, fileaccess. Write );
Streamwriter Sw = new streamwriter (FS, system. Text. encoding. getencoding ("gb2312 "));
Sw. writeline ("automatic number, name, age ");
Foreach (datarow DR in DT. Rows)
{
Sw. writeline (Dr ["ID"] + "," + Dr ["vname"] + "," + Dr ["iage"]);
}
Sw. Close ();
Response. addheader ("content-disposition", "attachment; filename =" + server. urlencode (name ));
Response. contenttype = "application/MS-excel"; // specify that the returned stream cannot be read by the client and must be downloaded.
Response. writefile (name); // send the file stream to the client
Response. End ();
Highlighted in red!
------
String strfile = "FILENAME" + datetime. Now. tostring ("yyyymmddhhmmss") + ". CSV ";
// Replace this part with the content obtained from the datatable/gridview.
Stringbuilder sb = new stringbuilder ();
SB. appendline ("ID, name ");
SB. appendline ("1, Jun juncai ");
SB. appendline ("2, ");
SB. appendline ("3, Jon ");
Stringwriter Sw = new stringwriter (SB );
Sw. Close ();
Response. addheader ("content-disposition", String. Format ("attachment; filename = {0}", strfile ));
Response. charset = "gb2312 ";
Response. contenttype = "application/MS-excel ";
Response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
Response. Write (SW );
Response. Flush ();
Response. End ();