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

命名空间:

 

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(
thisthis.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();
        }
    }

相关文章:

  • 2022-12-23
  • 2022-01-23
  • 2021-12-02
  • 2022-12-23
  • 2021-09-28
  • 2021-12-16
  • 2022-12-23
  • 2021-11-22
猜你喜欢
  • 2021-11-25
  • 2022-12-23
  • 2022-01-03
  • 2021-06-18
  • 2021-09-27
  • 2022-02-26
相关资源
相似解决方案