当前位置:主页 > 软件编程 > .NET代码 >

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

时间:2021-03-02 11:44:33 | 栏目:.NET代码 | 点击:

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方法名?Q?S便起,也可以在一????Y面定?x多??方法(不同???源)

2:?O??蟊?

?蟊碓O??@?e就不涉及了

3:把第一步新建的???源加到?蟊硌Y面?定

 注意:?@?e需要先引用 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??蟊硌Y面的???源名?Q
            viewer.LocalReport.DataSources.Add(rds_1);

            ReportParameter rp1 = new ReportParameter("???1","???1的值" );//?o??蒂x值
            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);

                //*?O置?蟊???,并?@示
                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的值" );//?o??蒂x值
                  ReportParameter rp2 = new ReportParameter("???2","???2的值" );

                this.rpResut.LocalReport.SetParameters(new ReportParameter[] {rp1, rp2 });
                this.rpResut.DataBind();
                this.rpResut.LocalReport.Refresh();

至此,?蟊淼漠a出和?@示都OK了,如果需要更深入的了解,?查看其它文章

您可能感兴趣的文章:

相关文章