【问题标题】:How to verify that a File Exists in FastReport 4如何验证 FastReport 4 中是否存在文件
【发布时间】:2015-08-04 15:33:37
【问题描述】:

我有一个使用 FastReport 4

DelphiXE 编写的报告

图片在运行时使用存储在表中的文件名动态加载,使用如下:

procedure Picture1OnBeforePrint(Sender: TfrxComponent);
var  pname : string;                                     
begin
   pname := frxGlobalVariables['imgPath']+ <frxDBDataset1."PHOTO">;                                                      
   Picture1.Picture.LoadFromFile(pname);
end;

只要文件在那里,它就可以正常运行。如何验证要加载的文件是否存在?

我尝试使用 Delphi FileExists() 函数,但它显然在 FastReport 4

中不存在

更新: 使用下面的说明,我将 FrFileExists 函数添加到我的报告中。

我将报告中的代码调用如下:

procedure MasterData1OnBeforePrint(Sender: TfrxComponent);
begin
   Photo := imgPath + <frxDBDataset1."PICTURENAME"> + '.jpg';
   if (FrFileExists(Photo)) then Picture1.LoadFromFile(Photo);
end;

我运行了运行时设计器,功能在那里,但正在运行 我得到以下报告:

The following error(s) have occured:  
Could not convert variant of type (Null) into type (Boolean)

如果你认为变量 Photo 不正确,只要图片存在,下面的代码就可以工作:

Picture1.LoadFromFile(Photo);

仍然需要让它工作。

【问题讨论】:

  • @Val Marinov:这是什么://必需的逻辑。这是我需要帮助的部分。
  • Chuck,您已经在 Val 的回答中发表了该评论。 Val 不会在此处收到您对问题的评论,只会收到有关答案的评论。但是我已经回复了您的评论,所以如果您仍然需要帮助,请提出后续问题。

标签: delphi delphi-xe fastreport


【解决方案1】:

FastReport 允许编写和使用自定义函数。

如何做到这一点您可以在:FastReport DeveloperManual-en.pdf中找到。

在第 37、38、39 页的“在报表中使用自定义函数”一章中

我希望这会有所帮助。

更新

在报告中使用自定义函数

FastReport 内置大量标准函数供使用 在报告设计中。 FastReport 还允许自定义函数 编写和使用。使用“FastScript”库添加功能 接口,包含在 FastReport 中(了解更多关于 FastScript 参考它的库手册)。

让我们看看程序是怎样的 和/或函数可以添加到 FastReport。数量和种类 参数因功能而异。 “设置”的参数和 FastScript 不支持“记录”类型,因此它们必须是 使用更简单的类型实现,例如 TRect 可以作为 四个整数:X0、Y0、X1、Y1。还有更多关于使用 FastScript 文档中带有各种参数的函数。

在 Delphi 表单中声明函数或过程及其代码。

function TForm1.MyFunc(s: String; i: Integer): Boolean;
begin
// required logic
end;
procedure TForm1.MyProc(s: String);
begin
// required logic
end;

为报表组件创建“onUser”函​​数处理程序。

function TForm1.frxReport1UserFunction(const MethodName: String;
var Params: Variant): Variant;
begin
  if MethodName = 'MYFUNC' then
    Result := MyFunc(Params[0], Params[1])
  else if MethodName = 'MYPROC' then
  MyProc(Params[0]);
end;

使用报表组件的add方法将其添加到函数列表中 (通常在 Delphi 表单的“onCreate”或“onShow”事件中)。

frxReport1.AddFunction('function MyFunc(s: String; i: Integer):Boolean');
frxReport1.AddFunction('procedure MyProc(s: String)');

添加的功能现在可以在报告脚本中使用,并且可以 由“TfrxMemoView”类型的对象引用。功能也是 显示在“数据树”功能选项卡上。在此选项卡上,功能是 分为类别和选择时有关功能的提示 出现在选项卡的底部窗格中。将上面的代码示例修改为 在单独的类别中注册功能,并显示描述性 提示:

frxReport1.AddFunction('function MyFunc(s: String; i: Integer): Boolean',
'My functions',
' MyFunc function always returns True');
frxReport1.AddFunction('procedure MyProc(s: String)',
'My functions',
' MyProc procedure does not do anything');

添加的功能将出现在“我的功能”类别下。到 现有类别中的注册功能使用以下之一 类别名称:

  • 'ctString' 字符串函数
  • 'ctDate' 日期/时间函数
  • 'ctConv' 转换函数
  • 'ctFormat' 格式化
  • 'ctMath' 数学函数
  • 'ctOther' 其他函数

如果类别名称留空,则函数位于 函数树根。要添加大量功能,它是 建议将所有逻辑放在单独的库单元中。这里 是一个例子:

unit myfunctions;
interface
implementation
uses SysUtils, Classes, fs_iinterpreter;
// you can also add a reference to any other external library here
type
  TFunctions = class(TfsRTTIModule)
    private
    function CallMethod(Instance: TObject; ClassType: TClass;
    const MethodName: String; var Params: Variant):Variant;
    public
    constructor Create(AScript: TfsScript); override;
end;

function MyFunc(s: String; i: Integer): Boolean;
begin
// required logic
end;

procedure MyProc(s: String);
begin
// required logic
end;
{ TFunctions }
constructor TFunctions.Create;
begin
  inherited Create(AScript);
  with AScript do
    AddMethod('function MyFunc(s: String; i: Integer): Boolean',CallMethod,'My functions', ' MyFunc function always returns True');
    AddMethod('procedure MyProc(s: String)', CallMethod,'My functions','MyProc procedure does not do anything'');
  end;
end;

function TFunctions.CallMethod(Instance: TObject; ClassType: TClass;
const MethodName: String;
var Params: Variant): Variant;
begin
  if MethodName = 'MYFUNC' then
    Result := MyFunc(Params[0], Params[1])
  else if MethodName = 'MYPROC' then
    MyProc(Params[0]);
  end;
initialization
 fsRTTIModules.Add(TFunctions);
end.

使用 .pas 扩展名保存文件,然后在 Delphiproject 表单的“uses”子句。您所有的自定义功能 然后将可用于任何报告组件,而无需 需要编写代码将这些函数添加到每个“TfrxReport”和 无需为每个报表组件编写额外的代码 “onUser”函​​数处理程序。

更新2

创建自定义函数FileExists在Delphi中声明函数例如:

function TForm1.FrFileExists(FileName : string):boolean;
begin
  // required logic
  Result := FileExists(FileName);
end; 

使用报表组件的add方法将其添加到函数列表中(通常在Delphi表单的“onCreate”或“onShow”事件中)。

procedure TForm1.FormCreate(Sender: TObject);
begin
  frxReport1.AddFunction('function FrFileExists(FileName:String):Boolean','My functions',
'This function returns True if file exists');
  frxReport1.DesignReport; //<-- THIS SHOW REPORT DESIGNER RUNTIME
end;

为报表组件创建“onUser”函​​数处理程序。

function TForm1.frxReport1UserFunction(const MethodName: string;var Params: Variant): Variant;
begin
  if MethodName = 'FrFileExists' then
    Result := FrFileExists(Params[0])
end;

您不能期望在快速报告 IDE 设计时间中看到这些功能。 要在 IDE 运行时查看函数,请执行以下操作:

在uses子句中包含frxDesgn

使用此代码显示设计器:

 frxReport1.DesignReport; //see code On create above

运行项目,将看到 Fast Report Ide 和我们全新的功能FrFileExists

【讨论】:

  • 这是什么://必需的逻辑。这是我需要帮助的部分。
  • 想一想,查克。如果你在那里打电话给FileExists怎么办?
  • @ChuckO 请查看更新2
【解决方案2】:

我不知道为什么,但是 MethodName 以大写形式传递字符串, 将函数更正为大写 MethodName = 'FRFILEEXISTS'

之后它将起作用(我已经在我的项目中对其进行了测试) 约翰

【讨论】:

    【解决方案3】:

    我很高兴找到这个 frFileExists 函数,因为我使用的是 C++ Builder,所以我拥有它 转换为 C++ 生成器。编译成功后,FastReport IDE 中不显示该函数。有人可以帮忙指出我的错误,非常感谢。我的代码附在下面:

    // To create custom function FileExists
    boolean __fastcall TForm1::FrFileExists(String FileName)
    {
      return (FileExists(FileName.c_str()));
    }
    
    void __fastcall TForm1::FormCreate(TObject *Sender)
    {
      frxReport1->AddFunction(L"FrFileExists", L"My Functions",
         L"This function return true if file exists" );
      frxReport1->DesignReport();
    }
    
    Variant __fastcall TForm1::frxReport1UserFunction(const UnicodeString MethodName,
          Variant &Params)
    {
      if (MethodName == "FrFileExist") {
          return (FrFileExists(Params));
      }
    }
    

    【讨论】:

    • 如果你想要一个答案,你应该发布一个新问题。将此作为对 5 年前问题的回复发布,使其不可见。
    猜你喜欢
    • 2014-02-01
    • 1970-01-01
    • 2019-01-01
    • 2011-03-02
    • 2023-04-08
    • 1970-01-01
    • 2021-07-23
    相关资源
    最近更新 更多