// 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; // Get the value of the last row x := XLApp.ActiveCell.Row; // Get the value of the last column y := XLApp.ActiveCell.Column;
// Set Stringgrid's row &col dimensions.
AGrid.RowCount := x; // AGrid.ColumnCount := y;
// Assign the Variant associated with the WorkSheet to the Delphi Variant
RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value; // Define the loop for filling in the TStringGrid k := 2; repeat for r := 1 to y do begin AGrid.Cells[(r - 1), (k-2)] := RangeMatrix[K, R]; if r=1 then AGrid.Cells[0,k-2]:= Timetostr(strtofloat( AGrid.Cells[0,k-2])) ;
end; Inc(k, 1); AGrid.RowCount := k + 1; until k > x; // Unassign the Delphi Variant Matrix RangeMatrix := Unassigned;
finally // Quit Excel if not VarIsEmpty(XLApp) then begin // XLApp.DisplayAlerts := False; XLApp.Quit; XLAPP := Unassigned; Sheet := Unassigned; Result := True; end; end; end;
procedure TForm1.Button1Click(Sender: TObject); var fname:string; begin // OpenDialog1.InitialDir:='..\..\Export' ;
if OpenDialog1.Execute=true then
fname:= OpenDialog1.FileName;
if Xls_To_StringGrid(StringGrid1, fname) then ShowMessage('Файл '+fname+ ' успешно загружен');