其实如果是在windows 的窗体中,水晶报表的Crystalreportviewer本身提供了导出功能,所以可以不需要编写任何代码。
但是在asp.net中要实现水晶报表的导出功能则稍微有点不同了,需要实现少量的代码,下面是一个代码模版:
public void Export(string FileName, string Ext)
{
ReportDocument reportDocument = GetReportDocument();
ExportOptions exportOptions = new ExportOptions();
DiskFileDestinationOptions diskOptions = ExportOptions.CreateDiskFileDestinationOptions();
exportOptions.ExportFormatType = GetExportFormatType(Ext);
exportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
diskOptions.DiskFileName = FileName+"."+Ext;
exportOptions.ExportDestinationOptions = diskOptions;
reportDocument.Export(exportOptions);
}
{
ReportDocument reportDocument = GetReportDocument();
ExportOptions exportOptions = new ExportOptions();
DiskFileDestinationOptions diskOptions = ExportOptions.CreateDiskFileDestinationOptions();
exportOptions.ExportFormatType = GetExportFormatType(Ext);
exportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
diskOptions.DiskFileName = FileName+"."+Ext;
exportOptions.ExportDestinationOptions = diskOptions;
reportDocument.Export(exportOptions);
}
其中GetExportFormatType()的实现如下:
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;
}
}
{
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;
}
}
而GetReportDocument()则是一个返回用户要导出的报表的ReportDocument 对象的函数,用户自己随意去实现。
另外还要注意,FileName的格式必须是:"c:\\crystalreport" 这样的形式。
套用这个代码模版,就可以实现5种格式的报表导出功能;