水晶报表填充.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);
 
        }
    }
}

作者: 韩兆新
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类:  [06]ASP.NET相关
标签:  ASP.NET

本文转自韩兆新博客博客园博客,原文链接:http://www.cnblogs.com/hanzhaoxin/p/3684289.html,如需转载请自行联系原作者
目录
相关文章
|
5月前
|
存储
.NET Core - 自定义配置数据源:低成本实现定制化配置方案
.NET Core - 自定义配置数据源:低成本实现定制化配置方案
|
5月前
.NET Core-自定义配置数据源
前面,我们学习了配置框架的4种配置方式,那么你知道如何实现自定义的配置数据源吗?知道如何低成本实现定制化配置方案吗?下面我们就一起来学习一下吧。
一起谈.NET技术,改善代码设计 —— 优化物件之间的特性(Moving Features Between Objects)
  系列博客       1. 改善代码设计 —— 优化函数的构成(Composing Methods)       2. 改善代码设计 —— 优化物件之间的特性(Moving Features Between Objects)       3.
672 0

相关产品

  • 云迁移中心