资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Asp.net中怎么使用报表-创新互联

本篇文章为大家展示了Asp.net中怎么使用报表,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

成都创新互联专注于企业成都全网营销、网站重做改版、岳阳网站定制设计、自适应品牌网站建设、H5页面制作商城网站制作、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为岳阳等各大城市提供网站开发制作服务。

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:設計報表

Asp.net中怎么使用报表

報表設計這裡就不涉及了

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

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

Asp.net中怎么使用报表

把數據源導入后綁定即可

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();


上述内容就是Asp.net中怎么使用报表,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


新闻名称:Asp.net中怎么使用报表-创新互联
转载来源:http://cdkjz.cn/article/doshpg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220