动态RDLC报表(七)

Windows Forms 中显示报表

新建一个Form并添加一个ReportViewer,在Load事件中RefreshReport。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RDLC
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Report_Load);
            DynamicBindRV();
        }


        private void Report_Load(object sender, EventArgs e)
        {
            this.reportViewer1.RefreshReport();
        }
    }
}

初始化动态RDLC报表类DynamicReport的实例,并设置相关参数,把ReportViewer传递给DynamicReport。

            //new一个报表类对象
            var dynamicReport = new DynamicReport();
            //使用字体,默认微软雅黑
            //dynamicReport.FontString = "宋体";
            //是否直接打印,默认false
            //dynamicReport.OnlyPrint = true;
            //是否显示签名签章,默认true
            //dynamicReport.IsSignature = false;
            //默认值A4,二等分纸241 x 140
            //dynamicReport.PageWidth = 24.1F;
            //dynamicReport.PageHeight = 14.0F;
            //边距,默认值1.5F、0.8F
            //dynamicReport.LeftMargin = 1.5F;
            //dynamicReport.TopMargin = 0.8F;
            dynamicReport.SetReport(this.reportViewer1);

添加二维表头

            dynamicReport.HearderTop = CreateHearderTop();
            dynamicReport.HearderMerge = CreatehearderMerge();


        private DataTable CreateHearderTop()
        {
            DataTable hearderTop = new DataTable();
            hearderTop.Columns.Add("CellNumber", Type.GetType("System.Int32"));
            hearderTop.Columns.Add("CellValue", Type.GetType("System.String"));
            DataRow heardRow = hearderTop.NewRow();
            heardRow["CellNumber"] = 4;
            heardRow["CellValue"] = "中国大学";
            hearderTop.Rows.Add(heardRow);
            return hearderTop;
        }

        private DataTable CreatehearderMerge()
        {
            DataTable hearderMerge = new DataTable();
            hearderMerge.Columns.Add("CellNumber", Type.GetType("System.Int32"));
            hearderMerge.Columns.Add("CellValue", Type.GetType("System.String"));
            DataRow heardRow = hearderMerge.NewRow();
            heardRow["CellNumber"] = 1;
            heardRow["CellValue"] = "";
            hearderMerge.Rows.Add(heardRow);
            heardRow = hearderMerge.NewRow();
            heardRow["CellNumber"] = 2;
            heardRow["CellValue"] = "所属院系";
            hearderMerge.Rows.Add(heardRow);
            heardRow = hearderMerge.NewRow();
            heardRow["CellNumber"] = 1;
            heardRow["CellValue"] = "";
            hearderMerge.Rows.Add(heardRow);
            return hearderMerge;
        }

设置列样式

            List reportColoumStyles = new List();
            foreach (DataColumn dc in dt.Columns)
            {
                if (dc.ColumnName == "专业名称")
                {
                    reportColoumStyles.Add(new ReportColoumStyle() { ColoumName = dc.ColumnName, ColoumWidth = 4F });
                }
                else if (dc.ColumnName == "毕业时间")
                {
                    reportColoumStyles.Add(new ReportColoumStyle() { ColoumName = dc.ColumnName, ColoumWidth = 3.5F, TextAlign = TextAlign.Right, ConsoleColor = ConsoleColor.Red });
                }
                else
                {
                    reportColoumStyles.Add(new ReportColoumStyle() { ColoumName = dc.ColumnName, ColoumWidth = 5F, TextAlign = TextAlign.Center });
                }
            }
            foreach(DataColumn dc in CreateLabel(dynamicReport.FontString).Columns)
            {
                if (dc.ColumnName == "LeftName")
                {
                    reportColoumStyles.Add(new ReportColoumStyle() { ColoumName = dc.ColumnName, ColoumWidth = 3.5F, TextAlign = TextAlign.Right, ConsoleColor = ConsoleColor.Red });
                }
                else
                {
                    reportColoumStyles.Add(new ReportColoumStyle() { ColoumName = dc.ColumnName, ColoumWidth = 5F, TextAlign = TextAlign.Center });
                }
            }
            dynamicReport.SetColoumStyle(reportColoumStyles);

不连接数据库直接创建一个数据表,并添加到DynamicReport中,设置“毕业时间”为统计列。

        DataTable dt = CreateData();
        dynamicReport.AddData(dt, "毕业时间");

        private DataTable CreateData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("学院名称", Type.GetType("System.String"));
            dt.Columns.Add("专业名称", Type.GetType("System.String"));
            dt.Columns.Add("所在班级", Type.GetType("System.String"));
            dt.Columns.Add("毕业时间", Type.GetType("System.String"));
            for (int i = 0; i < 10; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = "学院" + i;
                dr[1] = "专业" + i;
                dr[2] = "班级" + i;
                dr[3] = 2020 + i + ".0" + i;
                dt.Rows.Add(dr);
            }
            return dt;
        }

