asp.net gridview 生成excel及word

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();
    }

 

调用:

   protected void Button1_Click(object sender, EventArgs e)
    {
        Export("application/ms-excel", "Excel1.xls");
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    }

 

 

 

Word:

 private void Export(string FileType, string FileName)
    {
        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();
    }

 

调用:

 protected void Button1_Click(object sender, EventArgs e)
    {
        Export("application/ms-word", "Word1.doc");
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    }

 

你可能感兴趣的:(String,object,Excel,asp.net,button)