首页 > 其他 > 详细

动态创建报表XRChart

时间:2021-09-06 03:58:05      阅读:18      评论:0      收藏:0      [点我收藏+]

技术分享图片

技术分享图片

技术分享图片

重点是不指定Chart的DataSource与Series1的datasource

动态加载报表

技术分享图片
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;
using System.Reflection;
using System.IO;
using System.Collections;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Assembly LoadAssembly()
        {
            var path=Path.Combine( AppDomain.CurrentDomain.BaseDirectory ,@"Plugs\ClassLibrary1.dll");

            return Assembly.LoadFrom(path);
             
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            var assembly = LoadAssembly();
            var rpt = assembly.CreateInstance("ClassLibrary1.XtraReport2") as XtraReport;
            var parent = assembly.CreateInstance("ClassLibrary1.PRIem");
            var itemType= assembly.GetType("ClassLibrary1.RItem");
            var ptyName = itemType.GetProperty("Name");
            var ptyNum = itemType.GetProperty("Num");
            var listType= typeof(List<>).MakeGenericType(itemType);
            var list= Activator.CreateInstance(listType) as IList;
            var rnd = new Random(Environment.TickCount);
            for (int i = 0; i < 4; i++)
            {
                var item = Activator.CreateInstance(itemType);
                ptyName.SetValue(item, "名称" + i,null);
                ptyNum.SetValue(item, rnd.Next(0, 100),null);
                list.Add(item);
            }
            var ptyList= parent.GetType().GetProperty("RItemList");
            ptyList.SetValue(parent, list, null);
            Console.WriteLine("it");
            #region 静态方式
            //var list = list
            //var rnd = new Random(Environment.TickCount);
            //for (int i = 0; i < 5; i++)
            //{
            //    var it = new RItem() { Name = "名称" + i, Num = rnd.Next(0, 100) };
            //    list.Add(it);
            //}
            //var pList = new List<PRIem>();
            //pList.Add(new PRIem() { RItemList = list, Name = "parent" });
            #endregion

            rpt.AllControls<XRTableCell>().ToList().ForEach(cell =>
            {
                cell.Font = new System.Drawing.Font("宋体", 10.0F);

            });
            rpt.AllControls<XRLabel>().ToList().ForEach(lbl =>
            {
                if (lbl.Name == "xrLabel1") return;
                lbl.Font = new System.Drawing.Font("宋体", 10.0F);
            });
            if (list.Count <= 0) throw new Exception("未提供有效列表!");

            rpt.DataSource = new List<Object>(){parent};
            rpt.RequestParameters = false;
            rpt.PrintingSystem.ShowMarginsWarning = false;
            rpt.PrintingSystem.ShowPrintStatusDialog = false;
            ReportPrintTool printTool = new ReportPrintTool(rpt);
            printTool.Report.CreateDocument(false);
            printTool.PrintingSystem.ShowMarginsWarning = false;
            Application.DoEvents();
            var showDialog = true;
            if (showDialog)
            {
                printTool.ShowPreviewDialog();
            }
            else
            {
                printTool.Print();
            }

        }
    }
}
View Code

 

动态创建报表XRChart

原文:https://www.cnblogs.com/wdfrog/p/15226754.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!