日期:2014-05-20  浏览次数:20862 次

数据导出到excel 2007
导出到Excel 2007,导出到2003的我知道。但是2007就不能用了,请教各位高手!

------解决方案--------------------
导出Excel2007
C# code

        this.GridView1.AllowPaging = false;
     //   Bindcontrols.BindGridView(GridView1, sql);//绑定GridView
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.Write("<meta http-equiv=Content-Type content=text/html;charset=GB2312>");
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("导出Excel名称"+".xls", System.Text.Encoding.UTF8) + "");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "application/vnd.xls";//设置输出文件类型为excel文件。
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        GridView1.RenderControl(oHtmlTextWriter);
        Response.Output.Write(oStringWriter.ToString());
        Response.Flush();
        Response.End();
        this.GridView1.AllowPaging = true;

//重写控件
    private void DisableControls(Control gv)
    {
        LinkButton lb = new LinkButton();
        Literal l = new Literal();
        string name = String.Empty;
        for (int i = 0; i < gv.Controls.Count; i++)
        {
            if (gv.Controls[i].GetType() == typeof(Button))
            {
                l.Text = (gv.Controls[i] as Button).Text;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            if (gv.Controls[i].GetType() == typeof(LinkButton))
            {
                l.Text = (gv.Controls[i] as LinkButton).Text;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            if (gv.Controls[i].GetType() == typeof(System.Web.UI.HtmlControls.HtmlInputText))
            {
                l.Text = (gv.Controls[i] as System.Web.UI.HtmlControls.HtmlInputText).Value;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            if (gv.Controls[i].HasControls())
            {
                DisableControls(gv.Controls[i]);
            }
        }
    }

------解决方案--------------------
C# code
 /// <summary>
        /// 根据EXCEL文件名,返回连接字符串
        /// </summary>
        /// <param name="FileName">Excel文件名</param>
        public string GetConnectionString(string fileName)
        {
            ExcelVersionEnum ev = this.GetExcelVersion(fileName);

            string hdr = "HDR=Yes;";
            if (this.IsHDR == false) hdr = "HDR=No;";
            //HDR  = Yes  把第1行作为字段看待
            //IMEX = 1    通知驱动程序始终将“互混”数据列作为文本读取
            string connStr = string.Empty;
            if (ev == ExcelVersionEnum.Excel2007)
                connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                               "Data Source = " + @fileName + ";" +
                               "Extended Properties=\"Excel 12.0 Xml;" + hdr + "Imex=1;\"";
            else
                connStr = "Provider = Microsoft.Jet.OLEDB.4.0;" +
                    "Data Source = " + @fileName + ";" +
                    "Extended Properties=\"Excel 8.0;" + hdr + "Imex=1;\"";
           
            return connStr;
        }

------解决方案--------------------
5楼的办法其实是导出HTML。
其他的没仔细看,楼主可以下载我上传的aspose.cells
http://download.csdn.net/source/1862524
使用参考:http://bokee.shinylife.net/blog/article.asp?id=961