本人做的一个单位项目中,要求客户端能将数据导出到excel格式保存,
本人将按钮点击事件中调用该函数
代码如下:        public override void VerifyRenderingInServerForm(Control control)
        {
            // Confirms that an HtmlForm control is rendered for
        }        protected void ImageButtonExcel_Click(object sender, ImageClickEventArgs e)
        {
            string strFileName = "在岗职工工资统计报表(" + RadioButtonListType.SelectedValue + ")";            ToExcel(GridViewZgzgtjbb, strFileName);
        } private void ToExcel(GridView Gv, string strFileName)
        {
            Gv.AllowPaging = false;
            Gv.AllowSorting = false;
            Gv.DataBind();            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(strFileName) + ".xls");
            Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
            Gv.Page.EnableViewState = false;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            Gv.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();            Gv.AllowPaging = true;
            Gv.AllowSorting = true;
            Gv.DataBind();
        }这段代码一直用了很久都功能正常,
最近单位由于office版本升级,由excel2003改为了excel2007,于是要求下载的附件直接使用excel2007的文件格式保存(文件扩展名*.xlsx).
请高手指点如何能从gridview导出成excel2007的*.xlsx文件,并且能正常打开不报错.

解决方案 »

  1.   

    引用Microsoft Excel 12.0 Object Library
      

  2.   

    客户端要将数据导出,并保存为xlsx格式,方法有许多。
    引用Microsoft Excel 12.0 Object Library,需要安装excel 2007。
    另一种可以使用 Office OPEN XML SDK 2.0 直接存取xlsx格式。
      

  3.   


    我已经找到并引用了Microsoft Excel 12.0 Object Library,请问要如何修改上面的代码才行?
      

  4.   

      protected void btnToExcel_Click(object sender, EventArgs e)
       {
         Response.Clear();//清楚缓冲区流所有内容输出
          Response.BufferOutput = true;
            Response.Charset = "GB2312";//获取或设置输出流的HTTP字符集
            //假定导出的文件名为FileName.xls
            Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");//将http头添加到输出流
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            //设置导出的文件格式
            Response.ContentType = "application/ms-excel";
            EnableViewState = false;
            System.Globalization.CultureInfo cultureinfo = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter stringWrite = new System.IO.StringWriter(cultureinfo);
            System.Web.UI.HtmlTextWriter textWrite = new HtmlTextWriter(stringWrite);
            Gv.RenderControl(textWrite);
            //把html流写回到浏览器
            Response.Write(stringWrite.ToString());
            Response.End();
     
      

  5.   


    楼上给的这个方法只适应于 excel2003,我要求的是要成生成 excel2007的 *.xlsx文件。这段代码我试过了,不行,会报错。
      

  6.   

    不可能吧,,把数据放到datertable里面,然后girdview在绑定它,在按照6楼的方法,就OK了
      

  7.   

    这个问题最近琢磨了一下,试了很多方法,目前试出来了一种方法,就是先在服务器上安装Offiec2007的类,再在项目中引用它,先在服务器的指定目录中生成这个Excel文件,然后再提供给用户下载。但这个方法有些缺陷。例如:字段名称显示。XM,在SQL数据库是是英文显示,生成的Excel文件仍是英文显示。要手动编程把英文字段名改成中文显示(XM变成姓名),很烦琐。不知大家还有什么好方法没有?
      

  8.   

    http://www.cnblogs.com/sufei/archive/2009/05/23/1487540.html 这里是汇总你可以看看,保证你受益不浅啊,呵呵