gridview 保存到 excel c# .net

u010703853 2014-06-04 09:54:24
用 c# 开发的 webform 如何将 gridview 的值保存到 excel 中
我之前写的 在xp 系统中 可以打开 , 可是换了win7 后,打开就有乱码。 求鉴定:


protected void Button1_Click(object sender, EventArgs e)
{
string XX = Label1.Text + Label3.Text;
Export("application/ms-excel", XX + "月份采购计划.xls");
}


public override void VerifyRenderingInServerForm(Control control)
{

}

private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}


我发过2次类似的帖子,均未解决, 不死心,再次挑战..... 谢谢
...全文
216 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
搬砖de蚂蚁 2014-06-04
  • 打赏
  • 举报
回复
引用 13 楼 u010703853 的回复:
[quote=引用 12 楼 nblover 的回复:] 加个BOM试试,也许有用!
BOM 物料清单 ? 这个有啥用呀? 不懂耶....[/quote] BOM = ByteOrderMarking
  • 打赏
  • 举报
回复
你导出的是2003格式的 win7上默认的是office2007,要打开2003的文档,需要安装office2007兼容性组件
  • 打赏
  • 举报
回复
引用 12 楼 nblover 的回复:
加个BOM试试,也许有用!
BOM 物料清单 ? 这个有啥用呀? 不懂耶....
搬砖de蚂蚁 2014-06-04
  • 打赏
  • 举报
回复
加个BOM试试,也许有用!
gz14055459 2014-06-04
  • 打赏
  • 举报
回复
让你测试是不是office版本引起的问题。。。无语了
EdsionWang 2014-06-04
  • 打赏
  • 举报
回复

   public static void DGToExcel(Control ctl, string fileName)
        {
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename="+fileName);
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            ctl.Page.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }
修改一下编码方式解决乱码问题
  • 打赏
  • 举报
回复
引用 6 楼 duanzi_peng 的回复:
[quote=引用 4 楼 u010703853 的回复:]
[quote=引用 3 楼 duanzi_peng 的回复:]
试试 下边的代码

GridView gv = new GridView();
gv.DataSource = dtError;
gv.DataBind();
gv.Attributes.Add("style", "vnd.ms-excel.numberformat:@");

HttpResponse hResponse = this.Response;
string fileName1 = "新员工格式验证错误统计" + DateTime.Now.ToString("yyyyMMdd");

hResponse.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName1, System.Text.Encoding.UTF8) + ".xls");
hResponse.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
hResponse.ContentType = "application/ms-excel";
this.EnableViewState = false;

StringWriter tw = new StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
gv.RenderControl(hw);

hResponse.Write(tw);
hResponse.End();

你的方法我试过的呀。
http://bbs.csdn.net/topics/390796988 见 3 楼

是不是不兼容的缘故呢?[/quote]
弹出下载框是什么样子的?、要用IE下载[/quote]
弹出框如下


保存后 打开的样子 如下:
http://bbs.csdn.net/topics/390780207
  • 打赏
  • 举报
回复
引用 5 楼 gz14055459 的回复:
你安装的win7是ghost 版的吧,如果是,里面集成是的office2007版本的,你原来是office2003版本,肯定打不开的。建议你把office2007删除,重新安装office2003.试试看。
是 win7 Ghost 版的 公司现在都是 office 2007 的 (总不能全部都要安装2003吧) 之前是 office 2003 就没有问题 , 有啥好的办法可以解决吗?
exception92 2014-06-04
  • 打赏
  • 举报
回复
引用 4 楼 u010703853 的回复:
[quote=引用 3 楼 duanzi_peng 的回复:] 试试 下边的代码

GridView gv = new GridView();  
                        gv.DataSource = dtError;  
                        gv.DataBind();  
                        gv.Attributes.Add("style", "vnd.ms-excel.numberformat:@");  
  
                        HttpResponse hResponse = this.Response;  
                        string fileName1 = "新员工格式验证错误统计" + DateTime.Now.ToString("yyyyMMdd");  
  
                        hResponse.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName1, System.Text.Encoding.UTF8) + ".xls");  
                        hResponse.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");  
                        hResponse.ContentType = "application/ms-excel";  
                        this.EnableViewState = false;  
  
                        StringWriter tw = new StringWriter();  
                        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);  
                        gv.RenderControl(hw);  
  
                        hResponse.Write(tw);  
                        hResponse.End();
你的方法我试过的呀。 http://bbs.csdn.net/topics/390796988 见 3 楼 是不是不兼容的缘故呢?[/quote] 弹出下载框是什么样子的?、要用IE下载
gz14055459 2014-06-04
  • 打赏
  • 举报
回复
你安装的win7是ghost 版的吧,如果是,里面集成是的office2007版本的,你原来是office2003版本,肯定打不开的。建议你把office2007删除,重新安装office2003.试试看。
  • 打赏
  • 举报
回复
引用 3 楼 duanzi_peng 的回复:
试试 下边的代码

GridView gv = new GridView();  
                        gv.DataSource = dtError;  
                        gv.DataBind();  
                        gv.Attributes.Add("style", "vnd.ms-excel.numberformat:@");  
  
                        HttpResponse hResponse = this.Response;  
                        string fileName1 = "新员工格式验证错误统计" + DateTime.Now.ToString("yyyyMMdd");  
  
                        hResponse.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName1, System.Text.Encoding.UTF8) + ".xls");  
                        hResponse.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");  
                        hResponse.ContentType = "application/ms-excel";  
                        this.EnableViewState = false;  
  
                        StringWriter tw = new StringWriter();  
                        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);  
                        gv.RenderControl(hw);  
  
                        hResponse.Write(tw);  
                        hResponse.End();
你的方法我试过的呀。 http://bbs.csdn.net/topics/390796988 见 3 楼 是不是不兼容的缘故呢?
exception92 2014-06-04
  • 打赏
  • 举报
回复
试试 下边的代码

GridView gv = new GridView();  
                        gv.DataSource = dtError;  
                        gv.DataBind();  
                        gv.Attributes.Add("style", "vnd.ms-excel.numberformat:@");  
  
                        HttpResponse hResponse = this.Response;  
                        string fileName1 = "新员工格式验证错误统计" + DateTime.Now.ToString("yyyyMMdd");  
  
                        hResponse.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName1, System.Text.Encoding.UTF8) + ".xls");  
                        hResponse.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");  
                        hResponse.ContentType = "application/ms-excel";  
                        this.EnableViewState = false;  
  
                        StringWriter tw = new StringWriter();  
                        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);  
                        gv.RenderControl(hw);  
  
                        hResponse.Write(tw);  
                        hResponse.End();
欢乐的小猪 2014-06-04
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/350049052
  • 打赏
  • 举报
回复

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