SoFunction
Updated on 2025-04-08

Reasons and solutions for exporting Excel garbled code

Copy the codeThe code is as follows:

protected void Excel_Click(object sender, EventArgs e)
{
= "UTF-8";
();
();
= .UTF8;
= .UTF8;
("content-disposition", "attachment; filename=");
= "application/excel";
sw = new ();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// turn off paging
= false;
dataBind();
(htw);
(());
();
// turn the paging on again
= true;
dataBind();
}

key:
Copy the codeThe code is as follows:

= "UTF-8";//Add encoding format
();
();
= .UTF8;//Add encoding format for table content
= .UTF8;// Add encoding format to the table header

If the above cannot be solved, it can still be used
Copy the codeThe code is as follows:

();
();
("content-disposition", "attachment; filename=");
= "GB2312";
= ("GB2312");
= "application/excel";
sw = new ();
HtmlTextWriter htw = new HtmlTextWriter(sw);
if ( > 0)
{
(htw);
}
else
{
(htw);
}
(());
();

key:
Copy the codeThe code is as follows:

= "GB2312";
= ("GB2312");

Pay attention to observation, the main reason is actually the encoding format problem.

Now we can prevent garbled problems during export