public void ToDocement(System.Web.UI.Control ctl, ToContentType type, string FileName)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        string Type = type.ToString();
        if (type == ToContentType.word)
        {
            Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("FileName", System.Text.Encoding.UTF8) + ".doc");
        }
        else if (type == ToContentType.excel)
        {
            Response.AddHeader("Content-Disposition", "attachment;   filename=" + System.Web.HttpUtility.UrlEncode("FileName", System.Text.Encoding.UTF8) + ".xls");
        }
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.ContentType = "application/ms-" + type;//设置输出文件类型。 
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        ctl.RenderControl(oHtmlTextWriter);//控件ctl输出       
        Response.Output.Write(oStringWriter.ToString());
        Response.Flush();
        Response.End();
    }
    public enum ToContentType
    {
        word,
        excel
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        ToDocement(this.Panel1, ToContentType.word, "Word文档");
    }

 

 posted on 2015-11-12 13:58  纳米程序员  阅读(369)  评论(0编辑  收藏  举报