unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleServer, ExcelXP, COmObj;
type
TForm1 = class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
ExcelApplication1: TExcelApplication;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
function ermittleLetzteSpalteDerExcelDatei(dateiname: string): Integer;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
ShowMessage(IntToStr(ermittleLetzteSpalteDerExcelDatei(OpenDialog1.FileName)));
end;
function TForm1.ermittleLetzteSpalteDerExcelDatei(dateiname: string): Integer;
var
myExcel, sheet: OLEVariant;
zeile: Integer;
begin
if dateiname <> '' then
begin
try
// Create Excel-
OLE Object
myExcel := CreateOleObject('Excel.Application')
except
ShowMessage('Excel konnte nicht gestartet werden!');
exit
end;
// Excel verbergen
myExcel.Visible := false;
// Öffnen der gewählten Arbeitsmappe
myExcel.Workbooks.Open(dateiname);
// Sheet := XLApp.Workbooks[1].WorkSheets[1];
Sheet := myExcel.Workbooks[ExtractFileName(dateiname)].WorkSheets[1];
// In order to know the dimension of the WorkSheet, i.e the number of rows
// and the number of columns, we activate the last non-empty cell of it
Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
//myExcel.ActiveCell.Row;
Result := myExcel.ActiveCell.Row;
// Excel wieder schließen
myExcel.DisplayAlerts := False;
myExcel.Quit;
myExcel := Unassigned;
Sheet := Unassigned;
end;
end;
end.