我自已修改的ConverterQR2FR.pas,用于QuickReport 转换成FastReport

说明:FastReport4.68自带的ConverterQR2FR.pas文件为了通用性没有处理更多的标签类型,转换完的格式都是文本,我手中的项目如果这样转换,还需要很多工作量调整,所以改了一下,大家作为参考,有不好的地方希望大家指正

另外,项目中的报表全是一个数据集,所以我只处理了一个数据集,有些代码是专门处理我项目相关的,通用性可能不好,但对我来说够用了,转换完的报表可以直接使用,不需要进行任何调整。

ConverterQR2FR.pas

ContractedBlock.gif ExpandedBlockStart.gif ConverterQR2FR
{******************************************}
{                                          }
{             FastReport 4                 }
{        Converter from QuickReport        }
{                                          }
{         Copyright (c) 2007-2008          }
{             by Serg Puhoff               }
{            Fast Reports Inc.             }
{                                          }
{                                          }
{******************************************}

//
// Using:
//   conv := TConverterQr2Fr.Create;
//   conv.Source := QuickRep1;
//   conv.Target := FReport;
//   conv.Convert;
//   FReport.SaveToFile('converted_fromQR.fr3');
//

unit ConverterQR2FR;

interface

uses frxClass, QuickRpt, frxADOComponents, strutils,math;

type
  TConverterQr2Fr 
= class
  private
    FTarget: TfrxReport;
    FSource: TQuickRep;
  protected
    
procedure CreateMainBands;
    
procedure CreateCustomBand(const ABand: TQRCustomband);
    
procedure AddObjects(const ABand: TQRCustomband; const Band: TfrxBand);
  public
    property Source: TQuickRep read FSource write FSource;
    property Target: TfrxReport read FTarget write FTarget;

    
procedure Convert;
  
end;

implementation

uses SysUtils, Controls, QrPrntr, Classes, QRCtrls, frxRich;


{ TConverterQr2Fr }

procedure TConverterQr2Fr.AddObjects(const ABand: TQRCustomband;
  
const Band: TfrxBand);
var
  i: integer;
  s: string;
  Memo: TfrxMemoView;
  Rich: TfrxRichView;
  Shape: TfrxShapeView;
  Image: TfrxPictureView;
  
//此处改写,分清QrLabel, QrDBText ,QrExpr..
  Lbl: TQRLabel;
  Txt: TQRDBText;
  Exp: TQRExpr;
  Rt: TQRRichText;
  Sh: TQRShape;
  Img: TQRImage;
  Sys: TQRSYSData;
  ExpMemo: TQRExprMemo;
  cus: TQRCustomLabel;

begin
  
for i := 0 to ABand.ControlCount - 1 do
    
if ABand.Controls[i] is TQRLabel then
    
begin
      Lbl :
= ABand.Controls[i] as TQRLabel;
      Memo :
= TfrxMemoView.Create(Band);
      Memo.CreateUniqueName;
      Memo.Memo.Text :
= Lbl.Caption;
      Memo.Font.Assign(Lbl.Font);
      memo.Font.Charset :
= 1;
      Memo.SetBounds(Lbl.Left, Lbl.Top, Lbl.Width
*2, Lbl.Height);
    
end

    
else if ABand.Controls[i] is TQRExprMemo then
    
begin
      ExpMemo :
= ABand.Controls[i] as TQRExprMemo;
      Memo :
= TfrxMemoView.Create(Band);
      Memo.CreateUniqueName;
      Memo.Memo.Text :
= ExpMemo.Lines.Text;
      Memo.Font.Assign(ExpMemo.Font);
      memo.Font.Charset :
= 1;

      Memo.SetBounds(ExpMemo.Left, ExpMemo.Top, ExpMemo.Width
*2, ExpMemo.Height);
    
end

    
else if ABand.Controls[i] is TQRSYSData then
    
begin
      Sys :
= ABand.Controls[i] as TQRSYSData;
      Memo :
= TfrxMemoView.Create(Band);
      Memo.CreateUniqueName;
      
case sys.Data of
        qrsTime: Memo.Text :
= '[Time]';
        qrsDate: Memo.Text :
= '[Date]';
        qrsDateTime: Memo.Text :
= '[<Date>+<Time>]';
        qrsPageNumber: Memo.Text :
= '[Page]';
        qrsReportTitle: Memo.Text :
= '''kw-report''';
        qrsDetailCount: Memo.Text :
= '[COUNT(MasterData1)]';
        qrsDetailNo: Memo.Text :
= '[Line]';
      
end;
      Memo.Font.Assign(Sys.Font);
      memo.Font.Charset :
= 1;
      Memo.SetBounds(Sys.Left, Sys.Top, Sys.Width
*2, Sys.Height);
    
end

    
else if ABand.Controls[i] is TQRDBText then
    
begin
      Txt :
= ABand.Controls[i] as TQRDBText;
      Memo :
= TfrxMemoView.Create(Band);
      Memo.CreateUniqueName;
      
