水晶报表文件导出方法小结

近期在做水晶报表时,作者用到了水晶报表文件导出功能。在网上一搜,资料还真多,这些资料不仅多,而且非常的杂乱,让人看得非常不舒服。于是在此将其作了一点汇总和整理,以供大家参考。写的不好,请多包涵。

命名空间:

 

using  CrystalDecisions.CrystalReports.Engine;
using  CrystalDecisions.Shared;

 

 

方法一,利用ReportDocument对象的ExportToDisk方法。

具体代码为:

 

复制代码
protected   void  btnExport1_Click( object  sender, EventArgs e)
    {
        
string  FileName  =  Session.SessionID  +   " . "   +  ddlFileType.SelectedValue;
        
string  FilePath  =  Request.MapPath( " . " +   " /ReportFile/ " .Replace( @" / " @" \ " +  FileName;

        ReportDocument Document 
=   new  ReportDocument(); 
        
        Document.Load(Server.MapPath(
" CrystalReport.rpt " ));
        Document.SetDataSource(GetReportDataSource()); 
        
        
this .CrystalReportViewer1.ReportSource  =  Document;

        
// 设置导出文件格式
        ExportFormatType exprotFormatType  =  ExportFormatType.NoFormat;
        
switch  (ddlFileType.SelectedValue.ToLower())
        {
            
case   " pdf " :
                exprotFormatType 
=  CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
                
break ;
            
case   " doc " :
                exprotFormatType 
=  CrystalDecisions.Shared.ExportFormatType.WordForWindows;
                
break ;
            
case   " xls " :
                exprotFormatType 
=  CrystalDecisions.Shared.ExportFormatType.Excel;
                
break ;
            
default :
                
break ;
        }

        
try
        {
            
// 导出操作
            Document.ExportToDisk(exprotFormatType, FilePath);
            ExportFile(FileName, FilePath);
            System.IO.File.Delete(FilePath);
        }
        
catch  (Exception ex)
        {
            
string  jsError  =   " alert(' "   +  ex.Message  +   " '); " ;
            ScriptManager.RegisterStartupScript(
this this .GetType(),  " ExportReport " , jsError,  true );
        }
    }


protected   void  ExportFile( string  exportFileName,  string  filePath)
    {
        FileInfo fileInfo 
=   new  FileInfo(filePath);
        
if  (fileInfo.Exists  ==   true )
        {
            Response.Clear();
            Response.AddHeader(
" Content-Disposition " " attachment;filename= "   +  exportFileName);
            Response.AddHeader(
" Content-Length " , fileInfo.Length.ToString());
            Response.AddHeader(
" Content-Transfer-Encoding " " binary " );
            Response.ContentType 
=   " application/octet-stream " ;
            Response.ContentEncoding 
=  System.Text.Encoding.GetEncoding( " gb2312 " );
            Response.WriteFile(fileInfo.FullName);
            Response.Flush();
            Response.End();
        }
    }
复制代码

 

 

方法二,利用ReportDocument对象的Export方法。

具体代码如下:

 

复制代码
  protected   void  btnExport2_Click( object  sender, EventArgs e)
    {
        
string  FileName  =  Session.SessionID  +   " . "   +  ddlFileType.SelectedValue;
        
string  FilePath  =  Request.MapPath( " . " +   " /ReportFile/ " .Replace( @" / " @" \ " +  FileName;

        ReportDocument Document 
=   new  ReportDocument(); 
        
        Document.Load(Server.MapPath(
" CrystalReport.rpt " ));
        Document.SetDataSource(GetReportDataSource()); 
        
        
this .CrystalReportViewer1.ReportSource  =  Document;

        
// 设置导出文件格式
        ExportOptions exportOptions  =  Document.ExportOptions;
        exportOptions.ExportDestinationType 
=  ExportDestinationType.DiskFile;
        
switch  (ddlFileType.SelectedValue.ToLower())
        {
            
case   " pdf " :
                exportOptions.ExportFormatType 
=  CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
                
break ;
            
case   " doc " :
                exportOptions.ExportFormatType 
=  CrystalDecisions.Shared.ExportFormatType.WordForWindows;
                
break ;
            
case   " xls " :
                exportOptions.ExportFormatType 
=  CrystalDecisions.Shared.ExportFormatType.Excel;
                
break ;
            
default :
                
break ;
        }

            
        DiskFileDestinationOptions diskFile 
=   new  DiskFileDestinationOptions();
       
        diskFile.DiskFileName 
=  FilePath;
        Document.ExportOptions.DestinationOptions 
=  diskFile;

        
try
        {
            
// 导出操作
            Document.Export();
            ExportFile(FileName, FilePath);
            System.IO.File.Delete(FilePath);
        }
        
catch  (Exception ex)
        {
            
string  jsError  =   " alert(' "   +  ex.Message  +   " '); " ;
            ScriptManager.RegisterStartupScript(
this this .GetType(),  " ExportReport " , jsError,  true );
        }
    }
复制代码

  

希望此篇梳理性随笔能帮助大家更方便地使用水晶报表文件导出功能。

文章出处:www.cnblogs.com/jizhong

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。否则保留追究法律责任的权利。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值