繁体   English   中英

如何验证 FastReport 4 中的文件是否存在

[英]How to verify that a File Exists in FastReport 4

我有一份使用FastReport 4DelphiXE编写的报告

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

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);

仍然需要让它工作。

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

如何执行此操作,您可以在:FastReport DeveloperManual-en.pdf 中找到。

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

我希望这有帮助。

更新

在报表中使用自定义函数

FastReport 有大量内置的标准函数用于报表设计。 FastReport 还允许编写和使用自定义函数。 使用包含在 FastReport 中的“FastScript”库接口添加函数(要了解有关 FastScript 的更多信息,请参阅其库手册)。

让我们看看如何将过程和/或函数添加到 FastReport。 参数的数量和类型因函数而异。 FastScript 不支持“Set”和“Record”类型的参数,因此必须使用更简单的类型来实现它们,例如一个 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”类型的对象引用。 该功能也显示在“数据树”功能选项卡上。 On this tab functions are divided into categories and when selected a hint about the function appears in the bottom pane of the tab. 修改上面的代码示例以在不同的类别中注册函数,并显示描述性提示:

 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;

您不能期望在 Fast report IDE 设计时看到这些功能。 要查看 IDE 运行时中的函数,请执行以下操作:

在uses 子句中包含frxDesgn

使用此代码显示设计器:

 frxReport1.DesignReport; //see code On create above

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

在此处输入图片说明

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

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

我很高兴找到这个 frFileExists 函数,因为我使用的是 C++ Builder,所以我将它转换为 C++ Builder。 编译成功后,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));
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM