Implement dynamic data sources on the web, export PDF files, DOC files

xiaoxiao2021-03-06  60

using System; using System.IO; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Data.OleDb; using System.Drawing; using System.Web; using System. Web.SessionState; use system.Web.ui; use system.web.ui.webcontrols; using system.web.ui.htmlcontrols; using crystaldecisions.crystalReports.Engine; using crystaldecisions.shared;

A summary description of Namespace CRDataSet {///

/// WebForm1. /// public class WebForm1: System.Web.UI.Page {OleDbConnection adoOleDbConnection; OleDbDataAdapter adoOleDbDataAdapter; public DataSet dataSet; CrystalReport1 crReportDocument; protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI .Webcontrols.textbox textbox1; protected crystaldecisions.web.crystalReportviewer CrystalReportViewer1;

private void ExportReport (ReportDocument Report, string OptionStr) {string ExportPath; ExportPath = Request.PhysicalApplicationPath "Exported //"; if (Directory.Exists (ExportPath) == false) Directory.CreateDirectory (Request.PhysicalApplicationPath "Exported // ");

REPORTDOCUMENT; DISKFILEDESTINATIONOPTIONOPTIONS = CRREPTIONS CREXPORTOTIONS = CRREPORTDocument.exportOptions;

switch (OptionStr) {case "Portable Document (PDF)": crDiskFileDestinationOptions.DiskFileName = ExportPath "PortableDoc.pdf"; crExportOptions.DestinationOptions = crDiskFileDestinationOptions; crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; crExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat; break; case "MS Word (DOC)": crDiskFileDestinationOptions.DiskFileName = ExportPath "Word.doc"; crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; crExportOptions.ExportFormatType = ExportFormatType.WordForWindows; crExportOptions.DestinationOptions = crDiskFileDestinationOptions; break;} try {// Export the report crReportDocument.Export (); crReportDocument.Close (); Response.ClearContent (); Response.ClearHeaders (); switch (OptionStr) {case "Portable Document (PDF)": Response.ContentType = "application / pdf" Response.writefile (ExportPat) H "portabledoc.pdf"); break; case "MS Word (DOC": response.contenttype = "Application / Msword"; response.writefile (ExportPath "word.doc"); Break;}

Response.close (); response.close ();} catonse.write ("
"); response.write (Err.Message.toString ());}} private void Page_load (Object) sender, System.EventArgs e) {// Put user code to initialize the page here string connectionString = ""; connectionString = "Provider = SQLOLEDB;"; connectionString = "Server = it003; Database = pubs;"; connectionString = "User ID = sa; password ="; // Create and open a connection using the connection string adooledbconnection = new oledbconnection (connectionString); CONNECTIONSTRING

// BUILD A SQL Statement to Query The Datasource String Sqlstring = ""; Sqlstring = "Select * from authors";

// Retrieve The Data Using The SQL Statement and Existing Connection AdooledBDataAdapter = New OLEDBDataAdapter (Sqlstring, AdooledbConnection);

// Create a instance of a dataset dataset = new dataset ();

. // Fill the dataset with the data retrieved The name of the table // in the dataset must be the same as the table name in the report adoOleDbDataAdapter.Fill (dataSet, "authors");. // Create an instance of the strongly-typed report object crReportDocument = new CrystalReport1 (); // Pass the populated dataset to the report crReportDocument.SetDataSource (dataSet); // Set the viewer to the report object to be previewed CrystalReportViewer1.ReportSource = crReportDocument;.}

#Region Web Form Designer Generated Code Override Protected Void OnNit (Eventargs E) {// // Codegen: This call is required for the ASP.NET Web Form Designer. // InitializeComponent (); base.onit (e);} ///

/// Designer Supports the required method - Do not use the code editor to modify the // / this method. /// private void InitializeComponent () {this.Button1.Click = new System.EventHandler (this.Button1_Click); this.Load = new System.EventHandler (this.Page_Load);} #endregion

private void Button1_Click (object sender, System.EventArgs e) {this.crReportDocument = new CrystalReport1 (); // Pass the populated dataset to the report DataSet ds = new DataSet (); OleDbConnection con = new OleDbConnection ( "Provider = SQLOLEDB; Server = localhost; data = pubs; uid = sa; pwd = "); con.open (); oledbdataadapter ada = new oledbdataadapter (" SELECT * AUTHORS WHERE AU_ID LIKE '% 899%' ", Con); ada.fill (DS, "authors"); crreportDocument.setDataSource (DS); ExportReport (this.crreportDocument, "Portable Document (PDF)");

}}}

转载请注明原文地址:https://www.9cbs.com/read-112565.html

New Post(0)