水晶报表的公有类

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;

 

   public class ExportCrystalL
    {

       /// <summary>
       /// 导出数据
       /// </summary>
       /// <param name="crReportDocument">加载的报表</param>
       /// <param name="FilePath">导出的文件所在路径</param>
       /// <param name="FileName">导出的文件名</param>
       /// <param name="Ext">导出的文件格式</param>
       public void Export(ReportDocument crReportDocument, string FilePath, string FileName, string Ext)
       {
           ExportOptions exportOptions = new ExportOptions();
           DiskFileDestinationOptions diskOptions = new DiskFileDestinationOptions();

           exportOptions = crReportDocument.ExportOptions;
           //设置导出格式。
           exportOptions.ExportFormatType = GetExportFormatType(Ext);
           exportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
           //   设置磁盘文件选项。
           diskOptions.DiskFileName = FilePath + FileName + "." + Ext;
           exportOptions.ExportDestinationOptions = diskOptions;
           try
           {
               //导出报表到服务器端
               crReportDocument.Export();
               //导出到客户端
               //HttpContext.Current.Response.Redirect("Temp\\" + FileName + "." + Ext);
           }
               //HttpContext.Current.Response.Redirect("Export\\" + FileName + "." + Ext);
              
          
           catch (Exception ex)
           {
               HttpContext.Current.Response.Write(ex.Message);
           }
  
       }
      
       /// <summary>
       /// 返回导出的文件类型
       /// </summary>
       /// <param name="ext"></param>
       /// <returns></returns>
       private ExportFormatType GetExportFormatType(string ext)
       {
           switch (ext)
           {
               case "pdf":
                   return ExportFormatType.PortableDocFormat;
               case "rtf":
                   return ExportFormatType.RichText;
               case "doc":
                   return ExportFormatType.WordForWindows;
               case "xls":
                   return ExportFormatType.Excel;
               case "html":
                   return ExportFormatType.HTML32;
               default:
                   return ExportFormatType.NoFormat;
           }
       }

        public void ExportReport(ReportDocument crReportDocument, string FilePath, string FileName, string Ext)
        {
             Export(crReportDocument, FilePath, FileName, Ext);
             string fileName = FilePath + FileName + "." + Ext;

             //FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read);
             //BinaryReader br = new BinaryReader(file);
             //br.BaseStream.Seek(0, SeekOrigin.Begin);
             //HttpContext.Current.Response.Clear();
             //HttpContext.Current.Response.ClearContent();
             //HttpContext.Current.Response.ClearHeaders();
             //HttpContext.Current.Response.ContentType = "application/octet-stream";
             //HttpContext.Current.Response.AddHeader("Context-Disposition", "attachment;filename=" + file.Name);
             //HttpContext.Current.Response.BinaryWrite(br.ReadBytes((int)br.BaseStream.Length));
             //HttpContext.Current.Response.Flush();
             //HttpContext.Current.Response.End();
             //br.Close();
             //file.Close();

             //File.Copy(fileName, @"c:\temp\" + FileName + "." + Ext, true);
             //File.Delete(fileName);

             string path = FilePath + FileName + "." + Ext;
             System.IO.FileInfo file = new System.IO.FileInfo(path);
             HttpContext.Current.Response.Clear();
             HttpContext.Current.Response.ClearContent();
             HttpContext.Current.Response.ClearHeaders();
             HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name));
             HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
             HttpContext.Current.Response.ContentType = "application/octet-stream";
             HttpContext.Current.Response.WriteFile(file.FullName);
             HttpContext.Current.Response.End();
             File.Delete(path);
     }
    }

 

posted @ 2010-06-23 17:20  JasonNET  阅读(189)  评论(0编辑  收藏  举报