简体   繁体   English

在Delphi 5中嵌入Excel文件

[英]Embed Excel file in Delphi 5

I'm trying embed an Excel file into my Delphi 5 application, so I can avoid my users just deleting the file accidentally. 我正在尝试将Excel文件嵌入到我的Delphi 5应用程序中,这样可以避免用户只是意外删除文件。

Using the embedded file, I create it on disk with a Save dialog, and then open it with the Excel := CreateOleObject('Excel.Application'); 使用嵌入式文件,通过“保存”对话框在磁盘上创建它,然后使用Excel := CreateOleObject('Excel.Application');打开它Excel := CreateOleObject('Excel.Application'); method. 方法。 I've seen examples on how to load a resource, using THandles, but I don't seem to get it working with Excel.WorkBooks.Open(EmbeddedExcelFile); 我已经看到了有关如何使用THandles加载资源的示例,但是我似乎无法使其与Excel.WorkBooks.Open(EmbeddedExcelFile); .

Have you had to do something like this before? 您以前必须做过这样的事情吗? How would you do it? 你会怎么做?

Thanks! 谢谢!

You have to include the file as a resource. 您必须将文件包含为资源。 Say you have a blah.xls 说你有一个blah.xls

Create a blah.rc file with the following content 创建具有以下内容的blah.rc文件

blah RCDATA blah.xls

compile it with the resource compiler into blah.res 使用资源编译器将其编译为blah.res

embed the RES file within the executable 将RES文件嵌入可执行文件中

{$R 'blah.res'}

in your application code, extract the file and run it with this code 在您的应用程序代码中,提取文件并使用此代码运行

procedure ExtractAndRun(resourceID:string; resourceFn:string);
 var
  resourceStream: TResourceStream;
  fileStream: TFileStream;
  tempPath: string;
  fullFileName: string;

 begin
  tempPath:=GetTempDir;
  FullFilename:=TempPath+'\'+resourceFN;
  if not FileExists(FullFilename) then
   begin
     resourceStream := TResourceStream.Create(hInstance, resourceID, RT_RCDATA);
     try
      fileStream := TFileStream.Create(FullFilename, fmCreate);
      try
       fileStream.CopyFrom(resourceStream, 0);
      finally
       fileStream.Free;
      end;
     finally
      resourceStream.Free;
     end;
   end;
  ShellExecute(0,'open', pchar(FullFilename), nil, nil, SW_SHOWNORMAL);
end;

you'll have to add ShellApi in your uses clause 您必须在uses子句中添加ShellApi

maybe you'll need this GetTempDir function 也许您需要此GetTempDir函数

function GetTempDir: string;
 var
  Buffer: array[0..MAX_PATH] of char;
 begin
  GetTempPath(SizeOf(Buffer) - 1, Buffer);
  result := StrPas(Buffer);
 end; 

invoke the function like this 像这样调用函数

 extractAndRun('blah','blah.xls');

I am pretty sure it will not work. 我很确定它不会起作用。 You have to save the file in a temp folder, alter it and and then do whatever you want. 您必须将文件保存在temp文件夹中,对其进行更改,然后执行所需的任何操作。

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

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