In ASP. NET, the crystal report does not provide the report export and printing functions as in Windows Form. We need to add code to control it by ourselves. Here is a DEMO:
Export:
MyReport ReportDoc = new myReport (); // table name
Private void btnExport_Click (object sender, System. EventArgs e)
{
CrystalDecisions. Shared. DiskFileDestinationOptions DiskOpts = new CrystalDecisions. Shared. DiskFileDestinationOptions ();
ReportDoc. ExportOptions. ExportDestinationType = CrystalDecisions. Shared. ExportDestinationType. DiskFile;
Switch (DropDownList1.SelectedItem. Text)
{
Case "Rich Text (RTF )":
ReportDoc. ExportOptions. ExportFormatType = CrystalDecisions. Shared. ExportFormatType. RichText;
DiskOpts. DiskFileName = "d :\\ demo. rtf ";
Break;
Case "Portable Document (PDF )":
ReportDoc. ExportOptions. ExportFormatType = CrystalDecisions. Shared. ExportFormatType. PortableDocFormat;
DiskOpts. DiskFileName = "d :\\ demo.pdf ";
Break;
Case "MS Word (DOC )":
ReportDoc. ExportOptions. ExportFormatType = CrystalDecisions. Shared. ExportFormatType. WordForWindows;
DiskOpts. DiskFileName = "d :\\ demo.doc ";
Break;
Case "MS Excel (XLS )":
ReportDoc. ExportOptions. ExportFormatType = CrystalDecisions. Shared. ExportFormatType. Excel;
DiskOpts. DiskFileName = "d :\\ demo.xls ";
Break;
Default:
Break;
}
ReportDoc. ExportOptions. DestinationOptions = DiskOpts;
ReportDoc. Export ();
}
Print:
Private void btnPrint_Click (object sender, System. EventArgs e)
{
String strPrinterName; // specify the printer name
StrPrinterName = @ "Canon Bubble-Jet BJC-210SP ";
PageMargins margins; // sets the print margin.
Margins = ReportDoc. PrintOptions. PageMargins;
Margins. bottomMargin = 250;
Margins. leftMargin = 350;
Margins. rightMargin = 350;
Margins. topMargin = 450;
ReportDoc. PrintOptions. ApplyPageMargins (margins );
ReportDoc. PrintOptions. PrinterName = strPrinterName; // The Name Of The application printer.
// Print the report. startPageN and endPageN
// If the parameter is set to 0, all pages are printed.
ReportDoc. PrintToPrinter (1, false, 0, 0 );
}