wpf 基于模板的报表设计及打印预览

  wpf 中可以使用reportingservers ,水晶报表等东西,但是如果使用reportingservers 在频繁的数据交互中是 比较浪费资源或者存在效率问题。所以还有没有更好的办法呢。答案肯定是有的。
wpf中基于模板的报表设计就可以完成以上内容,对于要修改的报表文件是以文件的方式存放在服务器上,每次读取报表文件还是很方便的!
开工:第一步引入一个三方的dll ,这里集成了很多报表展示格式,有图文,有分组显示,具体源码我这里也有,需要的留言


第二部:创建你要做的报表文件:

    <FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:xrd="clr-namespace:CodeReason.Reports.Document;assembly=CodeReason.Reports"
                  xmlns:xrbc="clr-namespace:CodeReason.Reports.Document.Barcode;assembly=CodeReason.Reports"
                  PageHeight="29.7cm" PageWidth="21cm" ColumnWidth="21cm">
        <FlowDocument.Resources>
            <!-- Style for header/footer rows. -->
            <Style x:Key="headerFooterRowStyle" TargetType="{x:Type TableRowGroup}">
                <Setter Property="FontWeight" Value="DemiBold"/>
                <Setter Property="FontSize" Value="16"/>
                <Setter Property="Background" Value="LightGray"/>
            </Style>

            <!-- Style for data rows. -->
            <Style x:Key="dataRowStyle" TargetType="{x:Type TableRowGroup}">
                <Setter Property="FontSize" Value="12"/>
            </Style>

            <!-- Style for data cells. -->
            <Style TargetType="{x:Type TableCell}">
                <Setter Property="Padding" Value="0.1cm"/>
            
            </Style>
        </FlowDocument.Resources>
        <Section Padding="0,20,450,10" FontSize="12" BreakPageBefore="True" FontFamily="宋体"  BorderBrush="Green">
            <Paragraph FontSize="24" FontWeight="Bold" TextAlignment="Center">
                点菜单
            </Paragraph>     
    <Table CellSpacing="0" BorderThickness="0"> 
                <Table.Columns>              
                    <TableColumn Width="50*" />
                    <TableColumn Width="50*" />
                  
                </Table.Columns>        
                <TableRowGroup  >
                    <TableRow>
                        <TableCell>
                            <Paragraph >
                                 营业点: @ShopWork
                            </Paragraph>
                        </TableCell> 
                        <TableCell>
                            <Paragraph>
                                台号: @TableNo
                            </Paragraph>
                        </TableCell>
                    </TableRow>
                  
                 
                </TableRowGroup>

            </Table>
         
         
                <Table CellSpacing="0" BorderBrush="White" BorderThickness="0cm">
                    <TableRowGroup >
                        <xrd:TableRowForDynamicHeader TableName="Header"/>
                    </TableRowGroup>
                </Table>
            <Paragraph>---------------------------------------------
            </Paragraph>
            <Table CellSpacing="0" BorderBrush="White" BorderThickness="0" FontFamily="宋体" >
                    <Table.Columns>              
                    <TableColumn Width="1*" />
                    <TableColumn Width="2*" />
                    <TableColumn Width="1*" />
                 
                </Table.Columns>
                <TableRowGroup >
                    <xrd:TableRowForDynamicDataTable TableName="Data"/>
                </TableRowGroup>           
               
            </Table>
            <Paragraph>---------------------------------------------
            </Paragraph>     
    <Table CellSpacing="0" BorderThickness="0">    
                <Table.Columns>              
                    <TableColumn Width="50*" />
                    <TableColumn Width="50*" />                    
                </Table.Columns> 
                <TableRowGroup  >
                    <TableRow>
                        <TableCell>
                            <Paragraph TextAlignment="Left">
                               卡号:@CardNo
                            </Paragraph>
                        </TableCell>  
                      <TableCell>
                            <Paragraph TextAlignment="Left">
                             账单号:@BillNo
                            </Paragraph>
                        </TableCell>                 
                    </TableRow>
                    <TableRow>                 
                        <TableCell>
                            <Paragraph TextAlignment="Left">
                              会员号:@MemoNo
                            </Paragraph>
                        </TableCell>                             
                        <TableCell>
                            <Paragraph TextAlignment="Left">
                             人数:@MemoCount
                            </Paragraph>
                        </TableCell>
                    </TableRow>
                    <TableRow>                 
                        <TableCell>
                            <Paragraph TextAlignment="Left">
                              姓名:@Name
                            </Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph TextAlignment="Left">
                            日期:@Date
                            </Paragraph>
                        </TableCell>
                    </TableRow>
                  <TableRow>                 
                        <TableCell>
                            <Paragraph TextAlignment="Left">
                             服务员: @Operator
                            </Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph TextAlignment="Left">
                           时间: @Time        
                            </Paragraph>
                        </TableCell>
                    </TableRow>
                 
                </TableRowGroup>

            </Table>
         
        </Section>
    </FlowDocument>

复制代码
里面的内容大家可以看下,wpf 中就要用table标签,如果用grid 展示的话,在xp 会不兼容!


以上就是报表文件了

第三步:在wpf 窗体中调用;

页面中加入documentViewer 控件,用来展示 报表

    ReportDocument reportDocument = new ReportDocument();
                    var path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                    StreamReader reader = new StreamReader(new FileStream(path + @"Template\Performance.Template.xaml", FileMode.Open, FileAccess.Read));   // 读取报表的模板文件

                    reportDocument.XamlData = GetContent(reader.ReadToEnd());  // 这里是替换模板文件中的 静态变量内容
                    reportDocument.XamlImagePath = Path.Combine(Environment.CurrentDirectory, @"Template\");//几乎用不到
                    reader.Close();
                    ReportData data = new ReportData();
                    var table = GetRoundScore();
                    data.DataTables.Add(table); //根据报表文件中的data数据 赋值
                    XpsDocument xps = reportDocument.CreateXpsDocument(data);
                    documentViewer.Document = xps.GetFixedDocumentSequence();   // 展示报表

复制代码
报表的预览完成了,你可以调用预览窗口中的 打印按钮打印。也可以自己 添加打印事件!内容如下:

    documentViewer.Document = null;
                PrintDialog pd = new PrintDialog();
                DataSet ds = CreateReportCondata(_billNo);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ReceiptPrint(ds.Tables[0], ds.Tables[1]);
                    PrintQueue pq = pd.PrintQueue;
                    //本机默认打印机打印
                    pq.ShareName = new LocalPrintServer().DefaultPrintQueue.ShareName;
                    var paginator = documentViewer.Document.DocumentPaginator;
                    pd.PrintDocument(paginator, " 打印");
                }

复制代码
以上如果不预览的话,直接可以new 这样的窗体,调用上面的方法可以直接打印


本文来自http://www.aaspx.com/thread-123-1-1.html

你可能感兴趣的:(WPF)