由于公司面监着倒闭,这两周都是闲着。前段时间独自一人为公司开发了eTeamWork系统,和OA差不多。自定义工作流,审批流转,消息等都有,只是表单,需要开发人员编写。该系统尚未做有报表。也是因为只做好财务帐单,和采购单的一部份(先做采购单,由于某种原因,停止转做财务帐单)。今天突然想把web报表技术搞定。
上网找了一下,找到的都觉得太烦琐了,要这样解决登录失败的问题。
如下:
using CrystalDecisions.Shared ;
using CrystalDecisions.CrystalReports .Engine ;
//命名空间的相关说明请查看VS的帮助文档
其次假如用户创建的rpt文件名为CrystalReport1.rpt,则实现如下:
private void Page_Load(object sender, System.EventArgs e)
{
TableLogOnInfo logOnInfo = new TableLogOnInfo ();
ReportDocument Rd = new ReportDocument();
Rd.Load ("C://Inetpub//wwwroot//NetPrintTest//CrystalReport1.rpt");
logOnInfo.ConnectionInfo.ServerName = localhost;
logOnInfo.ConnectionInfo.DatabaseName = northwind;
logOnInfo.ConnectionInfo.UserID =sa;
logOnInfo.ConnectionInfo.Password =sa;
Rd.Database .Tables [0].ApplyLogOnInfo (logOnInfo);
CrystalReportViewer1.ReportSource = Rd;
}
我以前一直是做C/S软件,三年前就写了一个通用的报表组件。使用时给组件传递报表文件路径、DataSet以及其它参数就可以了(如报表标题,打印者名称等),我心里在想,在web也应该能实现吧,用不着象上面那样把数据库连接暴露出来。
我带着自己的想法,到微软件网站去找了一下,终于找到了水晶报表的例子。http://msdn.microsoft.com/en-us/library/ms227408(VS.80).aspx。打开看web方面的报表。找到了可行的办法。在这发布出来跟大家共享。
Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Partial Class _Default
Inherits System.Web.UI.Page
'创建报表文档
Private objReportDocument As ReportDocument
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
'从数据库读取记录集
Dim cnn As New SqlConnection("workstation id=STKHF109;packet size=4096;user id=sa;data source=(local);persist security info=false;initial catalog=MSPetShop4")
Dim cmm As New SqlCommand
With cmm
.Connection = cnn
.CommandType = CommandType.Text
.CommandText = "select *from Product"
End With
Dim adp As New SqlDataAdapter
adp.SelectCommand = cmm
Dim dstNew As New DataSet
adp.Fill(dstNew, "Product")
objReportDocument = New ReportDocument
'加载报表文件
objReportDocument.Load(Server.MapPath("CrystalReport.rpt"))
'绑定记录集
objReportDocument.SetDataSource(dstNew)
'赋给报表预览控件显示
Me.CrystalReportViewer1.ReportSource = objReportDocument
End If
End Sub
End Class