最近公司的功能需要使用报表,用的是微软自带的报表,谈一谈我们的做法,希望可以给想学习的人一些指导

1:新建報表所需的數據源DataSet.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data; namespace ********
{
public class DataSet
{
public DataTable CreatDataSet()
{
DataTable dt = new DataTable();
dt.Columns.Add("A");
dt.Columns.Add("B");
dt.Columns.Add("C");
return dt; }
}
}

指定所需要綁定的Table的列,返回dataTable 類,CreatDataSet方法名稱隨便起,也可以在一個類裏面定義多個方法(不同數據源)

2:設計報表

報表設計這裡就不涉及了

3:把第一步新建的數據源加到報表裏面綁定

注意:這裡需要先引用 Interop.VBA.dll 才可以把新建的CS文件作為數據源導入

把數據源導入后綁定即可

4:直接把報表導出為PDF,Excel等格式

            ReportViewer viewer = new ReportViewer();
viewer.ProcessingMode = ProcessingMode.Local;
viewer.LocalReport.ReportEmbeddedResource = "***.Page.Report.Report1.rdlc";
ReportDataSource rds_1 = new ReportDataSource("DataSet1", dtReport);//DataSet1為報表裏面的數據源名稱
viewer.LocalReport.DataSources.Add(rds_1); ReportParameter rp1 = new ReportParameter("參數1","參數1的值" );//給參數賦值
ReportParameter rp2 = new ReportParameter("參數2","參數2的值" );
viewer.LocalReport.SetParameters(new ReportParameter[] {rp1, rp2 }); Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty; byte[] bytes = viewer.LocalReport.Render("Excel", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
//Excel ,PDF ,Word 等格式
// Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=1_" + DateTime.Now.ToString("yyyyMMddhhssmm") + "" + "." + extension);
Response.BinaryWrite(bytes); // create the file
Response.Flush(); // send it to the client to download

5:在頁面引用報表(rpResult為報表控件)

                DataTable dt = new DataTable();//自己拼出數據源就可以
ReportDataSource repDataSource = new ReportDataSource("DataSet1", dt); //*設置報表參數,并顯示
this.rpResut.LocalReport.ReportEmbeddedResource = "***.Page.Report.Report1.rdlc"";
this.rpResut.LocalReport.DataSources.Clear();
this.rpResut.LocalReport.DataSources.Add(repDataSource);
ReportParameter rp1 = new ReportParameter("參數1","參數1的值" );//給參數賦值
ReportParameter rp2 = new ReportParameter("參數2","參數2的值" ); this.rpResut.LocalReport.SetParameters(new ReportParameter[] {rp1, rp2 });
this.rpResut.DataBind();
this.rpResut.LocalReport.Refresh();

6:子報表的使用

        void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
string MasterID = e.Parameters["MasterID"].Values[];
DataRow[] drs_Main = dtSubReport.Select("MasterID = '" + MasterID + "'");
DataTable dtReport = drs_Main.CopyToDataTable();
e.DataSources.Add(new ReportDataSource("DataSetMain", dtReport)); }

7:調用

ReportViewer viewer = new ReportViewer();
viewer.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);

至此,報表的產出和顯示都OK了,如果需要更深入的了解,請查看其它文章

最新文章

  1. 读深入了解c++内核对象模型小结(1/3/4)
  2. xmemcached的使用
  3. PCM(Pulse-code modulation)脉冲编码调制
  4. HSV色彩空间
  5. Python3基础 大于一个数的同时小于一个数
  6. 扒一扒各大电商网站的m站都用的什么前端技术输入日志标题
  7. PHP输出中文乱码的问题
  8. linux下的僵尸进程处理SIGCHLD信号
  9. oracle 备份操作流程
  10. php与mysql之间操作原理
  11. [HNOI 2002]跳蚤
  12. Andriod 安全之Windows下CTS自动化测试环境的搭建
  13. LNMP php缓存器下载
  14. SharePoint Server 2019新特性
  15. vmware虚拟机环境下配置centos为静态IP的步骤
  16. Xenserver之HA实现-NFS的实现
  17. Maven下载项目依赖jar包和使用方法
  18. vue里面使用Velocity.js
  19. 038改变状态栏的颜色(扩展知识:关于iOS不同版本的消息通知知识)
  20. hadoop之 reduce个数控制

热门文章

  1. laravel5.5用户认证
  2. runloop的mode作用是什么?
  3. 自动化测试学习之路--HTML常见元素、属性的简单学习
  4. Java基础-6流程控制
  5. UIAutomator2、Appium、Robotium搭建环境与框架对比
  6. markdown备忘
  7. 剑指offer-替换空格02
  8. JavaScript各种数据类型
  9. PHP可变变量的简单使用
  10. Python中的单元测试模块Unittest快速入门