当前位置:网站首页>动态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;
效果如图
边栏推荐
- 在 ASP.NET Core 中上传文件
- 什么是控制板定制开发?
- uniapp电影购票选座系统源码
- 50道Redis面试题,来看看你会多少?
- Apache Doris Community PMC Yang Zhengguo: How do open source projects strike a balance between their own and the community's needs?
- The most complete architect knowledge map in history
- 那些关于DOM的常见Hook封装(二)
- BSN季度版本2022年8月31日迭代更新预告
- How to adjust futures account opening process and handling fee
- 单片机的优点和单片机开发的流程
猜你喜欢
手写flexible.js的原理实现,我终于明白移动端多端适配
低代码平台和专业开发人员——完美搭档?
基于CAP组件实现补偿事务与幂等性保障
史上最全架构师知识图谱
微服务:事务管理
.NET 6 study notes (4) - Solve the Nullable warning in VS2022
The senior told me that the MySQL of the big factory is connected through SSH
AlphaControls 控件 TsPanel TsGroupBox 块与组的结合
BSN季度版本2022年8月31日迭代更新预告
What platform is EPIC?
随机推荐
Metasploit——辅助模块(Auxiliary)
国际土壤模型协会 International Soil Modeling Consortium-ISMC
微软 .NET Core 3.1 年底将结束支持,请升级到.NET 6
【机器学习】回归树生成过程及举例理解
记一次 .NET 某工控自动化控制系统 卡死分析
Experience far more than Hue, this is the favorite SQL tool for technicians
How tall is the B+ tree of the MySQL index?
原油等特殊期货开户要求和豁免
如何仿造一个websocket请求?
The most complete architect knowledge map in history
JVM:(八)运行时数据区之方法区
华为云全流程护航《流浪方舟》破竹首发,打造口碑爆款
十七、一起学习Lua 错误处理
如何通过 open-local 玩转容器本地存储? | 龙蜥技术
C#介绍及基本数据类型
自动生成设备节点
在 C# 中如何检查参数是否为 null
Redis的那些事:一文入门Redis的基础操作
测试开发是什么,为什么现在这么吃香?
从事软件测试一年,只会基础的功能测试,怎么进一步学习?