如何使用Report Viewer在asp.net中创建报表

时间:2012-04-19 06:51:49

标签: c# asp.net reportviewer

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

2 个答案:

答案 0 :(得分:9)

答案 1 :(得分: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()函数创建Student Repository

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.在Index.aspx中,从ToolBox添加脚本管理器和报表查看器(拖放)

<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.Build And Run