导出Excel或word文档

来源:互联网 发布:acl应用到端口 编辑:程序博客网 时间:2024/04/29 20:22

#region 导出文件方法
    /// <summary>
    /// 导出文件方法
    /// </summary>
    /// <param name="exportFileName">导出文件的名字(包括后缀名,word为: .doc,excel为: .xls)</param>
    /// <param name="mime">MIME码(Word为:"application/ms-word",Excel为:"application/ms-excel")</param>
    /// <param name="control">导出的控件</param>

    private void ExportFile(stringexportFileName, stringmime, Controlcontrol)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + exportFileName);
        //设置输出流为简体中文 
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");   
        //设置输出文件类型。
        Response.ContentType = mime;
        this.EnableViewState = false;
        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        control.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();
    }

    // 使用导出方法,必须要重写的方法,否则会报错 :
    //类型“xxx”的控件“xxx”必须放在具有 runat=server 的窗体标记内
    public override void VerifyRenderingInServerForm(Control control)
    {
        // 空
    }
    #endregion


原创粉丝点击