WPF StimulSoft Reports WPF打印

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>
View Code

MainWindow後臺

 1    private void btn_Ll_Click(object sender, RoutedEventArgs e)
 2            {
 3 
 4             string FileName = @"F:\項目\WPFDemo\WPFStimulsoftReports" + "\\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         }
View Code

3.打開設計器 Designer.Wpf.exe

 

綁定數據源

 

 

 設計界面佈局

 

 運行 WPFDemo  顯示成功!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章