//Memo.Memo.Text := '[ds."'+Txt.DataField+'"]';
      
if Target.DataSets.Count > 0 then
      
begin
        Memo.DataSet :
= Target.DataSets[0].DataSet;
        Memo.DataField :
= Txt.DataField;
      
end
      
else
      
begin
        Memo.Memo.Text :
= Txt.Caption;
      
end;
      Memo.Font.Assign(Txt.Font);
      memo.Font.Charset :
= 1;
      
if trim(Txt.Mask) = '0.00' then
        Memo.DisplayFormat.FormatStr :
= '%2.2f';
      Memo.SetBounds(Txt.Left, Txt.Top, Txt.Width
*2, Txt.Height);
    
end
    
else if ABand.Controls[i] is TQRExpr then
    
begin
      Exp :
= ABand.Controls[i] as TQRExpr;
      Memo :
= TfrxMemoView.Create(Band);
      Memo.CreateUniqueName;
      s :
= Exp.Expression;
      
//s := stringreplace(
      
if leftstr(s, 6= 'PARAMS' then
        s :
= '[' + lowercase(rightstr(s, length(s) - 6)) + ']'
      
else
      
begin
        s :
= stringreplace(s, 'cdsReportDataSet.''<ds."', [rfReplaceAll, rfIgnoreCase]);
        s :
= stringreplace(s, ' ''', [rfReplaceAll]);
        s :
= stringreplace(s, '-''"> - ', [rfReplaceAll]);
        s :
= stringreplace(s, '+''"> + ', [rfReplaceAll]);
        s :
= stringreplace(s, '*''"> * ', [rfReplaceAll]);
        s :
= stringreplace(s, '/''"> / ', [rfReplaceAll]);
        s :
= '[' + s + '">]';
        s :
= stringreplace(s, ')">''">)', [rfReplaceAll]);
      
end;
      Memo.Memo.Text :
= s;
      Memo.Font.Assign(Exp.Font);
      memo.Font.Charset :
= 1;
      
if trim(Exp.Mask) = '0.00' then
        Memo.DisplayFormat.FormatStr :
= '%2.2f';

      Memo.SetBounds(Exp.Left, Exp.Top, Exp.Width
*2, Exp.Height);
    
end
    
else if ABand.Controls[i] is TQRRichText then
    
begin
      Rt :
= ABand.Controls[i] as TQRRichText;
      Rich :
= TfrxRichView.Create(Band);
      Rich.RichEdit.Lines.Assign(Rt.Lines);
      Rich.CreateUniqueName;
      Rich.RichEdit.Font.Assign(Rt.Font);
      Rich.RichEdit.Font.Charset :
= 1;
      Rich.Color :
= Rt.Color;
      Rich.SetBounds(Rt.Left, Rt.Top, Rt.Width
*2, Rt.Height);
    
end
    
//此段只能放最后,用来处理以上情况没处理到的
    
else if ABand.Controls[i] is TQRCustomLabel then
    
begin

      cus :
= ABand.Controls[i] as TQRCustomLabel;
      Memo :
= TfrxMemoView.Create(Band);
      Memo.CreateUniqueName;
      
if cus.Caption = '' then
        Memo.Memo.Text :
= cus.Lines.Text
      
else
        Memo.Memo.Text :
= cus.Caption;
      Memo.Font.Assign(cus.Font);
      memo.Font.Charset :
= 1;
      Memo.SetBounds(cus.Left, cus.Top, cus.Width
*2, cus.Height);
    
end

    
else if ABand.Controls[i] is TQRShape then
    
begin
      Sh :
= ABand.Controls[i] as TQRShape;
      Shape :
= TfrxShapeView.Create(Band);
      Shape.CreateUniqueName;
      
case Sh.Shape of
        qrsRectangle: Shape.Shape :
= skRectangle;
        qrsCircle: Shape.Shape :
= skEllipse;
        qrsRoundRect: Shape.Shape :
= skRoundRectangle;
      
end;
      Shape.SetBounds(Sh.Left, Sh.Top, ifthen(Sh.Width
=1,0,Sh.Width), ifthen(Sh.Height=1,0,Sh.Height));
    
end

    
else if ABand.Controls[i] is TQRImage then
    
begin
      Img :
= ABand.Controls[i] as TQRImage;
      Image :
= TfrxPictureView.Create(Band);
      Image.CreateUniqueName;
      Image.Picture.Assign(Img.Picture);
      Image.SetBounds(Img.Left, Img.Top, Img.Width
*2, Img.Height);
    
end;

end;

procedure TConverterQr2Fr.Convert;
begin
  
if not Assigned(Source) then
    raise Exception.Create(
'Source not assigned');
  
if not Assigned(Target) then
    raise Exception.Create(
'Target not assigned');

  Target.Clear;
  CreateMainBands;
end;

procedure TConverterQr2Fr.CreateCustomBand(const ABand: TQRCustomBand);
var
  s: string;
  Band: TfrxBand;
begin
  
if ABand.BandType = rbPageHeader then
    Band :
= TfrxPageHeader.Create(Target.Pages[0])
  
else
    
if ABand.BandType = rbPageFooter then
      Band :
= TfrxPageFooter.Create(Target.Pages[0])
    
else
      
if ABand.BandType = rbTitle then
        
//Band := TfrxHeader.Create(Target.Pages[0])
        Band :
= TfrxReportTitle.Create(Target.Pages[0])
      
else
        
if ABand.BandType = rbChild then
          Band :
= TfrxChild.Create(Target.Pages[0])
        
else
          
if ABand.BandType = rbColumnHeader then
            Band :
= TfrxColumnHeader.Create(Target.Pages[0])
          
else
            
if ABand.BandType = rbGroupHeader then
            
begin
              s :
= (ABand as TQrGroup).Expression;
              Band :
= TfrxGroupHeader.Create(Target.Pages[0]);
              s :
= stringreplace(s, 'cdsReportDataSet.''ds."', [rfReplaceAll, rfIgnoreCase]);
              s :
= trim(s) + '"';
              (Band as TfrxGroupHeader).Condition :
= s;
            
end
            
else
              
if ABand.BandType = rbGroupFooter then
                Band :
= TfrxGroupFooter.Create(Target.Pages[0])
              
else
                
if ABand.BandType = rbDetail then
                  
//Band := TfrxDetailData.Create(Target.Pages[0])
                  Band :
= TfrxMasterData.Create(Target.Pages[0])
                
else
                  Exit;

  Band.CreateUniqueName;

  Band.Top :
= ABand.Top;
  Band.Height :
= ABand.Height;

  AddObjects(ABand, Band);
end;

procedure TConverterQr2Fr.CreateMainBands;
var
  Page: TfrxReportPage;
  ds: TfrxAdoquery;
  i: integer;
begin
  Page :
= TfrxReportPage.Create(Target);
  ds :
= TfrxAdoquery.Create(Target);
  ds.Name :
= 'ds';
  Target.DataSets.Add(ds);
  Page.CreateUniqueName;
  Page.SetDefaults;

  
for i := 0 to Source.BandList.Count - 1 do
    CreateCustomBand(TQRCustomBand(Source.BandList[i]));
end;
end.


//205aa5d1322732bcddf9875f203310df

转载于:https://www.cnblogs.com/cnanyi/archive/2008/10/29/1321921.html

Test_FastReportPrint4万能fr3报表查看器 自设计中支持按文本格式查看fr3报表, 前提是报表中最好不要有图片,否则打开会无响应或反应很慢 测试fr3文件2M左右反应很慢 procedure TFrm_BarCode.FallBitBtn6Click(Sender: TObject); begin //默认设计 MyTmsPrintDropBtn1.FrxReport.DesignPreviewPage end; procedure TFrm_BarCode.FallBitBtn3Click(Sender: TObject); begin //自写设计 MyTmsPrintDropBtn1.PrintRpt_DesignerW(FrptPrintIDX,Frpttitle,FrptFile); end; procedure TFrm_BarCode.FallBitBtn5Click(Sender: TObject); begin //预览 MyTmsPrintDropBtn1.PrintRpt_PreviewNoDialog(FrptPrintIDX,Frpttitle,FrptFile); end; procedure TFrm_BarCode.FallBitBtn4Click(Sender: TObject); begin //无对话框打印 MyTmsPrintDropBtn1.PrintRpt_PrintNow(FrptPrintIDX,Frpttitle,FrptFile); end; procedure TFrm_BarCode.MyTmsPrintDropBtn1BeforeDropMenuClick( Sender: TMenuItem; ItemsTag: Integer; var CanClick: Boolean; var Memo: String); begin MyTmsPrintDropBtn1.RptFileName:=''; end; procedure TFrm_BarCode.AdvStringGrid1GetEditorType(Sender: TObject; ACol, ARow: Integer; var AEditor: TEditorType); begin if (ACol=0 ) and (ARow=0) then begin AEditor:=edComboList; end; end; procedure TFrm_BarCode.MyTmsPrintDropBtn2BeforeDropMenuClick( Sender: TMenuItem; ItemsTag: Integer; var CanClick: Boolean; var Memo: String); begin with MyTmsPrintDropBtn1 do begin RptPrintIDX:=0; RptPrtTitle:='xxxxxx'; RptFileName:=gsPath+'data\test.fr3' end; end; procedure TFrm_BarCode.FallBitBtn1Click(Sender: TObject); begin MyTmsPrintDropBtn1.FrxReport.PreviewPages.Export(frxXLSExport1) ; end; procedure TFrm_BarCode.FallBitBtn8Click(Sender: TObject); begin //初始化数据 with MyTmsPrintDropBtn1 do begin RptPrintIDX:=StrToInt(Trim(lbe_idx.Text)); //打印机 RptPrtTitle:=Trim(lbe_title.Text); //打印时显示的文件名 RptFileName:=Trim(lbe_file.Text) //打印模板 end; end; end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值