光明天使米迦勒
2015-12-24 09:13
采纳率: 100%
浏览 1.3k
已采纳

GridView数据导出到EXCEL

我下面的这个方法导出的EXCEL乱码,请各位帮忙看一下。

     public static void ExportExcel(string ExcelName, GridView tempFgrid)
    {
        StringBuilder sb = new StringBuilder();
        System.IO.StringWriter sw = new System.IO.StringWriter(sb);
        Page page = new Page();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        HtmlForm form = new HtmlForm();

        tempFgrid.EnableViewState = false;
        page.EnableEventValidation = false;

        page.DesignerInitialize();
        page.Controls.Add(form);
        form.Controls.Add(tempFgrid);
        page.RenderControl(htw);
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Buffer = true;

        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + ExcelName + ".xls");
        HttpContext.Current.Response.Charset = "UTF-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
        HttpContext.Current.Response.Write(sb.ToString());
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
    }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • John_ToString 2015-12-24 09:22
    已采纳

    请考虑一下文字编码格式,只要双方对应起来就可以

    点赞 评论
  • Robot-S 2015-12-24 09:23


    代码片段:
    protected void btnToExcel_Click(object sender, EventArgs e)
    {
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    HtmlTextWriter htw = new HtmlTextWriter(s......
    答案就在这里:将GridView数据导出Excel
    ----------------------Hi,地球人,我是问答机器人小S,上面的内容就是我狂拽酷炫叼炸天的答案,除了赞同,你还有别的选择吗?

    点赞 评论

相关推荐 更多相似问题