// Die Werte (nicht die Formeln) aus einer Excel-Tabelle werden in ein
// Stringgrid geladen. MS-Excel muss auf dem Rechner installiert sein.

// Getestet mit D4 unter XP

uses ComObj; 
 
var 
  ExcelApplicationl, FWorkBook, FWorkSheet: OLEVariant; 
 
function spaltenname(x: word): string; 
begin 
  if x < 256 then begin 
    if x < 26 then result := chr(x + 65) else 
      result := chr(x div 26 + 64) + chr(x mod 26 + 65); 
  end else result := ''; 
end; 
 
procedure TForm1.FormCreate(Sender: TObject); 
var 
  x: integer; 
begin 
  with StringGrid1 do begin 
    FixedRows := 1; 
    FixedCols := 1; 
    cells[0, 0] := ''; 
    for x := 1 to ColCount - 1 do 
      cells[x, 0] := spaltenname(x - 1); 
    for x := 1 to RowCount - 1 do 
      cells[0, x] := inttostr(x); 
  end; 
end; 
 
procedure DisConnect; 
begin 
  ExcelApplicationl.Workbooks.Close; 
  ExcelApplicationl.Quit; 
end; 
 
procedure Connect(Mappe, Tabelle: string; vonSpalte, bisSpalte, 
  vonZeile, bisZeile: word; SG: TStringGrid); 
var 
  x, y: integer; 
  sx, sy: string; 
begin 
  ExcelApplicationl := CreateOleObject('Excel.Application'); 
  FWorkBook := ExcelApplicationl.Workbooks.Open(Mappe); 
  FWorkSheet := FWorkBook.Worksheets.Item[Tabelle]; 
  for x := vonSpalte - 1 to bisSpalte - 1 do begin 
    sx := spaltenname(x); 
    if sx <> '' then 
      for y := vonZeile to bisZeile do 
        if y > 0 then begin 
          sy := inttostr(y); 
          SG.cells[x + 1, y] := 
            FWorkSheet.Range[sx + sy, sx + sy]; 
        end; 
  end; 
  DisConnect; 
end; 
 
// Beispielaufruf 
procedure TForm1.Button6Click(Sender: TObject); 
begin 
  Connect('C:\mappe2.xls', 'Tabelle1', 1, StringGrid1.ColCount - 1, 
    1, StringGrid1.RowCount - 1, StringGrid1); 
end;



 

Zugriffe seit 6.9.2001 auf Delphi-Ecke