1.在工具中找到这四个dll 并且引用到新的Demo中
(1).Stimulsoft.Base.dll
(2).Stimulsoft.Report.dll
(3).Stimulsoft.Report.Wpf.dll
(4).Stimulsoft.Data.dll
2.WPF 创建MainWindow窗口
1 <Window x:Class="WPFStimulsoftReports.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WPFStimulsoftReports" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded"> 9 <Grid> 10 <Grid.RowDefinitions> 11 <RowDefinition Height="50" /> 12 <RowDefinition Height="4*"/> 13 <RowDefinition Height="1*"/> 14 </Grid.RowDefinitions> 15 <Grid> 16 <Grid.ColumnDefinitions > 17 <ColumnDefinition Width="2*"/> 18 <ColumnDefinition Width="5*"/> 19 </Grid.ColumnDefinitions> 20 <Button Content="浏览" Width="130" Name="btn_Ll" Click="btn_Ll_Click"/> 21 </Grid> 22 <Grid Grid.Row="1"> 23 <StackPanel> 24 <ContentControl x:Name="MyGrid" Height="Auto" Width="Auto"/> 25 </StackPanel> 26 </Grid> 27 </Grid> 28 29 </Window>
MainWindow后台
1 private void btn_Ll_Click(object sender, RoutedEventArgs e) 2 { 3 4 string FileName = @"F:项目WPFDemoWPFStimulsoftReports" + "\Report.mrt"; 5 List<StudentModel> list = new List<StudentModel>() 6 { 7 new StudentModel(){Age = 19,Sex = "男",Class = "一班",Remark = "年龄不到20!"}, 8 new StudentModel(){Age = 17,Sex = "女",Class = "二班",Remark = "不及格!"}, 9 }; 10 DataSet ds = new DataSet(); 11 var table = aa.ToDataTable(list); 12 ds.Tables.Add(table); 13 14 15 16 StiReport report = new StiReport(); 17 report.RegData("CL", ds); 18 report.Load(FileName); 19 report.Compile(); 20 report.ShowWithWpf(); 21 22 } 23 public class StudentModel 24 { 25 public int Age { get; set; } 26 public string Sex { get; set; } 27 public string Class { get; set; } 28 public string Remark { get; set; } 29 30 } 31 public static DataTable ToDataTable<TResult>(this IEnumerable<TResult> value) where TResult : class 32 { 33 //创建属性的集合 34 //获得反射的入口 35 Type type = typeof(TResult); 36 DataTable dt = new DataTable(); 37 //把所有的public属性加入到集合 并添加DataTable的列 38 PropertyInfo[] propertis = type.GetProperties(); 39 foreach (PropertyInfo property in propertis) 40 { 41 Type colType = property.PropertyType; 42 if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>))) 43 { 44 colType = colType.GetGenericArguments()[0]; 45 } 46 dt.Columns.Add(property.Name, colType); 47 } 48 //Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p);dt.Columns.Add(p.Name, p.PropertyType); }); 49 foreach (var item in value) 50 { 51 //创建一个DataRow实例 52 DataRow row = dt.NewRow(); 53 //给row 赋值 54 foreach (PropertyInfo p in propertis) 55 { 56 row[p.Name] = p.GetValue(item, null); 57 } 58 //pList.ForEach(p => row[p.Name] = p.GetValue(item)); 59 //加入到DataTable 60 dt.Rows.Add(row); 61 } 62 return dt; 63 }
3.打开设计器 Designer.Wpf.exe
绑定数据源
设计界面布局
运行 WPFDemo 显示成功!