加入Logo

        dynamicReport.AddLogo(CreateLogo(), true);
        private DataTable CreateLogo()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("OutletsLogo", Type.GetType("System.Byte[]"));
            dt.Columns.Add("OutletsName", Type.GetType("System.String"));
            dt.Columns.Add("OutletsAddress", Type.GetType("System.String"));
            dt.Columns.Add("OutletsTel", Type.GetType("System.String"));
            dt.Columns.Add("OutletsFax", Type.GetType("System.String"));
            dt.Columns.Add("OutletsEmail", Type.GetType("System.String"));
            dt.Columns.Add("OS", Type.GetType("System.Byte[]"));
            dt.Columns.Add("SignatureImg", Type.GetType("System.Byte[]"));
            dt.Columns.Add("EmployeeName", Type.GetType("System.String"));
            dt.Columns.Add("Reviewed", Type.GetType("System.String"));
            dt.Columns.Add("OutletsWeChat", Type.GetType("System.Byte[]"));
            DataRow dr = dt.NewRow();
            Image image = global::RDLC.Properties.Resources.JMHH;
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] bytes = ms.GetBuffer();
            ms.Close();
            dr[0] = bytes;
            dr[1] = "中国人民电脑服务中心";
            dr[2] = "地球村中国区人民大厦999层888号";
            dr[3] = "0000-88888888";
            dr[4] = "0000-99999999";
            dr[5] = "[email protected]";
            image = global::RDLC.Properties.Resources.OS;
            ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            bytes = ms.GetBuffer();
            ms.Close();
            dr[6] = bytes;
            image = global::RDLC.Properties.Resources.SignatureImg;
            ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            bytes = ms.GetBuffer();
            ms.Close();
            dr[7] = bytes;
            dr[8] = "Michael Xu";
            dr[9] = "true";
            image = global::RDLC.Properties.Resources.www;
            ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            bytes = ms.GetBuffer();
            ms.Close();
            dr[10] = bytes;
            dt.Rows.Add(dr);
            return dt;
        }

加入标签

        dynamicReport.AddLabel(CreateLabel(dynamicReport.FontString), Color.DarkBlue, true);

        private DataTable CreateLabel(string fontString)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("LeftName", Type.GetType("System.String"));
            dt.Columns.Add("LeftValue", Type.GetType("System.String"));
            dt.Columns.Add("RightName", Type.GetType("System.String"));
            dt.Columns.Add("RightValue", Type.GetType("System.String"));
            for (int i = 0; i < 3; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = "LeftName名" + i;
                dr[1] = "LeftValue值" + i;
                dr[2] = "RightName名" + i;
                dr[3] = "RightValue值" + i;
                dt.Rows.Add(dr);
            }
            DataRow drPage = dt.NewRow();
            drPage[0] = "时间";
            drPage[1] = "2022-03-25";
            drPage[2] = "页码";
            drPage[3] = "=Globals!PageNumber / =Globals!TotalPages";
            dt.Rows.Add(drPage);
            return dt;
        }

加入标题、备注和页脚

            dynamicReport.AddTitle("测试标题1", 12,"Bold", Color.DarkBlue, TextAlign.Center, 0.8F);
            dynamicReport.AddTitle("测试标题2", 9, "Normal", Color.DarkBlue, TextAlign.Right, 0.5F);
            dynamicReport.AddRemarks("它能记录每笔交易的日期、内容和数额,还可以添加备注,以及显示收支状态。It has the date of each transaction, the title and amount, a little field for memos, and a running balance", 10, "Normal", Color.DarkBlue, TextAlign.Left, 0.6F);
            dynamicReport.AddPageFooter("测试页脚1", 9, "Normal", Color.DarkBlue, TextAlign.Left, 0.5F);
            dynamicReport.AddPageFooter("=Globals!PageNumber / =Globals!TotalPages", 9, "Normal", Color.DarkBlue, RDLC.TextAlign.Center, 0.8F);

设置报表名称并显示出来 

reportViewer1.LocalReport.DisplayName = "测试";
dynamicReport.ShowReport();

如果需要直接打印的话,设置 dynamicReport.OnlyPrint = true;

效果如图

动态RDLC报表(七)_第1张图片​​​​​​​​​​​​​​

动态RDLC报表(一)

动态RDLC报表(二)

动态RDLC报表(三)

动态RDLC报表(四)

动态RDLC报表(五)

动态RDLC报表(六)

动态RDLC报表(七)

动态RDLC报表完整实例下载
 

你可能感兴趣的:(数据库,大数据,C#,RDLC报表,ReportViewer)