导入excel乱码

private static void WriteToExcelFromDataGrid(DataGrid dg, string fileName)
{

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");//正确utf-7,utf-8不支持中文
response.ContentType ="application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
response.Charset = "gb2312";
response.Write(tw.ToString());
response.End();
}

你可能感兴趣的:(Excel)