Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling existing excel file using OLE - delphi

Tags:

delphi

ole

I have an excel file which contains a format for the data that will be passed in it. Can Someone help me how to open an existing excel file?

var
  myExcel:variant;
begin
  myExcel:=CreateOleObject('Excel.application');
  myExcel.caption:='Summary of Check Issued';
  myExcel.visible:=false;
  myexcel.workbooks.add(1);
end

that statement will create a new excel file but what i want is a statement to just open my existing excel file.

like image 954
user3505049 Avatar asked Mar 03 '26 20:03

user3505049


1 Answers

The exact line of code you are looking for is:

WorkBook := ExcelFile.WorkBooks.Open('yourfilename.xls');

The complete code might look something like this:

var
   ExcelFile : Variant;
   WorkBook : Variant;
   WorkSheet : Variant;

begin

  // Open Excel OLE
  ExcelFile :=  CreateOleObject('Excel.Application');

  // Handle WoorkBook
  if not VarIsNull(ExcelFile) then begin
      WorkBook := ExcelFile.WorkBooks.Open('yourfilename.xls');

      if not VarIsNull(WorkBook) then begin
      // Handle Sheet
          WorkSheet := WorkBook.WorkSheets.Item['yourSheetName'];
      end;

  end; 

Refer to this article for more detailed information.

like image 62
Matt C Avatar answered Mar 05 '26 17:03

Matt C