如何用Crystal Report10(企业版)实现客户端打印
2005-01-03 13:49
295 查看
常情况下,不需要写任何代码就可以打印。
如果出现不能打印的情况,请检查一下
一、是否已经安装打印机。
二、编写的程序是否有问题,如:是否再form_load里用if (!this.IsPostBack)将读取数据部分屏蔽了,因为在打印时需要从新到获取数据的。
问题终于解决了,贴出源代码
<%@ Register TagPrefix="cr" Namespace="CrystalDecisions.Web" Assembly="CrystalDecisions.Web, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" %>
<%@ Register TagPrefix="ce" Namespace="CrystalDecisions.Enterprise.WebControls" Assembly="CrystalDecisions.Enterprise.Web, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" %>
<%@ Page language="c#" Codebehind="Customers.aspx.cs" AutoEventWireup="false" Inherits="Angushine.Web.Report.Customers" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Customers</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<link type="text/css" rel="stylesheet" href="../Css.css">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体"></FONT>
<cr:CrystalReportViewer id="CrystalReportViewer1" runat="server" Height="50px" Width="350px" AutoDataBind="true"
HasExportButton="False" HasGotoPageButton="False" HasSearchButton="False" HasToggleGroupTreeButton="False"
HasZoomFactorList="False"></cr:CrystalReportViewer>
<BR>
<asp:Button id="btnCustomers" runat="server" Text="Customers"></asp:Button>
<asp:Button id="btnEmployees" runat="server" Text="Employees"></asp:Button>
<asp:Button id="btnProducts" runat="server" Text="Products"></asp:Button><BR>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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.Shared ; //TableLogOnInfo类
using CrystalDecisions.CrystalReports.Engine ; //ReportDocument类
namespace Angushine.Web.Report
{
/// <summary>
/// Customers 的摘要说明。
/// </summary>
public class Customers : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnEmployees;
protected System.Web.UI.WebControls.Button btnProducts;
protected System.Web.UI.WebControls.Button btnCustomers;
protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;
private static ReportDocument oRpt;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
}
else
{
//If the report has been previewed once, the ReportSource needs to be
//set on each postback. When the Preview Report button is first clicked,
//it loads the report, binds it to the viewer, and puts the ReportDocument
//object into a Session varable. This code pulls that ReportDocument object
//from Session and reassigns the viewer's ReportSource, for each postback.
if (oRpt != null)
{
CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
CrystalReportViewer1.ReportSource = oRpt;
}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnCustomers.Click += new System.EventHandler(this.btnCustomers_Click);
this.btnEmployees.Click += new System.EventHandler(this.btnEmployees_Click);
this.btnProducts.Click += new System.EventHandler(this.btnProducts_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnCustomers_Click(object sender, System.EventArgs e)
{
/*
* pull模式,建立rpt文件,以及web页面,其中页面控件不需指定数据源.
* */
TableLogOnInfo logOnInfo = new TableLogOnInfo ();
oRpt = new ReportDocument();
string path = this.Server.MapPath("CrReports/Customers.rpt");
oRpt.Load (path);
/* MS Server 2000数据库,其他作相应的改动 */
logOnInfo.ConnectionInfo.ServerName = "localhost";
logOnInfo.ConnectionInfo.UserID = "sa";
logOnInfo.ConnectionInfo.Password = "sa";
oRpt.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
CrystalReportViewer1.ReportSource = oRpt; //建立.rpt文件与CryStalReportviewer文件之间的连接
//The ReportDocument object will need to placed into Session so that
//on each postback, the viewer's ReportSource can be set to this instance
Session.Add("REPORT", oRpt);
}
private void btnEmployees_Click(object sender, System.EventArgs e)
{
string strDB = System.Configuration.ConfigurationSettings.AppSettings["strDB"];
SqlConnection conn = new SqlConnection(strDB);
SqlDataAdapter da = new SqlDataAdapter("Select * from Employees",conn);
CrReports.DataSet1 ds = new Angushine.Web.Report.CrReports.DataSet1();
CrReports.Employees myrpt = new Angushine.Web.Report.CrReports.Employees();
da.Fill(ds,"t");
myrpt.SetDataSource(ds.Tables["t"]);
myrpt.Refresh();
this.CrystalReportViewer1.ReportSource = myrpt;
//myrpt.PrintToPrinter(1,true,1,1);
this.CrystalReportViewer1.DataBind();
}
private void btnProducts_Click(object sender, System.EventArgs e)
{
// 数据源是连接Access
oRpt = new ReportDocument();
string path = this.Server.MapPath("CrReports/Customer.rpt");
oRpt.Load (path);
CrystalReportViewer1.ReportSource = oRpt;
}
private void ReportPrint1_SubmitClicked(object sender, System.EventArgs e)
{
//oRpt.PrintToPrinter(1, false,0,0);
}
private void CrystalReportViewer1_Init(object sender, System.EventArgs e)
{
}
}
}
如果出现不能打印的情况,请检查一下
一、是否已经安装打印机。
二、编写的程序是否有问题,如:是否再form_load里用if (!this.IsPostBack)将读取数据部分屏蔽了,因为在打印时需要从新到获取数据的。
问题终于解决了,贴出源代码
<%@ Register TagPrefix="cr" Namespace="CrystalDecisions.Web" Assembly="CrystalDecisions.Web, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" %>
<%@ Register TagPrefix="ce" Namespace="CrystalDecisions.Enterprise.WebControls" Assembly="CrystalDecisions.Enterprise.Web, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" %>
<%@ Page language="c#" Codebehind="Customers.aspx.cs" AutoEventWireup="false" Inherits="Angushine.Web.Report.Customers" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Customers</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<link type="text/css" rel="stylesheet" href="../Css.css">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体"></FONT>
<cr:CrystalReportViewer id="CrystalReportViewer1" runat="server" Height="50px" Width="350px" AutoDataBind="true"
HasExportButton="False" HasGotoPageButton="False" HasSearchButton="False" HasToggleGroupTreeButton="False"
HasZoomFactorList="False"></cr:CrystalReportViewer>
<BR>
<asp:Button id="btnCustomers" runat="server" Text="Customers"></asp:Button>
<asp:Button id="btnEmployees" runat="server" Text="Employees"></asp:Button>
<asp:Button id="btnProducts" runat="server" Text="Products"></asp:Button><BR>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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.Shared ; //TableLogOnInfo类
using CrystalDecisions.CrystalReports.Engine ; //ReportDocument类
namespace Angushine.Web.Report
{
/// <summary>
/// Customers 的摘要说明。
/// </summary>
public class Customers : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnEmployees;
protected System.Web.UI.WebControls.Button btnProducts;
protected System.Web.UI.WebControls.Button btnCustomers;
protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;
private static ReportDocument oRpt;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
}
else
{
//If the report has been previewed once, the ReportSource needs to be
//set on each postback. When the Preview Report button is first clicked,
//it loads the report, binds it to the viewer, and puts the ReportDocument
//object into a Session varable. This code pulls that ReportDocument object
//from Session and reassigns the viewer's ReportSource, for each postback.
if (oRpt != null)
{
CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
CrystalReportViewer1.ReportSource = oRpt;
}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnCustomers.Click += new System.EventHandler(this.btnCustomers_Click);
this.btnEmployees.Click += new System.EventHandler(this.btnEmployees_Click);
this.btnProducts.Click += new System.EventHandler(this.btnProducts_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnCustomers_Click(object sender, System.EventArgs e)
{
/*
* pull模式,建立rpt文件,以及web页面,其中页面控件不需指定数据源.
* */
TableLogOnInfo logOnInfo = new TableLogOnInfo ();
oRpt = new ReportDocument();
string path = this.Server.MapPath("CrReports/Customers.rpt");
oRpt.Load (path);
/* MS Server 2000数据库,其他作相应的改动 */
logOnInfo.ConnectionInfo.ServerName = "localhost";
logOnInfo.ConnectionInfo.UserID = "sa";
logOnInfo.ConnectionInfo.Password = "sa";
oRpt.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
CrystalReportViewer1.ReportSource = oRpt; //建立.rpt文件与CryStalReportviewer文件之间的连接
//The ReportDocument object will need to placed into Session so that
//on each postback, the viewer's ReportSource can be set to this instance
Session.Add("REPORT", oRpt);
}
private void btnEmployees_Click(object sender, System.EventArgs e)
{
string strDB = System.Configuration.ConfigurationSettings.AppSettings["strDB"];
SqlConnection conn = new SqlConnection(strDB);
SqlDataAdapter da = new SqlDataAdapter("Select * from Employees",conn);
CrReports.DataSet1 ds = new Angushine.Web.Report.CrReports.DataSet1();
CrReports.Employees myrpt = new Angushine.Web.Report.CrReports.Employees();
da.Fill(ds,"t");
myrpt.SetDataSource(ds.Tables["t"]);
myrpt.Refresh();
this.CrystalReportViewer1.ReportSource = myrpt;
//myrpt.PrintToPrinter(1,true,1,1);
this.CrystalReportViewer1.DataBind();
}
private void btnProducts_Click(object sender, System.EventArgs e)
{
// 数据源是连接Access
oRpt = new ReportDocument();
string path = this.Server.MapPath("CrReports/Customer.rpt");
oRpt.Load (path);
CrystalReportViewer1.ReportSource = oRpt;
}
private void ReportPrint1_SubmitClicked(object sender, System.EventArgs e)
{
//oRpt.PrintToPrinter(1, false,0,0);
}
private void CrystalReportViewer1_Init(object sender, System.EventArgs e)
{
}
}
}
相关文章推荐
- 如何实现.NET在客户端的打印word并保存至默认文件夹
- 如何实现客户端打印web应用上的文件,打印机在客户端上
- 实时进销存如何帮助企业从销售、采购到库存实现一体化管理?
- 探究ftp的奥妙之系统用户如何上传下载文件、vsftpd如何识别证书并利用客户端来实现远程连接传输
- 如何:在 MVC 中从客户端实现远程验证
- wince串口打印函数是如何实现的?(作者wogoyixikexie@gliet)
- C#在一台客户端进行了某个操作以后如何让其他不同的客户端如何实现相同的操作(三)
- 在Web中如何运用JavaScript实现打印功能
- QQ或者微信客户端加载本地图片是如何实现的?
- html5 如何实现客户端验证上传文件的大小
- TDPO和TSM客户端是如何实现ORACLE备份的
- rdlc报表 打印图片功能如何实现
- C#下如何实现服务器+客户端的聊天程序
- 如何实现不需要Active控件的分页打印
- 如何实现页面的打印版本
- 如何用Socket实现客户端通信模块
- RichTextBox如何实现所见即所得打印
- ERP如何帮助服装企业实现渠道扁平化?
- 如何在ASP中实现网络打印功能
- 如何使用JCA (J2EE 连接器架构)实现企业应用