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;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace crdataset
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
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 crReportDocument = Report;
DiskFileDestinationOptions crDiskFileDestinationOptions = new DiskFileDestinationOptions();
ExportOptions crExportOptions = 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(ExportPath + "PortableDoc.pdf");
break;
case "MS Word (DOC)":
Response.ContentType="application/msword";
Response.WriteFile(ExportPath + "Word.doc");
break;
}
Response.Flush();
Response.Close();
}
catch (Exception err)
{
Response.Write("<BR>");
Response.Write(err.Message.ToString());
}
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
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);
//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 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
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;database=pubs;uid=sa;pwd=");
con.Open();
OleDbDataAdapter ada=new OleDbDataAdapter("select * from authors where au_id like '%899%'",con);
ada.Fill(ds,"authors");
crReportDocument.SetDataSource(ds);
ExportReport(this.crReportDocument,"Portable Document (PDF)");
}
}
}