赞助商
我们知道在VS 2003中提供了Crystal Report,这为.NET报表程序开发带来的质的提高,但是它是收费的。 而现在VS 2005和VS 2008为我们提供了RDLC报表,它是完全免费的RDLC报表。RDLC的全称是Report Definition Language Client-Side Processing, 它所强调的是在客户端的处理能力。今天我就拿在项目中开发的报表出来说一说。

  先来Show一下我的报表内容,我自己现在看看都感觉并不轻松,哈哈,不多说,Show起来。

  附件: r_Report1.GIF

      通过上面的代码大家可看出报表相对比较复杂,即使这样复杂的报表RDLC也能完成,可想而知他并不逊色与向Crystal Report这样的报表,并且是免费的,更可贵的是他还拥有功能强大的公式功能及子报表功能。


      下面的代码是如何设置提供数据及生成Report,至于如何设计Report我就不多说了,网络上已经有很多这方面的资料了。

参数提供代码

传递参数代码
  1. 1  private void GenerateReport(string isoOid)
  2. 2        {
  3. 3            List<ReportDataSource> reportDataSource = new List<ReportDataSource>();
  4. 4            DataSet ds = new DataSet();
  5. 5            DataSet ds1 = new DataSet();
  6. 6            string templatePath = string.Empty;
  7. 7            string totalRecords = string.Empty;
  8. 8
  9. 9            //Genereate by spool
  10. 10            ds = SpoolManager.GetSpoolByIsoOid(Session["CurrentOperateProject"].ToString(), isoOid);
  11. 11            ds1 = ISODrawingManager.GetISODrawingByIdPipeClass(long.Parse(isoOid));
  12. 12            reportDataSource.Add(new ReportDataSource("QC13QC15DataSet_T_Spool", ds.Tables[0]));
  13. 13            reportDataSource.Add(new ReportDataSource("QC13QC15DataSet_ISO_DRAWING", ds1.Tables[0]));
  14. 14
  15. 15            //TemplateFiles
  16. 16            templatePath = "../ReportsTemplate/ReportQC13QC15.rdlc";
  17. 17            Project p = ProjectManager.GetProjectById(long.Parse(Session["CurrentOperateProject"].ToString()));
  18. 18            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
  19. 19                totalRecords = "0";
  20. 20            else
  21. 21                totalRecords = ds.Tables[0].Rows.Count.ToString();
  22. 22            List<ReportParameter> parameterList = new List<ReportParameter>();
  23. 23            parameterList.Add(new ReportParameter("Report_ProjectName", p.ProjectName));
  24. 24            parameterList.Add(new ReportParameter("Report_ProjectOwner", p.Owner));
  25. 25            parameterList.Add(new ReportParameter("Report_ProjectNo", "2308"));
  26. 26            parameterList.Add(new ReportParameter("Report_SpoolCount", totalRecords));
  27. 27
  28. 28            ////Generate Report
  29. 29            GetReportMultipleDataSourceFile(reportDataSource, templatePath, parameterList, ConstantManager.REPORT_FORMAT_PDF_VALUE);
  30. 30        }
复制代码
reportDataSource.Add(new ReportDataSource("QC13QC15DataSet_T_Spool", ds.Tables[0]));
      reportDataSource.Add(new ReportDataSource("QC13QC15DataSet_ISO_DRAWING", ds1.Tables[0]));

      上面两条的作用是提供两个数据集给RDLC Report Template中,而在RDLC中我们经常将这种类型的数据集提供给Table对象。

生成Report代码

产生Reprot代码
  1. 1 /// <summary>
  2. 2        /// Generate the report by below parameters
  3. 3        /// </summary>
  4. 4        /// <param name="reportDateSource">data source</param>
  5. 5        /// <param name="TemplatePath">template report file path</param>
  6. 6        /// <param name="parameterList">all parameters</param>
  7. 7        /// <param name="FileType">file type</param>
  8. 8        public void GetReportMultipleDataSourceFile(List<ReportDataSource> reportDateSource, string TemplatePath, List<ReportParameter> parameterList, string FileType)
  9. 9        {
  10. 10            string reportFormat = FileType;
  11. 11            string outputfile = "Report.";
  12. 12            ReportViewer rview = new ReportViewer();
  13. 13            rview.ProcessingMode = ProcessingMode.Local;
  14. 14            rview.LocalReport.ReportPath = Server.MapPath(TemplatePath);
  15. 15            rview.LocalReport.DataSources.Clear();
  16. 16            foreach (ReportDataSource re in reportDateSource)
  17. 17            {
  18. 18                rview.LocalReport.DataSources.Add(re);
  19. 19            }
  20. 20
  21. 21            if (parameterList.Count > 0)
  22. 22                rview.LocalReport.SetParameters(parameterList);
  23. 23            string mimeType, encoding, extension, deviceInfo;
  24. 24            string[] streamids;
  25. 25            Microsoft.Reporting.WebForms.Warning[] warnings;
  26. 26            deviceInfo = "<DeviceInfo>" + "<SimplePageHeaders>True</SimplePageHeaders>" + "</DeviceInfo>";
  27. 27
  28. 28            byte[] bytes = rview.LocalReport.Render(reportFormat, deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);
  29. 29
  30. 30            if (warnings != null && warnings.Length > 0)
  31. 31            {
  32. 32                LoggingManager log = LoggingManager.GetLoggingManager();
  33. 33                foreach (Warning w in warnings)
  34. 34                {
  35. 35                    log.Info(w.Message);
  36. 36                }
  37. 37            }
  38. 38            HttpContext.Current.Response.Buffer = true;
  39. 39            HttpContext.Current.Response.Clear();
  40. 40            HttpContext.Current.Response.ContentType = mimeType;
  41. 41            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + outputfile + extension + ";");
  42. 42            HttpContext.Current.Response.BinaryWrite(bytes);
  43. 43            HttpContext.Current.Response.End();
  44. 44        }
复制代码
我们定义了两个泛型参数List<ReportDataSource> reportDataSource = new List<ReportDataSource>()和List<ReportParameter> parameterList = new List<ReportParameter>(),因为在一个Report中可能需要从多个数据集或参数中提取数据,而这两个泛型参数正是充当了这样的角色。ReportDataSource类是用来提供数据集的,ReportParameter用来传递参数的。 可以在上面代码GetReportMultipleDataSourceFile中找到其用法。

      在GetReportMultipleDataSourceFile方法的byte[] bytes = rview.LocalReport.Render(reportFormat, deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings)语句中,值得注意的是参数reportFormat,通过这个参数我们可以生成不同文件类型的报表,在我的项目中,使用了两种生成格式 --PDF及Excel两种,也就是说string FileType 这参数传递不同的类型格式,即可产生不同类型的报表。

      以上并非难懂代码,本人只是将自己项目中用到的写出来,以便大家参考,如有不合理之处还请指正。(文/我-大熊

RDLC文件下载:
赞助商
赞助商
TOP