注:由于自带的水晶报表Web方式下无法打印,所以,需要结合CrystalReportView和Javascript来实现打印的控制,包括记录打印人信息。另外操作中对打印机的参数进行了设置。
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="PrintForm.aspx.vb" Inherits="RC.PrintForm"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>PrintForm</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<style>.Display { }
.NotDisplay { DISPLAY: none }
.tbSeparateHeaderHeight { HEIGHT: 25px }
.tbSeparateFooterHeight { HEIGHT: 30px }
</style>
<script language="javascript">
//隐藏某段 HTML 对象
function HidePnl(objName)
{
Form1.all(objName).className='NotDisplay';
}
//显示某段 HTML 对象
function ShowPnl(objName)
{
Form1.all(objName).className='Display';
}
//打印操作执行前需要执行的动作
function beforePrint()
{
Form1.all.TbWait.style.display='';
Form1.all.imgPrint.style.display='none';
HidePnl('pnlShowPart');
}
//打印操作
function printReport()
{
try
{
ShowPnl('pnlPrintPart');
HidePnl('pnlShowPart');
//隐藏 Waitting
Form1.all.imgPrint.style.display='none';
Form1.all.TbWait.style.display = 'none';
//设置打印机属性
pageSetup_null();
//打印所有的报表
window.print();
//调用打印完成后的处理函数
afterPrint();
}catch(e){alert(e);}
}
//打印操作执行后需要执行的动作
function afterPrint()
{
ShowPnl('pnlShowPart');
HidePnl('pnlPrintPart');
//开启 [打印] 按钮
Form1.all.imgPrint.style.display='';
}
</script>
<script language="VBScript">
dim hkey_root,hkey_path,hkey_key
hkey_root="HKEY_CURRENT_USER"
hkey_path="\Software\Microsoft\Internet Explorer\PageSetup"
'//設置網頁打印的頁眉頁腳為空
function pageSetup_null()
on error resume next
Set RegWsh = CreateObject("WScript.Shell")
hkey_key="\header"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,""
hkey_key="\footer"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,""
hkey_key="\margin_bottom"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"0.25000"
hkey_key="\margin_left"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"0.10000"
hkey_key="\margin_right"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"0.10000"
hkey_key="\margin_top"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"0.25000"
end function
function pageSetup_default()
on error resume next
Set RegWsh = CreateObject("WScript.Shell")
hkey_key="\header"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"&w&b第&p页 共&P页"
hkey_key="\footer"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"&u&b&d"
end function
</script>
<script language="javascript">
//禁止右键
document.oncontextmenu=function()
{ return false; }
//禁止复制
document.onselectstart=function()
{ return false; }
document.ondragstart=function()
{ return false; }
//双击鼠标滚动屏幕的代码
var currentpos,timer;
function initialize()
{
timer=setInterval ("scrollwindow ()",30);
}
function sc()
{
clearInterval(timer);
}
function scrollwindow()
{
currentpos=document.body.scrollTop;
window.scrollTo(0,++currentpos);
if (currentpos !=document.body.scrollTop)
sc();
}
document.onmousedown=sc
document.ondblclick=initialize
//为了防止用户直接进入页面,在Page Load的时候,加入下面的检察
function onloadCheck()
{
if (window.opener==null)
{
alert('Not Permission! The page will be closed.');
window.opener="deny";
self.close();
}
}
</script>
</HEAD>
<body leftMargin="0" topMargin="0" MS_POSITIONING="GridLayout" onload="onloadCheck();" onunload="pageSetup_default();">
<form id="Form1" method="post" runat="server">
<asp:panel id="pnlShowPart" runat="server"></asp:panel><asp:panel id="pnlPrintPart" Runat="server"></asp:panel>
<!--***********************************************************************************
TbWait :点击 [打印] 按钮之后,出现的一个提示信息区域
********************************************************************************-->
<TABLE id="TbWait" style="DISPLAY: none" height="80" cellSpacing="1" cellPadding="0" width="300"
align="center" bgColor="#999999" border="0">
<TR>
<TD>
<TABLE height="100%" cellSpacing="0" cellPadding="0" width="100%" bgColor="#999999" border="0">
<TR bgColor="#ffffff">
<TD align="center" width="20%"><IMG height="32" src="../icon/waiting.gif" width="32" align="middle"></TD>
<TD vAlign="middle" align="left"><FONT color="black" size="2"><B>
正在验证打印请求,请稍侯</B></FONT></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<!--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
添加打印按钮
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<INPUT id="imgPrint" title="打印" onclick="beforePrint();" type="image" height="26" width="30"
src="/RC/icon/printer.jpg" runat="server" style="Z-INDEX: 12345; LEFT: 608px; POSITION: absolute; TOP: 8px">
</form>
</body>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>PrintForm</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<style>.Display { }
.NotDisplay { DISPLAY: none }
.tbSeparateHeaderHeight { HEIGHT: 25px }
.tbSeparateFooterHeight { HEIGHT: 30px }
</style>
<script language="javascript">
//隐藏某段 HTML 对象
function HidePnl(objName)
{
Form1.all(objName).className='NotDisplay';
}
//显示某段 HTML 对象
function ShowPnl(objName)
{
Form1.all(objName).className='Display';
}
//打印操作执行前需要执行的动作
function beforePrint()
{
Form1.all.TbWait.style.display='';
Form1.all.imgPrint.style.display='none';
HidePnl('pnlShowPart');
}
//打印操作
function printReport()
{
try
{
ShowPnl('pnlPrintPart');
HidePnl('pnlShowPart');
//隐藏 Waitting
Form1.all.imgPrint.style.display='none';
Form1.all.TbWait.style.display = 'none';
//设置打印机属性
pageSetup_null();
//打印所有的报表
window.print();
//调用打印完成后的处理函数
afterPrint();
}catch(e){alert(e);}
}
//打印操作执行后需要执行的动作
function afterPrint()
{
ShowPnl('pnlShowPart');
HidePnl('pnlPrintPart');
//开启 [打印] 按钮
Form1.all.imgPrint.style.display='';
}
</script>
<script language="VBScript">
dim hkey_root,hkey_path,hkey_key
hkey_root="HKEY_CURRENT_USER"
hkey_path="\Software\Microsoft\Internet Explorer\PageSetup"
'//設置網頁打印的頁眉頁腳為空
function pageSetup_null()
on error resume next
Set RegWsh = CreateObject("WScript.Shell")
hkey_key="\header"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,""
hkey_key="\footer"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,""
hkey_key="\margin_bottom"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"0.25000"
hkey_key="\margin_left"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"0.10000"
hkey_key="\margin_right"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"0.10000"
hkey_key="\margin_top"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"0.25000"
end function
function pageSetup_default()
on error resume next
Set RegWsh = CreateObject("WScript.Shell")
hkey_key="\header"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"&w&b第&p页 共&P页"
hkey_key="\footer"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"&u&b&d"
end function
</script>
<script language="javascript">
//禁止右键
document.oncontextmenu=function()
{ return false; }
//禁止复制
document.onselectstart=function()
{ return false; }
document.ondragstart=function()
{ return false; }
//双击鼠标滚动屏幕的代码
var currentpos,timer;
function initialize()
{
timer=setInterval ("scrollwindow ()",30);
}
function sc()
{
clearInterval(timer);
}
function scrollwindow()
{
currentpos=document.body.scrollTop;
window.scrollTo(0,++currentpos);
if (currentpos !=document.body.scrollTop)
sc();
}
document.onmousedown=sc
document.ondblclick=initialize
//为了防止用户直接进入页面,在Page Load的时候,加入下面的检察
function onloadCheck()
{
if (window.opener==null)
{
alert('Not Permission! The page will be closed.');
window.opener="deny";
self.close();
}
}
</script>
</HEAD>
<body leftMargin="0" topMargin="0" MS_POSITIONING="GridLayout" onload="onloadCheck();" onunload="pageSetup_default();">
<form id="Form1" method="post" runat="server">
<asp:panel id="pnlShowPart" runat="server"></asp:panel><asp:panel id="pnlPrintPart" Runat="server"></asp:panel>
<!--***********************************************************************************
TbWait :点击 [打印] 按钮之后,出现的一个提示信息区域
********************************************************************************-->
<TABLE id="TbWait" style="DISPLAY: none" height="80" cellSpacing="1" cellPadding="0" width="300"
align="center" bgColor="#999999" border="0">
<TR>
<TD>
<TABLE height="100%" cellSpacing="0" cellPadding="0" width="100%" bgColor="#999999" border="0">
<TR bgColor="#ffffff">
<TD align="center" width="20%"><IMG height="32" src="../icon/waiting.gif" width="32" align="middle"></TD>
<TD vAlign="middle" align="left"><FONT color="black" size="2"><B>
正在验证打印请求,请稍侯</B></FONT></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<!--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
添加打印按钮
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<INPUT id="imgPrint" title="打印" onclick="beforePrint();" type="image" height="26" width="30"
src="/RC/icon/printer.jpg" runat="server" style="Z-INDEX: 12345; LEFT: 608px; POSITION: absolute; TOP: 8px">
</form>
</body>
</HTML>
'*********************************************************************************************************************
'Author : SunnyZhao
'Vendor : Facet - 全领(上海)信息科技
'DateTime : 2005/08/02
'From Page : PrintReleaseCard.aspx
'Page Desc : 根据从页面 PrintReleaseCard.aspx 传过来的参数查询并显示报表,并提供报表打印之功能
'额外说明 : 当用户点击了 [打印] 按钮之后,将执行下列步骤
' 1. 判断用户有没有打印的权限,是否可以让用户打印。如果不让,提示信息;
' 2. 如果用户可以打印,那么修改数据库,添加用户打印信息;
'*********************************************************************************************************************
Imports System.Data.SqlClient
Imports CrystalDecisions.Web
Public Class PrintFormClass PrintForm
Inherits System.Web.UI.Page
Private myDBOx As New RC.DatabaseOperation
Private CryRptViewer As New CrystalReportViewer
Private intPageCount As Integer
Private myDataTable As DataTable
Private myReport '水晶报表的实例,暂时还无法声明,要根据后面的传入参数判断是哪张报表
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
End Sub
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Protected WithEvents pnlShowPart As System.Web.UI.WebControls.Panel
Protected WithEvents pnlPrintPart As System.Web.UI.WebControls.Panel
Protected WithEvents imgPrint As System.Web.UI.HtmlControls.HtmlInputImage
Private Sub Page_Init()Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
#Region "-- Page_Load --"
'Page_Load动作中,只将供用户查看的报表总结出来,待打印报表暂时还不需要总结出来,以增加系统速度
Private Sub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
'第一次 Load 页面,先清空 Session 的值
If Not Page.IsPostBack Then
Session("ReportPrint_Data") = Nothing
End If
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'明确定义一个水晶报表的实例,用来显示给用户查看以及操作
'由于本页面是打印通用页面,根据不同的传入参数,可以打印不同的报表
If Page.Request.QueryString("from") Is Nothing Then
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('无法验证打印请求!');"",300);</script>")
Exit Sub
End If
Dim strPrintType As String = Page.Request.QueryString("from")
'在下面就要根据不同的报表请求,实例化不同的水晶报表类即可
Select Case strPrintType
Case "PrintReleaseCard"
myReport = New ReleaseCardReport
Case Else
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('无此报表!');"",300);</script>")
Exit Sub
End Select
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'根据传入参数查询数据
Me.myDataTable = Me.SelectData()
If Me.myDataTable Is Nothing OrElse Me.myDataTable.Rows.Count = 0 Then
Exit Sub
End If
myReport.SetDataSource(myDataTable) '设置数据源
CryRptViewer.ID = "cryViewerShow"
CryRptViewer.DisplayGroupTree = False
'CryRptViewer.DisplayToolbar = False
CryRptViewer.ReportSource = myReport
'将水晶报表的实例放置到页面的适当位置
Me.pnlShowPart.Controls.Add(CryRptViewer)
'正常情况下,不显示 pnlPrintPart
Me.pnlPrintPart.CssClass = "NotDisplay"
'第一次 Load 页面的时候
If Not Page.IsPostBack Then
'给出 第几页/共几页 的值
Me.intPageCount = myDataTable.Rows.Count / 8
'Me.txtPageNO.Text = "1/" + CStr(Me.intPageCount)
End If
Catch ex As Exception
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('" + ex.Message + "');"",300);</script>")
End Try
End Sub
#End Region
#Region "-- 根据传入参数查询数据 --"
'*********************************************************************************************************************
'Author : SunnyZhao DateTime: 2005/08/02
'Function Name: SelectData
'Input : 从页面 PrintReleaseCard.aspx 传过来的参数
'Output : 放置查询结果的 DataTable
'Function Desc: 根据传入参数,判断当前要操作的是 [什么报表] ,查询相应的数据
'*********************************************************************************************************************
Private Function SelectData()Function SelectData() As DataTable
Dim strPrintType As String
Dim tbTemp As DataTable
Try
'由于本页面是打印通用页面,根据不同的传入参数,可以打印不同的报表
strPrintType = Page.Request.QueryString("from")
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'打印不同的报表,添加不同的 Case 选项,查找不同的需要数据源即可
'根据不同的打印请求,查询相应数据
Select Case strPrintType
Case "PrintReleaseCard"
If Page.Request.QueryString("beginRN") Is Nothing Or Page.Request.QueryString("endRN") Is Nothing Or Page.Request.QueryString("printDateType") Is Nothing Then
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('传入参数不正确!');"",300);</script>")
Return Nothing
End If
Dim strbeginRN As String = CStr(Page.Request.QueryString("beginRN"))
Dim strendRN As String = CStr(Page.Request.QueryString("endRN"))
Dim strDateType As String = CStr(Page.Request.QueryString("printDateType"))
Dim beginReleaseNo As New SqlParameter("@beginRN", strbeginRN)
Dim endReleaseNo As New SqlParameter("@endRN", strendRN)
Dim printDate As New SqlParameter("@printDate", strDateType)
'为了避免重复操作数据库,我们将查询出来的数据暂时保存在一个 Session 里面
'如果 Session 已经过期,那么就需要重新的对数据库进行操作
If Session("ReportPrint_Data") Is Nothing Then
tbTemp = myDBOx.GetDataAsDataTable(DatabaseOperation.ServerConnection.conn_RCS, "RCS_PrintReleaseCard", DatabaseOperation.SQLType.SQLStoredProcedure, beginReleaseNo, endReleaseNo, printDate)
Session("ReportPrint_Data") = tbTemp
Else
tbTemp = CType(Session("ReportPrint_Data"), DataTable)
End If
Case Else
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('无此报表!');"",300);</script>")
Return Nothing
End Select
'判断有没有查询到数据
If tbTemp.Rows.Count = 0 Then
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('查无相关数据!');"",300);</script>")
Return Nothing
End If
'返回需要的结果
Return tbTemp
Catch ex As Exception
Throw ex
End Try
End Function
#End Region
#Region "-- 总结所有有待打印的报表到指定的 Panel中 --"
'*********************************************************************************************************************
'Author : SunnyZhao DateTime: 2005/08/02
'Function Name: GenerateAllPrintedReprot
'Input : 已经查询好的数据 myDataTable
'Output : 放置报表到页面
'Function Desc: 根据数据内容,总结所有的报表内容到页面,但不显示
'*********************************************************************************************************************
Private Sub GenerateAllPrintedReprot()Sub GenerateAllPrintedReprot()
Dim intCurrIndex As Integer
'Dim pnlReportLayer As Panel
'定义两个 Table,用来对报表的排版做间隔之用
Dim tbSeparateHeader As Table
Dim tbSeparateFooter As Table
Dim cryPrintRpt
Dim cryPrintViewer As CrystalReportViewer
Try
Me.myDataTable = Me.SelectData()
Me.intPageCount = myDataTable.Rows.Count / 8
'逐个操作所有的报表页面
For intCurrIndex = 1 To Me.intPageCount
'实例化
'pnlReportLayer = New Panel
Dim strPrintType As String = Page.Request.QueryString("from")
'在下面就要根据不同的报表请求,实例化不同的水晶报表类即可
Select Case strPrintType
Case "PrintReleaseCard"
cryPrintRpt = New ReleaseCardReport
Case Else
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('无此报表!');"",300);</script>")
Exit Sub
End Select
cryPrintViewer = New CrystalReportViewer
tbSeparateHeader = New Table
tbSeparateFooter = New Table
'因为报表太靠上,在它前面多加一个 Table,增加一些间距,并为了控制分页打印的正确性,在下面页多加一个 Table
tbSeparateHeader.CssClass = "tbSeparateHeaderHeight" '主要设置它的高度
tbSeparateHeader.ID = "tbSeparateHeader" + CStr(intCurrIndex)
tbSeparateFooter.CssClass = "tbSeparateFooterHeight" '主要设置它的高度
tbSeparateFooter.ID = "tbSeparateFooter" + CStr(intCurrIndex)
Dim tr As TableRow
Dim td As TableCell
td = New TableCell
td.Text = " "
tr = New TableRow
tr.Cells.Add(td)
tbSeparateHeader.Rows.Add(tr)
td = New TableCell
td.Text = " "
tr = New TableRow
tr.Cells.Add(td)
tbSeparateFooter.Rows.Add(tr)
'设置数据源
cryPrintRpt.SetDataSource(myDataTable)
cryPrintViewer.ID = "cryPrintViewer" + CStr(intCurrIndex)
cryPrintViewer.DisplayGroupTree = False
cryPrintViewer.DisplayToolbar = False
cryPrintViewer.ReportSource = cryPrintRpt
'将水晶报表的实例放置到页面的适当位置
'pnlReportLayer.ID = "pnlReportLayer" + CStr(intCurrIndex)
'pnlReportLayer.CssClass = "reportHeight"
'pnlReportLayer.Controls.Add(tbSeparate)
'pnlReportLayer.Controls.Add(cryPrintViewer)
Me.pnlPrintPart.Controls.Add(tbSeparateHeader)
Me.pnlPrintPart.Controls.Add(cryPrintViewer)
Me.pnlPrintPart.Controls.Add(tbSeparateFooter)
'设置当前报表应该显示的页码
cryPrintViewer.ShowNthPage(intCurrIndex)
Next
Catch ex As Exception
Throw ex
End Try
End Sub
#End Region
#Region "-- 列印报表 --"
'*********************************************************************************************************************
'Author : SunnyZhao DateTime: 2005/08/04
'Function Name: imgPrint_ServerClick
'Input :
'Output : 打印并修改数据库打印信息
'Function Desc: 此动作要按照一下步骤来操作:
' 1. 判断用户有没有打印的权限,是否可以让用户打印。如果不让,提示信息;
' 2. 如果用户可以打印,那么修改数据库,添加用户打印信息;
'*********************************************************************************************************************
Private Sub imgPrint_ServerClick()Sub imgPrint_ServerClick(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgPrint.ServerClick
Dim conn As SqlConnection
Dim tran As SqlTransaction
conn = Me.myDBOx.Connection(DatabaseOperation.ServerConnection.conn_RCS)
conn.Open()
tran = conn.BeginTransaction
Try
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'1. 得到用户信息
Dim UserID As String = UCase(Right(Trim(Request.ServerVariables("LOGON_USER")), 5))
'UserID = "abc"
If UserID = "" Then
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('无法获取当前用户信息,禁止打印!');"",1000);</script>")
Exit Sub
End If
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'2. 根据不同的打印请求向数据库中添加打印纪录
Dim strPrintType As String = Page.Request.QueryString("from")
'在下面就要根据不同的报表请求,实例化不同的水晶报表类即可
Select Case strPrintType
Case "PrintReleaseCard"
Me.myDataTable = Me.SelectData()
Dim RNs(100) As String
Dim j As Integer = 0
RNs(j) = CStr(Me.myDataTable.Rows(0)("Release_No"))
Dim i As Integer
For i = 1 To Me.myDataTable.Rows.Count - 1
If CStr(Me.myDataTable.Rows(i)("Release_No")) <> RNs(j) Then
j += 1
RNs(j) = CStr(Me.myDataTable.Rows(i)("Release_No"))
End If
Next
i = 0
While RNs(i) <> ""
Dim p_ReleaseNO As New SqlParameter("@Release_No", RNs(i))
Dim p_UserID As New SqlParameter("@User_Id", UserID)
'新增用户打印信息
Me.myDBOx.ExecuteSQL_WithTransaction(conn, tran, "RCS_InsertPrintInfo", DatabaseOperation.SQLType.SQLStoredProcedure, _
p_ReleaseNO, p_UserID)
i += 1
End While
Case Else
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('无此报表!');"",300);</script>")
Exit Sub
End Select
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'3. 将所有 待打印报表 总结出来,并注册打印脚本到客户端
Call Me.GenerateAllPrintedReprot()
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""printReport()"",1000);</script>")
'执行完毕,没有错误提交事务
tran.Commit()
Catch ex As Exception
'有错误发生,回滚事务
tran.Rollback()
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('" + ex.Message + "');"",300);</script>")
Finally
'最后关闭数据库连接
conn.Close()
End Try
End Sub
#End Region
End Class
'Author : SunnyZhao
'Vendor : Facet - 全领(上海)信息科技
'DateTime : 2005/08/02
'From Page : PrintReleaseCard.aspx
'Page Desc : 根据从页面 PrintReleaseCard.aspx 传过来的参数查询并显示报表,并提供报表打印之功能
'额外说明 : 当用户点击了 [打印] 按钮之后,将执行下列步骤
' 1. 判断用户有没有打印的权限,是否可以让用户打印。如果不让,提示信息;
' 2. 如果用户可以打印,那么修改数据库,添加用户打印信息;
'*********************************************************************************************************************
Imports System.Data.SqlClient
Imports CrystalDecisions.Web
Public Class PrintFormClass PrintForm
Inherits System.Web.UI.Page
Private myDBOx As New RC.DatabaseOperation
Private CryRptViewer As New CrystalReportViewer
Private intPageCount As Integer
Private myDataTable As DataTable
Private myReport '水晶报表的实例,暂时还无法声明,要根据后面的传入参数判断是哪张报表
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
End Sub
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Protected WithEvents pnlShowPart As System.Web.UI.WebControls.Panel
Protected WithEvents pnlPrintPart As System.Web.UI.WebControls.Panel
Protected WithEvents imgPrint As System.Web.UI.HtmlControls.HtmlInputImage
Private Sub Page_Init()Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
#Region "-- Page_Load --"
'Page_Load动作中,只将供用户查看的报表总结出来,待打印报表暂时还不需要总结出来,以增加系统速度
Private Sub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
'第一次 Load 页面,先清空 Session 的值
If Not Page.IsPostBack Then
Session("ReportPrint_Data") = Nothing
End If
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'明确定义一个水晶报表的实例,用来显示给用户查看以及操作
'由于本页面是打印通用页面,根据不同的传入参数,可以打印不同的报表
If Page.Request.QueryString("from") Is Nothing Then
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('无法验证打印请求!');"",300);</script>")
Exit Sub
End If
Dim strPrintType As String = Page.Request.QueryString("from")
'在下面就要根据不同的报表请求,实例化不同的水晶报表类即可
Select Case strPrintType
Case "PrintReleaseCard"
myReport = New ReleaseCardReport
Case Else
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('无此报表!');"",300);</script>")
Exit Sub
End Select
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'根据传入参数查询数据
Me.myDataTable = Me.SelectData()
If Me.myDataTable Is Nothing OrElse Me.myDataTable.Rows.Count = 0 Then
Exit Sub
End If
myReport.SetDataSource(myDataTable) '设置数据源
CryRptViewer.ID = "cryViewerShow"
CryRptViewer.DisplayGroupTree = False
'CryRptViewer.DisplayToolbar = False
CryRptViewer.ReportSource = myReport
'将水晶报表的实例放置到页面的适当位置
Me.pnlShowPart.Controls.Add(CryRptViewer)
'正常情况下,不显示 pnlPrintPart
Me.pnlPrintPart.CssClass = "NotDisplay"
'第一次 Load 页面的时候
If Not Page.IsPostBack Then
'给出 第几页/共几页 的值
Me.intPageCount = myDataTable.Rows.Count / 8
'Me.txtPageNO.Text = "1/" + CStr(Me.intPageCount)
End If
Catch ex As Exception
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('" + ex.Message + "');"",300);</script>")
End Try
End Sub
#End Region
#Region "-- 根据传入参数查询数据 --"
'*********************************************************************************************************************
'Author : SunnyZhao DateTime: 2005/08/02
'Function Name: SelectData
'Input : 从页面 PrintReleaseCard.aspx 传过来的参数
'Output : 放置查询结果的 DataTable
'Function Desc: 根据传入参数,判断当前要操作的是 [什么报表] ,查询相应的数据
'*********************************************************************************************************************
Private Function SelectData()Function SelectData() As DataTable
Dim strPrintType As String
Dim tbTemp As DataTable
Try
'由于本页面是打印通用页面,根据不同的传入参数,可以打印不同的报表
strPrintType = Page.Request.QueryString("from")
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'打印不同的报表,添加不同的 Case 选项,查找不同的需要数据源即可
'根据不同的打印请求,查询相应数据
Select Case strPrintType
Case "PrintReleaseCard"
If Page.Request.QueryString("beginRN") Is Nothing Or Page.Request.QueryString("endRN") Is Nothing Or Page.Request.QueryString("printDateType") Is Nothing Then
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('传入参数不正确!');"",300);</script>")
Return Nothing
End If
Dim strbeginRN As String = CStr(Page.Request.QueryString("beginRN"))
Dim strendRN As String = CStr(Page.Request.QueryString("endRN"))
Dim strDateType As String = CStr(Page.Request.QueryString("printDateType"))
Dim beginReleaseNo As New SqlParameter("@beginRN", strbeginRN)
Dim endReleaseNo As New SqlParameter("@endRN", strendRN)
Dim printDate As New SqlParameter("@printDate", strDateType)
'为了避免重复操作数据库,我们将查询出来的数据暂时保存在一个 Session 里面
'如果 Session 已经过期,那么就需要重新的对数据库进行操作
If Session("ReportPrint_Data") Is Nothing Then
tbTemp = myDBOx.GetDataAsDataTable(DatabaseOperation.ServerConnection.conn_RCS, "RCS_PrintReleaseCard", DatabaseOperation.SQLType.SQLStoredProcedure, beginReleaseNo, endReleaseNo, printDate)
Session("ReportPrint_Data") = tbTemp
Else
tbTemp = CType(Session("ReportPrint_Data"), DataTable)
End If
Case Else
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('无此报表!');"",300);</script>")
Return Nothing
End Select
'判断有没有查询到数据
If tbTemp.Rows.Count = 0 Then
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('查无相关数据!');"",300);</script>")
Return Nothing
End If
'返回需要的结果
Return tbTemp
Catch ex As Exception
Throw ex
End Try
End Function
#End Region
#Region "-- 总结所有有待打印的报表到指定的 Panel中 --"
'*********************************************************************************************************************
'Author : SunnyZhao DateTime: 2005/08/02
'Function Name: GenerateAllPrintedReprot
'Input : 已经查询好的数据 myDataTable
'Output : 放置报表到页面
'Function Desc: 根据数据内容,总结所有的报表内容到页面,但不显示
'*********************************************************************************************************************
Private Sub GenerateAllPrintedReprot()Sub GenerateAllPrintedReprot()
Dim intCurrIndex As Integer
'Dim pnlReportLayer As Panel
'定义两个 Table,用来对报表的排版做间隔之用
Dim tbSeparateHeader As Table
Dim tbSeparateFooter As Table
Dim cryPrintRpt
Dim cryPrintViewer As CrystalReportViewer
Try
Me.myDataTable = Me.SelectData()
Me.intPageCount = myDataTable.Rows.Count / 8
'逐个操作所有的报表页面
For intCurrIndex = 1 To Me.intPageCount
'实例化
'pnlReportLayer = New Panel
Dim strPrintType As String = Page.Request.QueryString("from")
'在下面就要根据不同的报表请求,实例化不同的水晶报表类即可
Select Case strPrintType
Case "PrintReleaseCard"
cryPrintRpt = New ReleaseCardReport
Case Else
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('无此报表!');"",300);</script>")
Exit Sub
End Select
cryPrintViewer = New CrystalReportViewer
tbSeparateHeader = New Table
tbSeparateFooter = New Table
'因为报表太靠上,在它前面多加一个 Table,增加一些间距,并为了控制分页打印的正确性,在下面页多加一个 Table
tbSeparateHeader.CssClass = "tbSeparateHeaderHeight" '主要设置它的高度
tbSeparateHeader.ID = "tbSeparateHeader" + CStr(intCurrIndex)
tbSeparateFooter.CssClass = "tbSeparateFooterHeight" '主要设置它的高度
tbSeparateFooter.ID = "tbSeparateFooter" + CStr(intCurrIndex)
Dim tr As TableRow
Dim td As TableCell
td = New TableCell
td.Text = " "
tr = New TableRow
tr.Cells.Add(td)
tbSeparateHeader.Rows.Add(tr)
td = New TableCell
td.Text = " "
tr = New TableRow
tr.Cells.Add(td)
tbSeparateFooter.Rows.Add(tr)
'设置数据源
cryPrintRpt.SetDataSource(myDataTable)
cryPrintViewer.ID = "cryPrintViewer" + CStr(intCurrIndex)
cryPrintViewer.DisplayGroupTree = False
cryPrintViewer.DisplayToolbar = False
cryPrintViewer.ReportSource = cryPrintRpt
'将水晶报表的实例放置到页面的适当位置
'pnlReportLayer.ID = "pnlReportLayer" + CStr(intCurrIndex)
'pnlReportLayer.CssClass = "reportHeight"
'pnlReportLayer.Controls.Add(tbSeparate)
'pnlReportLayer.Controls.Add(cryPrintViewer)
Me.pnlPrintPart.Controls.Add(tbSeparateHeader)
Me.pnlPrintPart.Controls.Add(cryPrintViewer)
Me.pnlPrintPart.Controls.Add(tbSeparateFooter)
'设置当前报表应该显示的页码
cryPrintViewer.ShowNthPage(intCurrIndex)
Next
Catch ex As Exception
Throw ex
End Try
End Sub
#End Region
#Region "-- 列印报表 --"
'*********************************************************************************************************************
'Author : SunnyZhao DateTime: 2005/08/04
'Function Name: imgPrint_ServerClick
'Input :
'Output : 打印并修改数据库打印信息
'Function Desc: 此动作要按照一下步骤来操作:
' 1. 判断用户有没有打印的权限,是否可以让用户打印。如果不让,提示信息;
' 2. 如果用户可以打印,那么修改数据库,添加用户打印信息;
'*********************************************************************************************************************
Private Sub imgPrint_ServerClick()Sub imgPrint_ServerClick(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgPrint.ServerClick
Dim conn As SqlConnection
Dim tran As SqlTransaction
conn = Me.myDBOx.Connection(DatabaseOperation.ServerConnection.conn_RCS)
conn.Open()
tran = conn.BeginTransaction
Try
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'1. 得到用户信息
Dim UserID As String = UCase(Right(Trim(Request.ServerVariables("LOGON_USER")), 5))
'UserID = "abc"
If UserID = "" Then
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('无法获取当前用户信息,禁止打印!');"",1000);</script>")
Exit Sub
End If
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'2. 根据不同的打印请求向数据库中添加打印纪录
Dim strPrintType As String = Page.Request.QueryString("from")
'在下面就要根据不同的报表请求,实例化不同的水晶报表类即可
Select Case strPrintType
Case "PrintReleaseCard"
Me.myDataTable = Me.SelectData()
Dim RNs(100) As String
Dim j As Integer = 0
RNs(j) = CStr(Me.myDataTable.Rows(0)("Release_No"))
Dim i As Integer
For i = 1 To Me.myDataTable.Rows.Count - 1
If CStr(Me.myDataTable.Rows(i)("Release_No")) <> RNs(j) Then
j += 1
RNs(j) = CStr(Me.myDataTable.Rows(i)("Release_No"))
End If
Next
i = 0
While RNs(i) <> ""
Dim p_ReleaseNO As New SqlParameter("@Release_No", RNs(i))
Dim p_UserID As New SqlParameter("@User_Id", UserID)
'新增用户打印信息
Me.myDBOx.ExecuteSQL_WithTransaction(conn, tran, "RCS_InsertPrintInfo", DatabaseOperation.SQLType.SQLStoredProcedure, _
p_ReleaseNO, p_UserID)
i += 1
End While
Case Else
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('无此报表!');"",300);</script>")
Exit Sub
End Select
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'3. 将所有 待打印报表 总结出来,并注册打印脚本到客户端
Call Me.GenerateAllPrintedReprot()
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""printReport()"",1000);</script>")
'执行完毕,没有错误提交事务
tran.Commit()
Catch ex As Exception
'有错误发生,回滚事务
tran.Rollback()
Page.RegisterStartupScript("body", "<script language=javascript>setTimeout(""alert('" + ex.Message + "');"",300);</script>")
Finally
'最后关闭数据库连接
conn.Close()
End Try
End Sub
#End Region
End Class