在线咨询: 80015105 点击进行对话
联系我们
代理商登录 代理合作
客户服务管理登录
企业/个人应用
教育行业应用
网站运营/推广
软件外包    
鸿诺软件温馨提示,请先登录或者注册: 登录 | 注册 | 搜索
C#导出文件函数 - 产品论坛 [北京鸿诺软件技术有限公司]
产品论坛 [北京鸿诺软件技术有限公司]软件外包、研究与开发技术研究C#导出文件函数
    
 

鸿诺软件产品均提供免费下载。有任何建议,请登录发表建议或评论。鸿诺软件电话:010-51650063 QQ:80015105  帖子排序:
2008-3-29 9:31:41
it_it





发 帖 数:9
注册时间:2008-3-29
C#导出文件函数


/// <summary>
        /// 导出文件类型
        /// </summary>
        public enum FileType
        {
            /// <summary>
            /// Excel文档
            /// </summary>
            Excel,
            /// <summary>
            /// Html文件
            /// </summary>
            Html,
            /// <summary>
            /// Word文档
            /// </summary>
            Word
        }
        /// <summary>
        /// 文件到处函数
        /// </summary>
        /// <param name="controlName">需要导出成为文件服务器控件</param>
        /// <param name="fileType">文件类型</param>
        /// <param name="page">页面</param>
        public void ExportFile(System.Web.UI.Control controlName, FileType fileType, Page page)
        {
            page.Response.Clear();
            page.Response.Buffer = true;

            #region 处理输出类型
            switch (fileType)
            {
                case FileType.Excel:
                    page.Response.Charset = "GB2312";
                    page.Response.AppendHeader("Content-Disposition", "attachment;filename=ExportFile.xls");
                    page.Response.ContentType = "application/ms-excel";
                    break;
                case FileType.Html:
                    page.Response.Charset = "GB2312";
                    page.Response.AppendHeader("Content-Disposition", "attachment;filename=ExportFile.html");
                    page.Response.ContentType = "application/ms-html";
                    break;
                case FileType.Word:
                    page.Response.Charset = "GB2312";
                    page.Response.AppendHeader("Content-Disposition", "attachment;filename=ExportFile.doc");
                   
                    page.Response.ContentType = "application/ms-word";
                    break;
                default:
                    page.Response.Charset = "GB2312";
                    page.Response.AppendHeader("Content-Disposition", "attachment;filename=ExportFile.xls");
                    page.Response.ContentType = "application/ms-excel";
                    break;
            }
            #endregion
            page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");    //设置输出流为简体中文
            //this.EnableViewState = false;
            //System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            controlName.RenderControl(oHtmlTextWriter);          
            page.Response.Write(oStringWriter.ToString());
            page.Response.End();
        }

返回页首↑
北京鸿诺软件技术有限公司
中国 北京 海淀区罗庄西里13号东达商务写字楼2层
邮编: 100088   总机:010- 51650063/82357500
产品论坛 [北京鸿诺软件技术有限公司]