遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

动态创建报表XRChart

重点是不指定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

 

posted on 2021-09-04 15:27  遗忘海岸  阅读(195)  评论(0编辑  收藏  举报