c# fastreport 打印
在软件中需要引用 fastreport 提供的 dll
Fastreport 打印需要先在本地生成一个打印文件。
<?xml version="1.0" encoding="utf-8"?> <Report ScriptLanguage="CSharp" DoublePass="true" ReportInfo.Created="12/31/2020 10:58:42" ReportInfo.Modified="04/28/2021 13:28:35" ReportInfo.CreatorVersion="2021.1.0.0"> <Dictionary> <TableDataSource Name="明细" ReferenceName="Data.明细" DataType="System.Int32" Enabled="true"> <Column Name="商品名" DataType="System.String"/> <Column Name="单价" DataType="System.Decimal"/> <Column Name="数量" DataType="System.Decimal"/> </TableDataSource> <Parameter Name="客户名" DataType="System.String"/> <Parameter Name="电话号码" DataType="System.String"/> </Dictionary> <ReportPage Name="Page1" RawPaperSize="9" LeftMargin="1" RightMargin="1" FirstPageSource="1" OtherPagesSource="1" Watermark.Font="微软雅黑, 60pt"> <ReportTitleBand Name="ReportTitle1" Width="786.24" Height="103.95"> <TextObject Name="Text1" Left="37.8" Top="9.45" Width="699.3" Height="28.35" Text="单据" HorzAlign="Center" VertAlign="Center" Font="微软雅黑, 16pt"/> <TextObject Name="Text2" Left="37.8" Top="37.8" Width="699.3" Height="18.9" Text="宠主信息:[客户名]" VertAlign="Center" Font="宋体, 9pt, style=Bold"/> <TextObject Name="Text3" Left="37.8" Top="56.7" Width="75.6" Height="18.9" Text="电话号码:" VertAlign="Center" Font="宋体, 9pt"/> <TextObject Name="Text4" Left="122.85" Top="56.7" Width="236.25" Height="18.9" Text="[电话号码]" VertAlign="Center" Underlines="true" Font="宋体, 9pt"/> </ReportTitleBand> </ReportPage> </Report>
在实际打印时,加载打印文件,并将文件中的参数替换成实际值。
public static void Print() { FastReport.Report report = new FastReport.Report(); report.Load("./Reports/Labels.frx"); report.SetParameterValue("客户名", "张三"); report.SetParameterValue("电话号码", "12345"); DataTable dt = new DataTable(); dt.TableName = "明细"; dt.Columns.Add("商品名", typeof(string)); dt.Columns.Add("单价", typeof(string)); dt.Columns.Add("数量", typeof(string)); dt.Rows.Add(); DataSet ds = new DataSet(); ds.Tables.Add("商品1",2m,3m); ds.Tables.Add("商品2",3m,4m); report.RegisterData(ds); report.Prepare(); report.PrintSettings.Printer = "我的打印机"; report.PrintSettings.Copies = 1; report.PrintSettings.ShowDialog = false; report.Print(); }
fastreport 提供了 winform 的控件用于修改报表文件
<Window x:Class="WndSet" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Title="" Height="800" Width="1200" WindowStartupLocation="CenterScreen"> <Grid> <WindowsFormsHost Name="formHost" Margin="30,20,30,20"/> </Grid> </Window>
public partial class WndSet : Window
{
private DesignerControl dc = new DesignerControl();
public WndSet(Report r)
{
InitializeComponent();
this.formHost.Child = dc;
dc.Report = r;
}
}
在编辑界面选中控件或标题区可以添加事件,编写脚本。左下角可以切换脚本和图形界面。