首页 > 编程 > .NET > 正文

Asp.net 自带报表的使用详解

2020-01-17 23:44:08
字体:
来源:转载
供稿:网友

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

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表