A good solution for generating garbled data from Excel export from the DataGrid

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.