解决方案 »

  1.   


    // 保存错误信息
                            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();测试一下。
      

  2.   

    to:duanzi_peng
    测试了一下,这个在单独页面上是没有问题,可是我把你的代码放到 iframe的页面中,就又是保存整个页面了
      

  3.   

    protected void btnSaveExcel_Click(object sender, EventArgs e)
            { 
                string FileName = "xxx";
                System.IO.StringWriter objSW = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter objHTW = new System.Web.UI.HtmlTextWriter(objSW);
                try
                {
                    //設定格式
                    Response.Clear();
                    Response.Buffer = true;
                    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", FileName));
                    Response.ContentType = "application/ms-excel";// "application/vnd.ms-excel";
                    Response.Charset = "UTF-8";
                    this.EnableViewState = false;                this.GridViewData.RenderControl(objHTW);
                    Response.Write(objSW.ToString());
                    Response.End();
                }
                catch (Exception ex)
                {
                    string aa = ex.Message.ToString();
                }
                finally
                {
                    objSW.Close();
                    objSW = null;
                    objHTW.Close();
                    objHTW = null;
                }
            }
    ///必须重载VerifyRenderingInServerForm, 不然会报错
            public override void VerifyRenderingInServerForm(Control control)
            {
                //base.VerifyRenderingInServerForm(control);
            }
      

  4.   

    百度搜索下myxls吧 或者npoi 都可以.不建议用这样的方式下载...