AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Excel mit mehreren Sheets öffnen

Ein Thema von Moombas · begonnen am 5. Apr 2018 · letzter Beitrag vom 10. Sep 2018
 
Benutzerbild von Moombas
Moombas

Registriert seit: 22. Mär 2017
Ort: bei Flensburg
525 Beiträge
 
FreePascal / Lazarus
 
#1

Excel mit mehreren Sheets öffnen

  Alt 5. Apr 2018, 12:28
Moin ich habe folgendes Problem:

Ich habe eine xls Datei mit mehreren Sheets und möchte sie in Stringgrid einlesen. Mit dem ersten Sheet funktioniert es auch aber ab dem 2. kommt immer eine Fehlermeldung.

"Die Activate Methode des Range Objektes konnte nicht ausgeführt werden"

Jemand eine Idee wo mein Denkfehler ist?

Code:
function TTools.Xls_To_StringGrid(AGrid: TStringGrid; AXLSFile: string; Sheetname : string): Boolean;
const
  xlCellTypeLastCell = $0000000B;
var
  XLApp, Sheet: OLEVariant;
  RangeMatrix: Variant;
  x, y, k, r: Integer;
begin
  Result := False;
  XLApp := CreateOleObject('Excel.Application'); // Create Excel-OLE Object
  try
    XLApp.Visible := True;                       // Excel
    XLApp.Workbooks.Open(AXLSFile);              // Open the Workbook

    Sheet := XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets[sheetname]; // Sheet := XLApp.Workbooks[1].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;

    x := XLApp.ActiveCell.Row;                  // Get the value of the last row
    y := XLApp.ActiveCell.Column;               // Get the value of the last column

    // Set Stringgrid's row &col dimensions.
    AGrid.RowCount := x;
    AGrid.ColCount := y;

    RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value;    // Assign the Variant associated with the WorkSheet to the Delphi Variant

    //  Define the loop for filling in the TStringGrid
    k := 1;
    repeat
      for r := 1 to y do
        AGrid.Cells[(r - 1), (k - 1)] := RangeMatrix[K, R];
      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 TTools.EinlesenFClick(Sender: TObject);
begin
  Xls_To_StringGrid(DE_XLS, ExcelE.Text, 'DE');
  Xls_To_StringGrid(CH_XLS, ExcelE.Text, 'CH');
end;
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 06:39 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 by Thomas Breitkreuz