gridview 导出excel 变成了aspx页面是怎么回事   在本地导出就是.xls 的文件, 但是放在服务器下载时就变成了.aspx 页面, 求救了

解决方案 »

  1.   

        #region
        /// <summary>
        /// 根据数据控件,文件类型,文件名称导出文件
        /// </summary>
        /// <param name="source">数据控件</param>
        /// <param name="type">文件类型</param>
        /// <param name="fileName">文件名称</param>
        public static void Export(Control source, string type, string fileName, Page page)
        {
            HttpResponse response = HttpContext.Current.Response;
            response.Clear();
            response.ClearContent();
            response.Buffer = true;
            string fType = type == "excel" ? ".xls" : ".doc";
            response.HeaderEncoding = Encoding.UTF8;
            response.AppendHeader("Content-Disposition", "attachment;fileName=" + HttpUtility.UrlEncode(fileName + fType, Encoding.UTF8));
            response.ContentType = "application/ms-excel";
            response.Charset = "GB2312";
            response.ContentEncoding = Encoding.UTF8;
            StringWriter sWriter = new StringWriter();
            HtmlTextWriter hWriter = new HtmlTextWriter(sWriter);
            source.EnableViewState = false;
            source.RenderControl(hWriter);
            response.Write(sWriter);
            response.ContentType = "text/html";
            page.RegisterStartupScript("onload", "<script language=javascript>window.location=window.location;window.alert();</script>");
            response.End();
        }
        //重写VerifyRenderingInServerForm方法,什么事也不干(也就是阻止系统调用默认的VerifyRenderingInServerForm方法)
        public override void VerifyRenderingInServerForm(Control control)
        {
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //文本:vnd.ms-excel.numberformat:@
            //日期:vnd.ms-excel.numberformat:yyyy/mm/dd
            //数字:vnd.ms-excel.numberformat::#,##0.00
            //货币:vnd.ms-excel.numberformat¥#,##0.00
            //百分比:vnd.ms-excel.numberformat:#0.00%
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //表示对每一行以字符串的形式输出
                    e.Row.Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                }
            }
        }
        #endregion
      

  2.   

    打开EXCEL模板,遍历赋值到单元格
      

  3.   

    我的更怪,同样的Buton事件调用方法,TOEXCEL(Gridview1),有的页面可以直接下载的就是EXCEL,有的就是.ASPX,但是如果保存时在迅雷中选使用IE下载后就能下到EXCEL,我都想不通了。
      

  4.   

    http://blog.csdn.net/hongchawendu/article/details/7387535