I did not say anything. I got it from the Internet,
Reference from: http://aliketen.cnblogs.com/articles/363650.html
The Code is as follows:
Private void Export (system. Web. UI. webcontrols. DataGrid DG, string filename, string typename)
...{
System. Web. httpresponse = page. response;
Httpresponse. appendheader
("Content-disposition", "attachment; filename ="
+ Httputility. urlencode (filename, system. Text. encoding. utf8 ));
Httpresponse. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
Httpresponse. contenttype = typename;
System. Io. stringwriter Tw = new system. Io. stringwriter ();
System. Web. UI. htmltextwriter hW = new system. Web. UI. htmltextwriter (TW );
DG. rendercontrol (HW );
String filepath = server. mappath ("..") + filename;
System. Io. streamwriter Sw = system. Io. file. createtext (filepath );
Sw. Write (TW. tostring ());
Sw. Close ();
Downfile (httpresponse, filename, filepath );
Httpresponse. End ();
}
Private bool downfile (system. Web. httpresponse response, string filename, string fullpath)
...{
Try
...{
Response. contenttype = "application/octet-stream ";
Response. appendheader ("content-disposition", "attachment; filename =" +
Httputility. urlencode (filename, system. Text. encoding. utf8) + "; charset = gb2312 ");
System. Io. filestream FS = system. Io. file. openread (fullpath );
Long FLEN = FS. length;
Int size = 102400; // download data at the same time every k
Byte [] readdata = new byte [size]; // specify the buffer size
If (size> FLEN) size = convert. toint32 (FLEN );
Long FPOs = 0;
Bool isend = false;
While (! Isend)
...{
If (FPOs + size)> FLEN)
...{
Size = convert. toint32 (FLEN-FPOs );
Readdata = new byte [size];
Isend = true;
}
FS. Read (readdata, 0, size); // read a compressed Block
Response. binarywrite (readdata );
FPOs + = size;
}
FS. Close ();
System. Io. file. Delete (fullpath );
Return true;
}
Catch
...{
Return false;
}
}
}
To make it easier for everyone to copy
Check the following code to ensure the same
Private void Export (system. Web. UI. webcontrols. DataGrid DG, string filename, string typename)
{
System. Web. httpresponse = page. response;
Httpresponse. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename, system. Text. encoding. utf8 ));
Httpresponse. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
Httpresponse. contenttype = typename;
System. Io. stringwriter Tw = new system. Io. stringwriter ();
System. Web. UI. htmltextwriter hW = new system. Web. UI. htmltextwriter (TW );
DG. rendercontrol (HW );
String filepath = server. mappath ("..") + filename;
System. Io. streamwriter Sw = system. Io. file. createtext (filepath );
Sw. Write (TW. tostring ());
Sw. Close ();
Downfile (httpresponse, filename, filepath );
Httpresponse. End ();
}
Private bool downfile (system. Web. httpresponse response, string filename, string fullpath)
{
Try
{
Response. contenttype = "application/octet-stream ";
Response. appendheader ("content-disposition", "attachment; filename =" +
Httputility. urlencode (filename, system. Text. encoding. utf8) + "; charset = gb2312 ");
System. Io. filestream FS = system. Io. file. openread (fullpath );
Long FLEN = FS. length;
Int size = 102400; // download data at the same time every k
Byte [] readdata = new byte [size]; // specify the buffer size
If (size> FLEN) size = convert. toint32 (FLEN );
Long FPOs = 0;
Bool isend = false;
While (! Isend)
{
If (FPOs + size)> FLEN)
{
Size = convert. toint32 (FLEN-FPOs );
Readdata = new byte [size];
Isend = true;
}
FS. Read (readdata, 0, size); // read a compressed Block
Response. binarywrite (readdata );
FPOs + = size;
}
FS. Close ();
System. Io. file. Delete (fullpath );
Return true;
}
Catch
{
Return false;
}
}
How to call it ?? Actually, how can I call the export function.
The above method can effectively solve the problem of exported garbled characters.