水晶报表的导出和打印
using System;
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 System.IO;
using System.Data.SqlClient;
using UseCrystal.CrystalPush;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace CrystalPush
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnExport;
protected System.Web.UI.WebControls.Button btnPrint;
protected CrystalDecisions.Web.CrystalReportViewer Crv;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DropDownList ddlFormat;
myReport ReportDoc = new myReport();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string strProvider = "Server=(local);DataBase=myDatabase;UID=sa;PWD=111";
SqlConnection MyConn = new SqlConnection(strProvider);
MyConn.Open();
string strSel = "Select * from SaleOfCuntry";
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,MyConn);
DataSet1 ds = new DataSet1();
MyAdapter.Fill(ds,"SaleOfCuntry");
ReportDoc.SetDataSource(ds);
Crv.ReportSource = ReportDoc;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnExport.Click += new System.EventHandler(this.btnExport_Click);
this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
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 (ddlFormat.SelectedItem.Text)
{
case "Rich Text (RTF)":
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.RichText;//
DiskOpts.DiskFileName = "c:\\Output.rtf";//
break;
case "Portable Document (PDF)":
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;//
DiskOpts.DiskFileName = "c:\\Output.pdf";//
break;
case "MS Word (DOC)":
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.WordForWindows;//
DiskOpts.DiskFileName = "c:\\Output.doc";//
break;
case "MS Excel (XLS)":
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel;//
DiskOpts.DiskFileName = "c:\\Output.xls";//
break;
default:
break;
}
ReportDoc.ExportOptions.DestinationOptions = DiskOpts;
ReportDoc.Export();
}
private void btnPrint_Click(object sender, System.EventArgs e)
{
// 指定打印机名称,这里是网络工作站Gigi上的打印机Hp Jet 6
string strPrinterName;
strPrinterName = @"Canon Bubble-Jet BJC-210SP";
// 设置打印页边距
PageMargins margins;
margins = ReportDoc.PrintOptions.PageMargins;
margins.bottomMargin = 250;
margins.leftMargin = 350;
margins.rightMargin = 350;
margins.topMargin = 450;
ReportDoc.PrintOptions.ApplyPageMargins(margins);
//应用打印机名称
ReportDoc.PrintOptions.PrinterName = strPrinterName;
// 打印 // 打印报表。将 startPageN 和 endPageN
// 参数设置为 0 表示打印所有页。
ReportDoc.PrintToPrinter(1, false,0,0);
}
}
}
修改标题文本
private void btnChangeText_Click(object sender, System.EventArgs e)
{
TextObject tb = (TextObject )ReportDoc.ReportDefinition.ReportObjects["Text2"];
tb.Text = "订单号";
}