Refer to some articles on the Internet:
TableLogOnInfo logOnInfo = new TableLogOnInfo (); ReportDocument oRpt = new ReportDocument (); string RptDir = "F: //aspnet//crystaltopdf//crystalreport1.rpt"; absolute path oRpt.Load //crystalreport1.rpt file is located (RptDir ); logOnInfo.ConnectionInfo.ServerName = "localhost"; logOnInfo.ConnectionInfo.DatabaseName = "northwind"; logOnInfo.ConnectionInfo.UserID = "sa"; logOnInfo.ConnectionInfo.Password = "xxxxxx"; oRpt.Database.Tables [0] .Applylogonfo (logonfo);
// Establish a connection between .RPT files and CrystalReportViewer files CrystalReportViewer1.Reportsource = ORPT;
ExportOptions crExportOptions = new ExportOptions (); DiskFileDestinationOptions crDiskFileDestinationOptions = new DiskFileDestinationOptions (); crDiskFileDestinationOptions.DiskFileName = "f: //aspnet//crystaltopdf//crystalreport1.pdf"; crExportOptions = oRpt.ExportOptions; crExportOptions.DestinationOptions = crDiskFileDestinationOptions; crExportOptions. ExportDestinationType = ExportDestinationType.DiskFile; CrexportOptions.exportFormattype = ExportFormattype.portableDocformat;
ORPT.EXPORT (); ORPT.CLOSE ();
Response.clearContent (); response.clearheaders (); response.contentType = "Application / PDF"; response.writefile ("f: //aspnet//crystaltopdf//crystalreport1.pdf");
Response.flush (); response.close ();
Among them, I tried it, I must use the code to log in to log in. If I don't add, I don't add the wrong, I'm strange, I am adding to the report, why do you want to add a code? The method of generating PDF in the program and the method of generating Excel is similar.