步骤如下:
1.设计报表模板Report1.rpt,与数据源查询的结果集一致,如果数据源复杂,可先做一视图,用视图来设计报表和生成DataSet或DataTable.
2.在aspx页面上托入报表查看器CrystalReportViewer1和报表源控件CrystalReportSource1.
3.编码从表或视图中查询数据生成DataSet或DataTable.
4.用DataSet创建报表文件(ReportDoument),并绑定到报表查看器.
代码如下:
//存储过程名
string strSql = "SubPlanAndLast";
//连接字符串
string strCon = ConfigurationManager.ConnectionStrings["SqlCon"].ToString();
SqlConnection con = new SqlConnection(strCon);
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand(strSql, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@year", year);
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
CrystalReportSource1.ReportDocument.Load(Server.MapPath("Report1.rpt"));
//注意此处必需指明Dataset中的表的名称,否则会提示“您请求的报表需要更多信息.”
CrystalReportSource1.ReportDocument.SetDataSource(dt);
CrystalReportSource1.DataBind();
CrystalReportViewer1.ReportSource = CrystalReportSource1;
CrystalReportViewer1.DataBind();
CrystalReportViewer1.BestFitPage = true;
CrystalReportViewer1.Width = Unit.Pixel(400);