水晶報表填充.Net Objects數據源


Crystal Reports(水晶報表)是一款商務智能(BI)軟件,主要用於設計及產生報表。是業內最專業、功能最強的報表系統。

查看網絡資料及課本圖書,鮮有介紹通過.NET Objects作為數據源填充水晶報表的示例。本文將通過兩個簡單的示例演示水晶報表填充.Net Objects數據源的過程。

示例一:打印學生信息(一個.NET Objects數據源的填充)

示例效果:

image

Student.cs

using System;
using System.Collections.Generic;
using System.Web;
 
namespace CrystalReportsDemo
{
    public class Student
    {
        public Student(string studentNo,string studentName,string sex,int age)
        {
            this.StudentNo = studentNo;
            this.StudentName = studentName;
            this.Sex = sex;
            this.Age = age;
        }
        public string StudentNo
        { set; get; }
        public string StudentName
        { set; get; }
        public string Sex
        { get; private set; }
        public int Age
        { get; private set; }
    }
}

將其編譯后,即可通過數據庫專家將其作為數據源導入到字段資源管理器。

image

繪制報表樣式並插入數據庫字段:

image

在Default.aspx頁面拖入CrystalReportViewer控件:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CrystalReportsDemo._Default" %>
 
<%@ Register assembly="CrystalDecisions.Web, Version=13.0.2000.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="crvReport" runat="server" AutoDataBind="true" />
    
    </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace CrystalReportsDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FillReport();
        }
        private void FillReport()
        {
            List<Student> studentList = new List<Student>();
            studentList.Add(new Student("200901001", "學生一", "男", 23));
            studentList.Add(new Student("200901002", "學生二", "男", 24));
            studentList.Add(new Student("200901003", "學生三", "女", 22));
            studentList.Add(new Student("200901004", "學生四", "男", 23));
            studentList.Add(new Student("200901005", "學生五", "女", 25));
            studentList.Add(new Student("200901006", "學生六", "男", 23));
            CrStudents studentsReport = new CrStudents();
            studentsReport.SetDataSource(studentList);
            crvReport.ReportSource = studentsReport;
        }
    }
}

示例二:打印學生成績(多個.NET Objects數據源的填充)

示例效果:

image

Subject.cs

using System;
using System.Collections.Generic;
using System.Web;
 
namespace CrystalReportsDemo
{
    public class Subject
    {
        public Subject(string subjectName,string subjectType,double result,string comment)
        {
            this.SubjectName = subjectName;
            this.SubjectType = subjectType;
            this.Result = result;
            this.Comment = comment;
        }
        public string SubjectName
        { set; get; }
        public string SubjectType
        { set; get; }
        public double Result
        { set; get; }
        public string Comment
        { set; get; }
    }
}

主報表:(CrSupReport.rpt)

image

子報表:(CrSubReport.rpt)

image

Default2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default2.aspx.cs" Inherits="CrystalReportsDemo.Default2" %>
 
<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.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="crvReport2" runat="server" AutoDataBind="true" />
    </div>
    </form>
</body>
</html>

Default2.aspx.cs

using System;
using System.Collections.Generic;
 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
 
namespace CrystalReportsDemo
{
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FillReport();
        }
        private void FillReport()
        {
            List<Student> studentList = new List<Student>();
            studentList.Add(new Student("200901001", "學生一", "男", 23));
            ReportDocument supReport = new CrSupReport();
            supReport.SetDataSource(studentList);
            crvReport2.ReportSource = supReport;
 
            List<Subject> subjectList = new List<Subject>();
            subjectList.Add(new Subject("語文","文科",90,"評語"));
            subjectList.Add(new Subject("數學", "理科", 90, "評語"));
            subjectList.Add(new Subject("工程學", "工科", 80, "評語"));
            subjectList.Add(new Subject("現代醫學", "醫學", 90, "評語"));
 
            ReportDocument subReport = supReport.Subreports["CrSubReport"];
            subReport.SetDataSource(subjectList);
 
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM