水晶報表11在.net的制作
調用方式:
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Dim strUserID As String = "" '使用者代碼
Dim strRptCod As String = "" '報表代碼
Dim strRepTab As String = "" '列印資料存儲table名稱
Dim strSqlUpd As String = "" '在列印後欲UPDATE資料之Script
Dim strSqlDes As String = "" '產生列印資料存儲table之Script
Dim strSqlIns As String = "" '報表要列印資料先存入table之Script
Dim _Rpt00000 As Rpt00000 = New Rpt00000
'************此處選擇列印報表,並進行資料處理
strUserID = Session("USER_AID")
strRptCod = "AA500040-1.RPT"
strRepTab = "AA500040"
strSqlDes = GetCreTab_AA500040()
strSqlIns = GetIns_AA500040()
strSqlUpd = ""
'************開始列印資料處理動作
_Rpt00000.CreateRptTab(strUserID, strRepTab, strSqlDes, strSqlIns)
_Rpt00000.InsertInotData(strUserID, strRptCod, strRepTab, strSqlUpd)
'***********開始列印動作
Dim strScript As String = ""
strScript += "<script>FnOpenRpt('" + strUserID + "','" + strRptCod + "');</script>"
Page.RegisterStartupScript("strScript", strScript)
End Sub
'* 函數名稱: FnOpenRpt()
'* 目 的: 開啟報表窗體
function FnOpenRpt(UserID,RptCod)
{
var win_width= screen.availWidth-10; //取寬度
var win_height= screen.availHeight-80 ; //取高度
var left_pos =0; //左界
var top_pos =0; //右界
var strPass = "../Pub/RptCrvForm.aspx?RptCod=" + escape(RptCod);
strPass +="&UserID=" + escape(UserID);
var strP="width="+ win_width +",height="+ win_height + ",left=" +left_pos+",top="+top_pos;
strP+=",status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
window.open(strPass,'',strP);
return true;
}
''' 函數名稱: CreateRptTab()
''' 目 的: 在列印前處理列印資料存儲table,如果table存在,則刪除其中該USER之資料,如果Table不存在則創建table
Public Function CreateRptTab(ByVal UserID As String, ByVal RepTab As String, ByVal SqlDes As String, ByVal SqlIns As String)
Dim sbSQL As New System.Text.StringBuilder
Dim ReValue As Boolean = False
sbSQL.Append(" IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[" + RepTab + "]') AND OBJECTPROPERTY(id, N'IsUserTable')=1)" + Chr(13) + Chr(10))
sbSQL.Append(" DELETE FROM [" + RepTab + "] WHERE USRCOD='" + UserID + "' " + Chr(13) + Chr(10))
sbSQL.Append(" ELSE " + Chr(13) + Chr(10))
sbSQL.Append(SqlDes + Chr(13) + Chr(10))
Try
_objADODB.OpenConnection()
_objADODB.ExecSQL(sbSQL.ToString)
If SqlIns.Trim <> "" Then
_objADODB.ExecSQL(SqlIns)
End If
ReValue = True
Catch ex As Exception
Throw ex
Finally
_objADODB.CloseConnection()
End Try
Return ReValue
End Function
//直接顯示報表內容
Me.txtRptCod.Text = Request("RptCod")
Me.txtUserID.Text = Request("UserID")
Dim strRepTab As String = "AAWA02TB"
Dim objDT As DataTable
Dim oRptDoc As ReportDocument = New ReportDocument
Dim strSQL As String = ""
Dim path As String = ""
strRepTab = _Rpt00000.GetRepTabName(Me.txtUserID.Text, Me.txtRptCod.Text)
If strRepTab <> "" Then
strSQL = " SELECT * FROM " + strRepTab + " WHERE USRCOD='" + Me.txtUserID.Text.Trim + "'"
objDT = _objADODB.OpenDataTable(strSQL)
path = "..\RPT\" + Me.txtRptCod.Text
path = Server.MapPath(path)
Try
oRptDoc.Load(path)
Catch ex As Exception
Response.Write("伺服器端加載" + path + "報表檔失敗<BR>" + Chr(13) + Chr(10))
Response.Write("請檢查.rpt報表檔文檔路徑或文件內容是否正確")
Return
End Try
objDT.TableName = strRepTab
oRptDoc.SetDataSource(objDT)
Me.CryRptView.ReportSource = oRptDoc
Me.CryRptView.HasPrintButton = True
Else
Return
End If
//列印報表
Dim oRptDoc As New ReportDocument
Dim iPageS As Int32 '列印起始頁
Dim iPageE As Int32 '列印迄止頁
Dim iPageN As Int32 = 1 '列印份數
Dim path As String = ""
Dim strSQL As String = ""
Dim strRepTab As String = ""
Dim objDT As DataTable
iPageS = CType(Me.txtPageS.Text.Trim, Integer)
iPageE = CType(Me.txtPageE.Text.Trim, Integer)
iPageN = CType(Me.txtNum.Text.Trim, Integer)
strRepTab = _Rpt00000.GetRepTabName(Me.txtUserID.Text, Me.txtRptCod.Text)
Try
strSQL = " SELECT * FROM " + strRepTab + " WHERE USRCOD='" + Me.txtUserID.Text.Trim + "'"
objDT = _objADODB.OpenDataTable(strSQL)
path = "..\RPT\" + Me.txtRptCod.Text
oRptDoc.Load(Server.MapPath(path))
objDT.TableName = strRepTab
oRptDoc.SetDataSource(objDT)
'oRptDoc.PrintOptions.PrinterName '用預設印表機
oRptDoc.PrintToPrinter(iPageN, True, iPageS, iPageE)
Catch ex As LoadSaveReportException
ex.ToString()
Response.Write("開啓報表當時發生錯誤")
Catch ex As PrintException
ex.ToString()
Response.Write("列印時發生錯誤,請檢查印表機相關設定")
Catch ex As Exception
ex.ToString()
Response.Write("錯誤來源:" & ex.ToString)
End Try