Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Markierter Bereich in Excel (https://www.delphipraxis.net/63762-markierter-bereich-excel.html)

DevilsCamp 22. Feb 2006 17:10


Markierter Bereich in Excel
 
Wie ich Daten aus Excel bekomme weiß ich.

Allerdings bin ich am verzweifeln, wie ich es schaffe, nur den markierten Bereich in einer Excel-Tabelle auszulesen.
Da dieser ja dynamisch, zur Compilierzeit ja nicht bekannt ist, stehe ich da z.Z. vor einem Problem.

Ich hoffe, mir kann da jemand helfen.

Ferber 22. Feb 2006 18:20

Re: Markierter Bereich in Excel
 
Hi !

Hier ein komplettes Demo.

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComObj;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    FExcelApp:Variant;
    function GetApplication:Variant;
    function GetSelection: Variant;
    function GetActiveSheet: Variant;
  public
    property XlsApp:Variant read GetApplication;
    property ActiveSheet:Variant read GetActiveSheet;
    property Selection:Variant read GetSelection;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function TForm1.GetApplication:Variant;
begin
  if VarType(FExcelApp)=varEmpty then
  try
    FExcelApp:=GetActiveOleObject('Excel.Application');
    FExcelApp.Visible:=True;
  except
    FExcelApp:=  CreateOleObject('Excel.Application');
    FExcelApp.Visible:=True;
  end;
  Result:=FExcelApp;
end;

function TForm1.GetSelection: Variant;
begin
  Result:=XlsApp.Selection;
end;

function TForm1.GetActiveSheet: Variant;
begin
  Result:=XlsApp.ActiveSheet;
end;

procedure TForm1.Button1Click(Sender: TObject);
var Sel:Variant;
    r,c,r1,c1,r2,c2:Integer;
    s:String;
begin
  Sel:=Selection;
  r1:=Sel.Row;
  c1:=Sel.Column;
  r2:=Sel.Rows.Count;
  c2:=Sel.Columns.Count;
  for r:=r1 to r1+r2-1 do
    for c:=c1 to c1+c2-1 do
      begin
        s:=ActiveSheet.Cells[r,c].Value;
        Memo1.Lines.Add('Cell('+IntToStr(r)+','+IntToStr(c)+').Value='+s);
      end;
end;

end.
Excel ist IMHO auch sehr gut zum Drucken von Formularen.
In Excel Namen einfügen, die findet man aus Delphi mit:

Delphi-Quellcode:
procedure TExcel.RCofName(aName: String; var r, c: Integer);
begin
  XlsApp.Goto(aName);
  r:=Selection.Row;
  c:=Selection.Column;
end;
Mit freundlichen Grüssen vom 'Ferber' :coder:


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:44 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz