加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何搜索和替换odt Open Office文档?

发布时间:2020-12-15 10:06:32 所属栏目:大数据 来源:网络整理
导读:在我的Delphi应用程序中,我目前使用office ole自动化以编程方式对doc和docx word文档进行Search Replace.有没有人在OpenOffice中有相同的代码(对于doc,docs,odt)? 我还问了一个related question on saving to pdf. 解决方法 你应该把重点放在 XReplaceable
在我的Delphi应用程序中,我目前使用office ole自动化以编程方式对doc和docx word文档进行Search& Replace.有没有人在OpenOffice中有相同的代码(对于doc,docs,odt)?

我还问了一个related question on saving to pdf.

解决方法

你应该把重点放在 XReplaceable界面上.这是一个例子.请注意,没有错误处理.我已经用LibreOffice编写器对它进行了测试,它对我来说很好.
uses
  ComObj;

procedure OpenOfficeReplace(const AFileURL: string; ASearch: string; const AReplace: string);
var
  StarOffice: Variant;
  StarDesktop: Variant;
  StarDocument: Variant;
  FileReplace: Variant;
  FileParams: Variant;
  FileProperty: Variant;

begin
  StarOffice := CreateOleObject('com.sun.star.ServiceManager');
  StarDesktop := StarOffice.CreateInstance('com.sun.star.frame.Desktop');

  FileParams := VarArrayCreate([0,0],varVariant);
  FileProperty := StarOffice.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
  FileProperty.Name := 'Hidden';
  FileProperty.Value := False;
  FileParams[0] := FileProperty;

  StarDocument := StarDesktop.LoadComponentFromURL(AFileURL,'_blank',FileParams);

  FileReplace := StarDocument.CreateReplaceDescriptor;
  FileReplace.SearchCaseSensitive := False;
  FileReplace.SetSearchString(ASearch);
  FileReplace.SetReplaceString(AReplace);

  StarDocument.ReplaceAll(FileReplace);

  ShowMessage('Replace has been finished');

  StarDocument.Close(True);
  StarDesktop.Terminate;
  StarOffice := Unassigned;
end;

以及示例的用法

procedure TForm1.Button1Click(Sender: TObject);
begin
  OpenOfficeReplace('file:///C:/File.odt','Search','Replace');
end;

SearchDescriptor还有几个搜索/替换选项.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读