解决方案 »

  1.   

    两种思路:
    1.服务器生成提供下载,写后端代码
    2.客户端允许ActiveX下载,写JS代码
      

  2.   


    try it:
    http://www.cnblogs.com/insus/archive/2009/02/28/1400266.html
      

  3.   


    public override bool EnableEventValidation {
    get { return false; }
    set { base.EnableEventValidation = false; }
    }public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
    {
    }
    public static void ExportCommon(string XlsTittle, System.Web.UI.WebControls.GridView gridView, int[] hideColumns, string fileExt)
    {
    HttpContext context = HttpContext.Current;
    System.Web.UI.WebControls.Label TextTittle = new System.Web.UI.WebControls.Label();
    System.Web.UI.WebControls.Label TextTimeFrame = new System.Web.UI.WebControls.Label();
    System.Web.UI.WebControls.Label TextNowTime = new System.Web.UI.WebControls.Label(); TextTittle.Text = XlsTittle + "<br style='mso-data-placement:same-cell;'/>";
    TextNowTime.Text = "导出时间:" + DateAndTime.Now + "<br style='mso-data-placement:same-cell;'/>";
    TextTittle.ControlStyle.Font.Size = 15;
    TextTittle.ControlStyle.Font.Name = "微软雅黑";
    TextTittle.ControlStyle.ForeColor = Color.Black;
    context.Response.ClearContent();
    context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
    context.Response.AddHeader("content-disposition", string.Format("attachment; filename=Page{0}.{1}", gridView.PageIndex + 1, fileExt));
    switch (fileExt.ToLower()) {
    case "xls":
    context.Response.ContentType = "application/excel";
    break; // TODO: might not be correct. Was : Exit Select break;
    case "doc":
    context.Response.ContentType = "application/word";
    break; // TODO: might not be correct. Was : Exit Select break;
    } System.IO.StringWriter sw = new System.IO.StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw); Page page = new Page();
    HtmlForm form = new HtmlForm();
    gridView.EnableViewState = false; page.EnableEventValidation = false; page.DesignerInitialize(); // 隐藏指定的列
    if (hideColumns != null) {
    foreach (int columnIndex in hideColumns) {
    if (columnIndex < gridView.Columns.Count) {
    gridView.Columns[columnIndex].Visible = false;
    }
    }
    } // 如果HeaderRow里的控件是button的话,则把它替换成文本
    foreach (TableCell tc in gridView.HeaderRow.Cells) {
    // TableCell里的每个Control
    foreach (Control c in tc.Controls) {
    // 如果控件继承自接口IButtonControl
    if (c.GetType().GetInterface("IButtonControl") != null && c.GetType().GetInterface("IButtonControl").Equals(typeof(IButtonControl))) {
    // 如果该控件不是“导出Excel”按钮则把button转换成文本
    tc.Controls.Clear();
    tc.Text = ((IButtonControl)c).Text;
    }
    }
    }
    // 将服务器控件的内容输出到所提供的 System.Web.UI.HtmlTextWriter 对象中
    //  gridView.RenderControl(htw)
    page.Controls.Add(TextTittle);
    page.Controls.Add(TextNowTime);
    page.Controls.Add(form);
    //把form放到page内 form.Controls.Add(gridView);
    //再把gridview放到form中 page.RenderControl(htw);
    //输出page
    HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
    context.Response.Write(sw.ToString());
    context.Response.Flush();
    context.Response.End();
    }
      

  4.   

    copy过去改下表之类的就可以了,成功的截图都出来了,如果这样还不会
      

  5.   

    把GridView中的内容导出到Excel文档示例
    private void Export(string FileType, string FileName)
        {
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            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();
        }
      

  6.   

    参考我的博文:http://blog.csdn.net/zouyujie1127/article/details/12002177