ASP.NET MVC RDLC-导出

        #region 导出
        /// <summary>
        /// 导出Excel或Pdf文件,通过文件扩展名自动识别
        /// </summary>
        /// <param name="name">文件路径及名称(包括扩展名)</param>
        /// <returns>导出成功返回true,否则返回false</returns>
        private bool ReportExport(string name)
        {
            bool result = false;

            string Name = name;
            string strFileName;//文件名
            string strExtend = ".xls";//扩展名
            string strFormat = "Excel";//文件格式
            bool validate = false;

            if (name != null || name.Trim() != String.Empty)
            {
                Name = name.Trim();
                strFileName = Path.GetFileName(Name);
                if (strFileName.Trim() != String.Empty)
                {
                    strExtend = Path.GetExtension(Name).Trim().ToLower();

                    switch (strExtend)
                    {
                        case ".xls":
                            strFormat = "EXcel";
                            break;
                        case ".pdf":
                            strFormat = "PDF";
                            break;
                        default:
                            strFormat = "Excel";
                            break;
                    }
                    validate = true;
                }
            }

            if (validate)
            {
                Warning[] Warnings;
                string[] strStreamIds;
                string strMimeType;
                string strEncoding;
                string strFileNameExtension;

                byte[] bytes = this.rptView.LocalReport.Render(strFormat, null, out strMimeType,
   out strEncoding, out strFileNameExtension, out strStreamIds, out Warnings);

                using (System.IO.FileStream fs = new FileStream(Name, FileMode.Create))
                {
                    fs.Write(bytes, 0, bytes.Length);
                }

                result = true;
            }

            return result;
        }
        #endregion 导出

posted on 2014-04-23 13:51  麦克斯韦科思  阅读(380)  评论(0编辑  收藏  举报