水晶报表文件导出方法小结
近期在做水晶报表时,作者用到了水晶报表文件导出功能。在网上一搜,资料还真多,这些资料不仅多,而且非常的杂乱,让人看得非常不舒服。于是在此将其作了一点汇总和整理,以供大家参考。写的不好,请多包涵。
命名空间:
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
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();
}
}
{
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);
}
}
{
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);
}
}
希望此篇梳理性随笔能帮助大家更方便地使用水晶报表文件导出功能。