When the guide data to the original code is sometimes garbled private static void WriteToExcelFromDataGrid (DataGrid dg, string fileName) {// StringWriter tw = new StringWriter (new CultureInfo ( "zh-CHS", false)) in Excel; StringWriter tw = new StringWriter (); HtmlTextWriter hw = new HtmlTextWriter (tw); dg.RenderControl (hw); System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; response.Clear (); response.ContentEncoding = System.Text .Encoding.Getencoding ("UTF-8"); respternse.contenttype = "Application / vnd.ms-excel"; response.addheader ("Content-Disposition", "Attachment; FileName =" filename); response.charset = "GB2312"; Response.write (Tw.toString ()); response.end ();
Correct response.contentencoding = system.text.Encoding.Getencoding ("UTF-7"); reason UTF-8 does not support Chinese