There are many ways to export data to excel. For some complicated formats, I like to splice a <Table> In the background before using response output.
If the data contains Chinese characters or some special characters, many nonstandard writing methods may cause page garbled characters. Here we will bring you the most standard format (I think:
Page p = HttpContext.Current.Handler as Page;p.Response.Clear();p.Response.Buffer = true;p.Response.Charset = "UTF-8";p.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8) + ".xls");p.Response.ContentEncoding = Encoding.UTF8;p.Response.ContentType = "application/vnd.ms-excel";p.EnableViewState = false;p.Response.Write("
Note the following two points for the above Code:
1) charset/appendheader/contentencoding must all use utf8
2) if there is only <Table> in text, <HTML>
Now, no garbled characters are involved in how you export an Excel file.