最近要做报表,今天开始看,发现网上给的例子都很散,自己拿来老是运行不是很正确。要不就是运行不出来。不过还是找到了不少好的资料。比如
http://v.youku.com/v_playlist/f6068270o1p17.html 一个演示视频
http://wenku.baidu.com/view/0d8e92e0524de518964b7d52.html 水晶报表快速入门
自己做的基本上都是参照着2个,不过也发现一些问题,就是读入时候要求输入数据库密码。后来通过查阅资料发现可以在代码中实现自动输入。
通过CrystalReportSource1.ReportDocument.SetDatabaseLogon("sa", "sa", @"192.168.1.111", "AutoLot");实现。
具体例子:
webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CrystalWebApp.WebForm1" %> <%@ Register assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True" Height="1123px" ReportSourceID="CrystalReportSource1" Width="897px" /> <CR:CrystalReportSource ID="CrystalReportSource1" runat="server"> <Report FileName="SubReport.rpt"> </Report> </CR:CrystalReportSource> </div> </form> </body> </html>
webform.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CrystalDecisions.Shared; using CrystalDecisions.CrystalReports.Engine; namespace CrystalWebApp { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { CrystalReportSource1.ReportDocument.SetDatabaseLogon("sa", "sa", @"192.168.1.111", "AutoLot"); } } }
运行结果