当前位置:网站首页>动态RDLC报表(七)
动态RDLC报表(七)
2022-08-09 16:51:00 【xgh815】
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<ReportColoumStyle> reportColoumStyles = new List<ReportColoumStyle>();
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</Value><Style><FontFamily>" + fontString + "</FontFamily><FontSize>9pt</FontSize><Color>DarkBlue</Color></Style></TextRun><TextRun><Value> / </Value><Style><FontFamily>" + fontString + "</FontFamily><FontSize>9pt</FontSize><Color>DarkBlue</Color></Style></TextRun><TextRun><Value>=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</Value><Style><FontFamily>" + dynamicReport.FontString + "</FontFamily><FontSize>9pt</FontSize><Color>DarkBlue</Color></Style></TextRun><TextRun><Value> / </Value><Style><FontFamily>" + dynamicReport.FontString + "</FontFamily><FontSize>9pt</FontSize><Color>DarkBlue</Color></Style></TextRun><TextRun><Value>=Globals!TotalPages", 9, "Normal", Color.DarkBlue, RDLC.TextAlign.Center, 0.8F);
设置报表名称并显示出来
reportViewer1.LocalReport.DisplayName = "测试";
dynamicReport.ShowReport();如果需要直接打印的话,设置 dynamicReport.OnlyPrint = true;
效果如图
边栏推荐
猜你喜欢

开篇-开启全新的.NET现代应用开发体验

如何通过 open-local 玩转容器本地存储? | 龙蜥技术

期货开户应该了解的行内知识

期货开户流程和手续费如何调整

uniapp电影购票选座系统源码

The senior told me that the MySQL of the big factory is connected through SSH

学长告诉我,大厂MySQL都是通过SSH连接的

How to choose a good SaaS knowledge base tool?

Can't install the Vmware virtual machine on the Ark Kai server?

What is control board custom development?
随机推荐
What you should know about futures account opening
怎样选择一个好的SaaS知识库工具?
WPF 实现柱形统计图
方舟开服务器Vmware虚拟机安装不上?
什么是硬件集成开发?硬件集成开发的核心有哪些?
FAST-LIO2代码解析(三)
The principle implementation of handwritten flexible.js, I finally understand the multi-terminal adaptation of the mobile terminal
AI基础环境搭建和设置总文
[ Kitex 源码解读 ] 请求重试
ABP详细教程——模块类
Prometheus full installation
学长告诉我,大厂MySQL都是通过SSH连接的
Problems Existing in Hardware Development of Electronic Products
Ark Standalone/Administrator Special Item Command Codes
An in-depth understanding of the implementation principle of Hybrid
谭中意:你知道 “开源女王” 是谁吗?
110+ public professional datasets summarized
[SUCTF 2019]CheckIn
.NET 6学习笔记(4)——解决VS2022中Nullable警告
Discuz!论坛程序安装+模板配置教程