5

我正在使用 Visual Studio 2010。在那之前我曾使用过 Crystal Reporting,但现在我想使用 Report Viewer 生成报告。由于我是这个主题的新手,请指导我。谢谢 !!!

4

2 回答 2

1

我的代码用于为业务类对象创建报告...

使用 Business Class Objects & ReportViewer (ASP.NET/ C#) 创建报表 1.创建学生类

  public class StudentClass
  {
      public int No { get; set; }
      public string Name { get; set; }
      public string Degree { get; set; }
   }

2.使用GetStudents()函数创建学生资料库

public class StudentRepository : StudentClass
    {
        public List<StudentClass> studentList = new List<StudentClass>();

        public List<StudentClass> GetStudents()
        {            
            StudentClass student1 = new StudentClass();
            student1.No = 1;
            student1.Name = "Bhuvana";
            student1.Degree = "M.Tech";
            studentList.Add(student1);
            StudentClass student2 = new StudentClass();
            student2.No = 2;
            student2.Name = "Annie";
            student2.Degree = "B.Tech";
            studentList.Add(student2);
            StudentClass student3 = new StudentClass();
            student3.No = 3;
            student3.Name = "Muthu Abi";
            student3.Degree = "B.Tech";
            studentList.Add(student3);
            return studentList;
        }
    }

3.使用报表向导创建“StudentReport.rdlc”并选择DataSource

4.In Index.aspx 从工具箱中添加脚本管理器和报表查看器(拖放)

<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>  
    <rsweb:ReportViewer ID="ReportViewer1" runat="server">
    </rsweb:ReportViewer>       
</div>

5.修改代码隐藏文件中的Page_Load()方法

public partial class Index : System.Web.UI.Page
{
    StudentRepository sr = new StudentRepository();
    List<StudentClass> sc = new List<StudentClass>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ReportViewer1.ProcessingMode = ProcessingMode.Local;
            ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report/Student.rdlc");
            sc = sr.GetStudents();
            IEnumerable<StudentClass> ie;
            ie = sc.AsQueryable();
            ReportDataSource datasource = new ReportDataSource("DataSet1", ie);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(datasource);
        }

    }
}

6.构建并运行

于 2014-02-04T07:44:46.780 回答