• 动态RDLC报表(七)


    Windows Forms 中显示报表

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

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Drawing.Drawing2D;
    7. using System.Linq;
    8. using System.Text;
    9. using System.Threading.Tasks;
    10. using System.Windows.Forms;
    11. namespace RDLC
    12. {
    13. public partial class Form1 : Form
    14. {
    15. public Form1()
    16. {
    17. InitializeComponent();
    18. this.Load += new System.EventHandler(this.Report_Load);
    19. DynamicBindRV();
    20. }
    21. private void Report_Load(object sender, EventArgs e)
    22. {
    23. this.reportViewer1.RefreshReport();
    24. }
    25. }
    26. }

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

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

    添加二维表头

    1. dynamicReport.HearderTop = CreateHearderTop();
    2. dynamicReport.HearderMerge = CreatehearderMerge();
    3. private DataTable CreateHearderTop()
    4. {
    5. DataTable hearderTop = new DataTable();
    6. hearderTop.Columns.Add("CellNumber", Type.GetType("System.Int32"));
    7. hearderTop.Columns.Add("CellValue", Type.GetType("System.String"));
    8. DataRow heardRow = hearderTop.NewRow();
    9. heardRow["CellNumber"] = 4;
    10. heardRow["CellValue"] = "中国大学";
    11. hearderTop.Rows.Add(heardRow);
    12. return hearderTop;
    13. }
    14. private DataTable CreatehearderMerge()
    15. {
    16. DataTable hearderMerge = new DataTable();
    17. hearderMerge.Columns.Add("CellNumber", Type.GetType("System.Int32"));
    18. hearderMerge.Columns.Add("CellValue", Type.GetType("System.String"));
    19. DataRow heardRow = hearderMerge.NewRow();
    20. heardRow["CellNumber"] = 1;
    21. heardRow["CellValue"] = "";
    22. hearderMerge.Rows.Add(heardRow);
    23. heardRow = hearderMerge.NewRow();
    24. heardRow["CellNumber"] = 2;
    25. heardRow["CellValue"] = "所属院系";
    26. hearderMerge.Rows.Add(heardRow);
    27. heardRow = hearderMerge.NewRow();
    28. heardRow["CellNumber"] = 1;
    29. heardRow["CellValue"] = "";
    30. hearderMerge.Rows.Add(heardRow);
    31. return hearderMerge;
    32. }

    设置列样式

    1. List<ReportColoumStyle> reportColoumStyles = new List<ReportColoumStyle>();
    2. foreach (DataColumn dc in dt.Columns)
    3. {
    4. if (dc.ColumnName == "专业名称")
    5. {
    6. reportColoumStyles.Add(new ReportColoumStyle() { ColoumName = dc.ColumnName, ColoumWidth = 4F });
    7. }
    8. else if (dc.ColumnName == "毕业时间")
    9. {
    10. reportColoumStyles.Add(new ReportColoumStyle() { ColoumName = dc.ColumnName, ColoumWidth = 3.5F, TextAlign = TextAlign.Right, ConsoleColor = ConsoleColor.Red });
    11. }
    12. else
    13. {
    14. reportColoumStyles.Add(new ReportColoumStyle() { ColoumName = dc.ColumnName, ColoumWidth = 5F, TextAlign = TextAlign.Center });
    15. }
    16. }
    17. foreach(DataColumn dc in CreateLabel(dynamicReport.FontString).Columns)
    18. {
    19. if (dc.ColumnName == "LeftName")
    20. {
    21. reportColoumStyles.Add(new ReportColoumStyle() { ColoumName = dc.ColumnName, ColoumWidth = 3.5F, TextAlign = TextAlign.Right, ConsoleColor = ConsoleColor.Red });
    22. }
    23. else
    24. {
    25. reportColoumStyles.Add(new ReportColoumStyle() { ColoumName = dc.ColumnName, ColoumWidth = 5F, TextAlign = TextAlign.Center });
    26. }
    27. }
    28. dynamicReport.SetColoumStyle(reportColoumStyles);

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

    1. DataTable dt = CreateData();
    2. dynamicReport.AddData(dt, "毕业时间");
    3. private DataTable CreateData()
    4. {
    5. DataTable dt = new DataTable();
    6. dt.Columns.Add("学院名称", Type.GetType("System.String"));
    7. dt.Columns.Add("专业名称", Type.GetType("System.String"));
    8. dt.Columns.Add("所在班级", Type.GetType("System.String"));
    9. dt.Columns.Add("毕业时间", Type.GetType("System.String"));
    10. for (int i = 0; i < 10; i++)
    11. {
    12. DataRow dr = dt.NewRow();
    13. dr[0] = "学院" + i;
    14. dr[1] = "专业" + i;
    15. dr[2] = "班级" + i;
    16. dr[3] = 2020 + i + ".0" + i;
    17. dt.Rows.Add(dr);
    18. }
    19. return dt;
    20. }

    加入Logo

    1. dynamicReport.AddLogo(CreateLogo(), true);
    2. private DataTable CreateLogo()
    3. {
    4. DataTable dt = new DataTable();
    5. dt.Columns.Add("OutletsLogo", Type.GetType("System.Byte[]"));
    6. dt.Columns.Add("OutletsName", Type.GetType("System.String"));
    7. dt.Columns.Add("OutletsAddress", Type.GetType("System.String"));
    8. dt.Columns.Add("OutletsTel", Type.GetType("System.String"));
    9. dt.Columns.Add("OutletsFax", Type.GetType("System.String"));
    10. dt.Columns.Add("OutletsEmail", Type.GetType("System.String"));
    11. dt.Columns.Add("OS", Type.GetType("System.Byte[]"));
    12. dt.Columns.Add("SignatureImg", Type.GetType("System.Byte[]"));
    13. dt.Columns.Add("EmployeeName", Type.GetType("System.String"));
    14. dt.Columns.Add("Reviewed", Type.GetType("System.String"));
    15. dt.Columns.Add("OutletsWeChat", Type.GetType("System.Byte[]"));
    16. DataRow dr = dt.NewRow();
    17. Image image = global::RDLC.Properties.Resources.JMHH;
    18. System.IO.MemoryStream ms = new System.IO.MemoryStream();
    19. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    20. byte[] bytes = ms.GetBuffer();
    21. ms.Close();
    22. dr[0] = bytes;
    23. dr[1] = "中国人民电脑服务中心";
    24. dr[2] = "地球村中国区人民大厦999层888号";
    25. dr[3] = "0000-88888888";
    26. dr[4] = "0000-99999999";
    27. dr[5] = "admin@china.com.cn";
    28. image = global::RDLC.Properties.Resources.OS;
    29. ms = new System.IO.MemoryStream();
    30. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    31. bytes = ms.GetBuffer();
    32. ms.Close();
    33. dr[6] = bytes;
    34. image = global::RDLC.Properties.Resources.SignatureImg;
    35. ms = new System.IO.MemoryStream();
    36. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    37. bytes = ms.GetBuffer();
    38. ms.Close();
    39. dr[7] = bytes;
    40. dr[8] = "Michael Xu";
    41. dr[9] = "true";
    42. image = global::RDLC.Properties.Resources.www;
    43. ms = new System.IO.MemoryStream();
    44. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    45. bytes = ms.GetBuffer();
    46. ms.Close();
    47. dr[10] = bytes;
    48. dt.Rows.Add(dr);
    49. return dt;
    50. }

    加入标签

    1. dynamicReport.AddLabel(CreateLabel(dynamicReport.FontString), Color.DarkBlue, true);
    2. private DataTable CreateLabel(string fontString)
    3. {
    4. DataTable dt = new DataTable();
    5. dt.Columns.Add("LeftName", Type.GetType("System.String"));
    6. dt.Columns.Add("LeftValue", Type.GetType("System.String"));
    7. dt.Columns.Add("RightName", Type.GetType("System.String"));
    8. dt.Columns.Add("RightValue", Type.GetType("System.String"));
    9. for (int i = 0; i < 3; i++)
    10. {
    11. DataRow dr = dt.NewRow();
    12. dr[0] = "LeftName名" + i;
    13. dr[1] = "LeftValue值" + i;
    14. dr[2] = "RightName名" + i;
    15. dr[3] = "RightValue值" + i;
    16. dt.Rows.Add(dr);
    17. }
    18. DataRow drPage = dt.NewRow();
    19. drPage[0] = "时间";
    20. drPage[1] = "2022-03-25";
    21. drPage[2] = "页码";
    22. drPage[3] = "=Globals!PageNumber / =Globals!TotalPages";
    23. dt.Rows.Add(drPage);
    24. return dt;
    25. }

    加入标题、备注和页脚

    1. dynamicReport.AddTitle("测试标题1", 12,"Bold", Color.DarkBlue, TextAlign.Center, 0.8F);
    2. dynamicReport.AddTitle("测试标题2", 9, "Normal", Color.DarkBlue, TextAlign.Right, 0.5F);
    3. 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);
    4. dynamicReport.AddPageFooter("测试页脚1", 9, "Normal", Color.DarkBlue, TextAlign.Left, 0.5F);
    5. dynamicReport.AddPageFooter("=Globals!PageNumberValue><Style><FontFamily>" + dynamicReport.FontString + "FontFamily><FontSize>9ptFontSize><Color>DarkBlueColor>Style>TextRun><TextRun><Value> / Value><Style><FontFamily>" + dynamicReport.FontString + "FontFamily><FontSize>9ptFontSize><Color>DarkBlueColor>Style>TextRun><TextRun><Value>=Globals!TotalPages", 9, "Normal", Color.DarkBlue, RDLC.TextAlign.Center, 0.8F);

    设置报表名称并显示出来 

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

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

    效果如图

    ​​​​​​​​​​​​​​

    动态RDLC报表(一)

    动态RDLC报表(二)

    动态RDLC报表(三)

    动态RDLC报表(四)

    动态RDLC报表(五)

    动态RDLC报表(六)

    动态RDLC报表(七)

    动态RDLC报表完整实例下载
     

  • 相关阅读:
    携程开源分布式配置系统Apollo服务端是如何实时更新配置的?
    MySQL数据库无法插入中文解决
    记一次 处理MIUI 13 疯狂杀进程问题
    【差旅游记】启程-新疆哈密(2)
    编译 gtsam
    【毕业设计】基于Vue与SSM的在线聊天系统
    银河麒麟高级服务器操作系统V10下载安装及安装docker
    web扫码登录
    损失函数loss和优化器optimizer
    运维工单系统有什么用?“的修报修平台”能为运维管理上带来什么帮助?
  • 原文地址:https://blog.csdn.net/xgh815/article/details/126247871