//******************************************** // //File Name: WebExportReport.sln //Created: May 17, 2002 //Author ID: CHO //Purpose: This sample application demonstrates how to export your report // to the following formats: // // - Rich Text Format (RTF) // - Microsoft Word Format (DOC) // - Portable Document Format (PDF) // - Microsoft Excel (XLS) // - Crystal Report (RPT) // - HTML 3.2 (HTML) // - HTML 4.0 (DHTML) // //********************************************using System; using System.IO; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared;namespace WebExportReport { /// <summary> /// Summary description for WebForm1. /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.DropDownList DropDownList1; protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;
// CR Variables Database crDatabase; Tables crTables; TableLogOnInfo crTableLogOnInfo; ConnectionInfo crConnectionInfo; ReportDocument crReportDocument; ExportOptions crExportOptions; DiskFileDestinationOptions crDiskFileDestinationOptions; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e);
crReportDocument = new Chart(); crConnectionInfo = new ConnectionInfo(); crConnectionInfo.ServerName = "dataserver"; crConnectionInfo.DatabaseName = "Pubs"; crConnectionInfo.UserID = "sa"; crConnectionInfo.Password = "btk"; //Get the tables collection from the report object crDatabase = crReportDocument.Database; crTables = crDatabase.Tables; //Apply the logon information to each table in the collection foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables) { crTableLogOnInfo = crTable.LogOnInfo; crTableLogOnInfo.ConnectionInfo = crConnectionInfo; crTable.ApplyLogOnInfo(crTableLogOnInfo); } //Create an instance of the strongly-typed report object CrystalReportViewer1.ReportSource = crReportDocument; // ******************************************* // Initialize Dropdownlist for Format types // ******************************************* DropDownList1.Items.Add(""); DropDownList1.Items.Add("Rich Text (RTF)"); DropDownList1.Items.Add("Portable Document (PDF)"); DropDownList1.Items.Add("MS Word (DOC)"); DropDownList1.Items.Add("MS Excel (XLS)"); DropDownList1.Items.Add("Crystal Report (RPT)"); DropDownList1.Items.Add("HTML 3.2 (HTML)"); DropDownList1.Items.Add("HTML 4.0 (HTML)"); }
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void ExportReport() { // This subroutine uses a case statement to determine the selected export format from the dropdownlist // menu and then sets the appropriate export options for the selected export format. The report is // exported to a subdirectory called "Exported". // ******************************** //Check to see if the application directory has a subdirectory called "Exported". //If not, create the directory since exported files will be placed here. //This uses the Directory class of the System.IO namespace. string ExportPath; ExportPath = Request.PhysicalApplicationPath + "Exported\\"; if (Directory.Exists(ExportPath) == false) Directory.CreateDirectory(Request.PhysicalApplicationPath + "Exported\\"); // ******************************** // First we must create a new instance of the diskfiledestinationoptions class and // set variable called crExportOptions to the exportoptions class of the reportdocument. crDiskFileDestinationOptions = new DiskFileDestinationOptions(); crExportOptions = crReportDocument.ExportOptions; //Find the export type specified in the dropdownlist and export the report. The possible export format //types are Rich Text(RTF), Portable Document (PDF), MS Word (DOC), MS Excel (XLS), Crystal Report (RPT), //HTML 3.2 (HTML) and HTML 4.0 (HTML) // //Though not used in this sample application, there are options that can be specified for various format types. //When exporting to Rich Text, Word, or PDF, you can use the PdfRtfWordFormatOptions class to specify the //first page, last page or page range to be exported. //When exporting to Excel, you can use the ExcelFormatOptions class to specify export properties such as //the column width etc. if (DropDownList1.SelectedItem.Text == "Rich Text (RTF)") { //-------------------------------------------------------------------- //Export to RTF. //append a filename to the export path and set this file as the filename property for //the DestinationOptions class crDiskFileDestinationOptions.DiskFileName = ExportPath + "RichTextFormat.rtf"; //set the required report ExportOptions properties crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; crExportOptions.ExportFormatType = ExportFormatType.RichText; crExportOptions.DestinationOptions = crDiskFileDestinationOptions; //-------------------------------------------------------------------- } else if (DropDownList1.SelectedItem.Text == "Portable Document (PDF)") { //-------------------------------------------------------------------- //Export to PDF
接上面的 //append a filename to the export path and set this file as the filename property for //the DestinationOptions class crDiskFileDestinationOptions.DiskFileName = ExportPath + "PortableDoc.pdf"; //set the required report ExportOptions properties crExportOptions.DestinationOptions = crDiskFileDestinationOptions; crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; crExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat; //-------------------------------------------------------------------- } else if (DropDownList1.SelectedItem.Text == "MS Word (DOC)") { //-------------------------------------------------------------------- //Export to Word //append a filename to the export path and set this file as the filename property for //the DestinationOptions class crDiskFileDestinationOptions.DiskFileName = ExportPath + "Word.doc"; //set the required report ExportOptions properties crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; crExportOptions.ExportFormatType = ExportFormatType.WordForWindows; crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
//-------------------------------------------------------------------- } else if (DropDownList1.SelectedItem.Text == "MS Excel (XLS)") { //-------------------------------------------------------------------- //Export to Excel //append a filename to the export path and set this file as the filename property for //the DestinationOptions class crDiskFileDestinationOptions.DiskFileName = ExportPath + "Excel.xls"; //set the required report ExportOptions properties crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; crExportOptions.ExportFormatType = ExportFormatType.Excel; crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
//-------------------------------------------------------------------- } else if (DropDownList1.SelectedItem.Text == "Crystal Report (RPT)") { //-------------------------------------------------------------------- //Export to Crystal reports: //append a filename to the export path and set this file as the filename property for //the DestinationOptions class crDiskFileDestinationOptions.DiskFileName = ExportPath + "Report.rpt"; //set the required report ExportOptions properties crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; crExportOptions.ExportFormatType = ExportFormatType.CrystalReport; crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
//-------------------------------------------------------------------- } else if (DropDownList1.SelectedItem.Text == "HTML 3.2 (HTML)") { //-------------------------------------------------------------------- //Export to HTML32: HTMLFormatOptions HTML32Formatopts = new HTMLFormatOptions(); crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; crExportOptions.ExportFormatType = ExportFormatType.HTML32; HTML32Formatopts.HTMLBaseFolderName = ExportPath + "Html32Folder"; //Foldername to place HTML files HTML32Formatopts.HTMLFileName = "HTML32.html"; HTML32Formatopts.HTMLEnableSeparatedPages = false; HTML32Formatopts.HTMLHasPageNavigator = false; crExportOptions.FormatOptions = HTML32Formatopts; //-------------------------------------------------------------------- } else if (DropDownList1.SelectedItem.Text == "HTML 4.0 (HTML)") { //-------------------------------------------------------------------- //Export to Html 4.0: HTMLFormatOptions HTML40Formatopts = new HTMLFormatOptions(); crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; crExportOptions.ExportFormatType = ExportFormatType.HTML40; HTML40Formatopts.HTMLBaseFolderName = ExportPath + "Html40Folder"; // Foldername to place HTML files HTML40Formatopts.HTMLFileName = "HTML40.html"; HTML40Formatopts.HTMLEnableSeparatedPages = true; HTML40Formatopts.HTMLHasPageNavigator = true; HTML40Formatopts.FirstPageNumber = 1; HTML40Formatopts.LastPageNumber = 3; crExportOptions.FormatOptions = HTML40Formatopts; } //export format //Once the export options have been set for the report, the report can be exported. The Export command //does not take any arguments try { // Export the report crReportDocument.Export(); } catch (Exception err) { Response.Write("<BR>"); Response.Write(err.Message.ToString()); } }
private void Button1_Click(object sender, System.EventArgs e) { //Clicking on the "Export Report" button will run the ExportReport subroutine and export the report based on the //selected format from the dropdownlist. ExportReport(); } } }
//
//File Name: WebExportReport.sln
//Created: May 17, 2002
//Author ID: CHO
//Purpose: This sample application demonstrates how to export your report
// to the following formats:
//
// - Rich Text Format (RTF)
// - Microsoft Word Format (DOC)
// - Portable Document Format (PDF)
// - Microsoft Excel (XLS)
// - Crystal Report (RPT)
// - HTML 3.2 (HTML)
// - HTML 4.0 (DHTML)
//
//********************************************using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;namespace WebExportReport
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;
// CR Variables
Database crDatabase;
Tables crTables;
TableLogOnInfo crTableLogOnInfo;
ConnectionInfo crConnectionInfo; ReportDocument crReportDocument;
ExportOptions crExportOptions;
DiskFileDestinationOptions crDiskFileDestinationOptions; private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
crReportDocument = new Chart(); crConnectionInfo = new ConnectionInfo();
crConnectionInfo.ServerName = "dataserver";
crConnectionInfo.DatabaseName = "Pubs";
crConnectionInfo.UserID = "sa";
crConnectionInfo.Password = "btk"; //Get the tables collection from the report object
crDatabase = crReportDocument.Database;
crTables = crDatabase.Tables; //Apply the logon information to each table in the collection
foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
{
crTableLogOnInfo = crTable.LogOnInfo;
crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
crTable.ApplyLogOnInfo(crTableLogOnInfo);
} //Create an instance of the strongly-typed report object CrystalReportViewer1.ReportSource = crReportDocument; // *******************************************
// Initialize Dropdownlist for Format types
// *******************************************
DropDownList1.Items.Add("");
DropDownList1.Items.Add("Rich Text (RTF)");
DropDownList1.Items.Add("Portable Document (PDF)");
DropDownList1.Items.Add("MS Word (DOC)");
DropDownList1.Items.Add("MS Excel (XLS)");
DropDownList1.Items.Add("Crystal Report (RPT)");
DropDownList1.Items.Add("HTML 3.2 (HTML)");
DropDownList1.Items.Add("HTML 4.0 (HTML)");
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void ExportReport()
{
// This subroutine uses a case statement to determine the selected export format from the dropdownlist
// menu and then sets the appropriate export options for the selected export format. The report is
// exported to a subdirectory called "Exported". // ********************************
//Check to see if the application directory has a subdirectory called "Exported".
//If not, create the directory since exported files will be placed here.
//This uses the Directory class of the System.IO namespace.
string ExportPath;
ExportPath = Request.PhysicalApplicationPath + "Exported\\";
if (Directory.Exists(ExportPath) == false) Directory.CreateDirectory(Request.PhysicalApplicationPath + "Exported\\"); // ********************************
// First we must create a new instance of the diskfiledestinationoptions class and
// set variable called crExportOptions to the exportoptions class of the reportdocument.
crDiskFileDestinationOptions = new DiskFileDestinationOptions();
crExportOptions = crReportDocument.ExportOptions;
//Find the export type specified in the dropdownlist and export the report. The possible export format
//types are Rich Text(RTF), Portable Document (PDF), MS Word (DOC), MS Excel (XLS), Crystal Report (RPT),
//HTML 3.2 (HTML) and HTML 4.0 (HTML)
//
//Though not used in this sample application, there are options that can be specified for various format types.
//When exporting to Rich Text, Word, or PDF, you can use the PdfRtfWordFormatOptions class to specify the
//first page, last page or page range to be exported.
//When exporting to Excel, you can use the ExcelFormatOptions class to specify export properties such as
//the column width etc. if (DropDownList1.SelectedItem.Text == "Rich Text (RTF)")
{
//--------------------------------------------------------------------
//Export to RTF. //append a filename to the export path and set this file as the filename property for
//the DestinationOptions class
crDiskFileDestinationOptions.DiskFileName = ExportPath + "RichTextFormat.rtf"; //set the required report ExportOptions properties
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.RichText;
crExportOptions.DestinationOptions = crDiskFileDestinationOptions; //--------------------------------------------------------------------
}
else if (DropDownList1.SelectedItem.Text == "Portable Document (PDF)")
{
//--------------------------------------------------------------------
//Export to PDF
//append a filename to the export path and set this file as the filename property for
//the DestinationOptions class
crDiskFileDestinationOptions.DiskFileName = ExportPath + "PortableDoc.pdf"; //set the required report ExportOptions properties
crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat; //--------------------------------------------------------------------
}
else if (DropDownList1.SelectedItem.Text == "MS Word (DOC)")
{
//--------------------------------------------------------------------
//Export to Word
//append a filename to the export path and set this file as the filename property for
//the DestinationOptions class
crDiskFileDestinationOptions.DiskFileName = ExportPath + "Word.doc"; //set the required report ExportOptions properties
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.WordForWindows;
crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
//--------------------------------------------------------------------
}
else if (DropDownList1.SelectedItem.Text == "MS Excel (XLS)")
{
//--------------------------------------------------------------------
//Export to Excel //append a filename to the export path and set this file as the filename property for
//the DestinationOptions class
crDiskFileDestinationOptions.DiskFileName = ExportPath + "Excel.xls"; //set the required report ExportOptions properties
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.Excel;
crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
//--------------------------------------------------------------------
}
else if (DropDownList1.SelectedItem.Text == "Crystal Report (RPT)")
{
//--------------------------------------------------------------------
//Export to Crystal reports: //append a filename to the export path and set this file as the filename property for
//the DestinationOptions class
crDiskFileDestinationOptions.DiskFileName = ExportPath + "Report.rpt"; //set the required report ExportOptions properties
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.CrystalReport;
crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
//--------------------------------------------------------------------
}
else if (DropDownList1.SelectedItem.Text == "HTML 3.2 (HTML)")
{
//--------------------------------------------------------------------
//Export to HTML32: HTMLFormatOptions HTML32Formatopts = new HTMLFormatOptions(); crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.HTML32; HTML32Formatopts.HTMLBaseFolderName = ExportPath + "Html32Folder"; //Foldername to place HTML files
HTML32Formatopts.HTMLFileName = "HTML32.html";
HTML32Formatopts.HTMLEnableSeparatedPages = false;
HTML32Formatopts.HTMLHasPageNavigator = false; crExportOptions.FormatOptions = HTML32Formatopts;
//--------------------------------------------------------------------
}
else if (DropDownList1.SelectedItem.Text == "HTML 4.0 (HTML)")
{
//--------------------------------------------------------------------
//Export to Html 4.0: HTMLFormatOptions HTML40Formatopts = new HTMLFormatOptions(); crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.HTML40; HTML40Formatopts.HTMLBaseFolderName = ExportPath + "Html40Folder"; // Foldername to place HTML files
HTML40Formatopts.HTMLFileName = "HTML40.html";
HTML40Formatopts.HTMLEnableSeparatedPages = true;
HTML40Formatopts.HTMLHasPageNavigator = true;
HTML40Formatopts.FirstPageNumber = 1;
HTML40Formatopts.LastPageNumber = 3; crExportOptions.FormatOptions = HTML40Formatopts;
} //export format //Once the export options have been set for the report, the report can be exported. The Export command
//does not take any arguments
try
{
// Export the report
crReportDocument.Export();
}
catch (Exception err)
{
Response.Write("<BR>");
Response.Write(err.Message.ToString());
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
//Clicking on the "Export Report" button will run the ExportReport subroutine and export the report based on the
//selected format from the dropdownlist.
ExportReport();
}
}
}