Entry
Delphi: Spreadsheet: File: Run: How to run an Excel file from Delphi?
Jul 24th, 2003 14:11
Knud van Eeden,
Language: Computer: Borland: Delphi: Spreadsheet: File: Run: How to
run an Excel file from Delphi?
Steps: Overview:
1. -Open a new application:
-start your Delphi
-select from menu option 'File'
-select from list 'New'
-select from list 'Application'
2. -Put a button component on the form:
-Open the 'Standard' palette
-double click on icon 'Button'
3. -Apply an event to clicking on the button 1:
-double click on the 'Button 1' icon on the form
-type or copy/paste the text inside:
procedure TForm1.Button1Click(Sender: TObject);
// uses ShellApi
var executablefilenameS : string;
var parameterfilenameS : string;
begin
executablefilenameS := 'C:\Program Files\Microsoft Office\Office10
\excel.exe';
parameterfilenameS := 'myexcelfilename.xls';
ShellExecute( 0, 'open', PChar( executablefilenameS ), PChar(
parameterfilenameS ), nil, SW_SHOW );
end;
4. This will show the following source code in your .pas file:
--- cut here ---------------------------------------------------------
unit Unit1;
interface
uses
ShellApi,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
// uses ShellApi
var executablefilenameS : string;
var parameterfilenameS : string;
begin
executablefilenameS := 'C:\Program Files\Microsoft Office\Office10
\excel.exe';
parameterfilenameS := 'myexcelfilename.xls';
ShellExecute( 0, 'open', PChar( executablefilenameS ), PChar(
parameterfilenameS ), nil, SW_SHOW );
end;
end.
--- cut here ---------------------------------------------------------
5. Compile and run this code
6. When you click on the button, this will start Excel on your
computer, and load the given Excel spreadsheet
Note: you will have to adapt the path to your
Excel executable and spreadsheet filename to the conditions on
your computer)