导入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();
}

posted on 2008-12-03 15:54  shengel  阅读(733)  评论(1编辑  收藏  举报