Einzelnen Beitrag anzeigen

Benutzerbild von Moombas
Moombas

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

AW: Excel mit mehreren Sheets öffnen

  Alt 7. Sep 2018, 08:04
Moin zusammen,

ich bin es nochmal und habe einen kleinen Nachtrag/Nachfrage, die am besten hierzu passt.
Der Fehler der Exceltabelle beim Einlesen lag immer dann vor, wenn ein Filter gesetzt war und dieser bestimmte Zeilen raus gefiltert hat (ich war davon ausgegangen, das der Filter nur eine optische Geschichte ist und im Hintergrund die Tabelle ja noch alle Daten hat).

Weiß jemand wie ich den Filter über Delphi entferne? Das was in den Excel-Makros drin steht, hat leider nicht geholfen, denn wenn ich "Sheet.ShowAllData;" aktiviere kann mein Programm die Exceldatei nicht mehr einlesen (keine Fehlermeldung, das Stringgrid bleibt einfach leer). Ich würde damit gerne diese Fehlerquelle ausschließen.

Delphi-Quellcode:
function XLS_To_StringGrid(AXLApp : OleVariant; AGrid : TStringGrid; Sheetname : string):Boolean;
const
  xlCellTypeLastCell = $0000000B;
var
  Sheet: OLEVariant;
  RangeMatrix: Variant;
  X, Y, K, R: Integer;
begin
  Result := False;
  try
    try
      if (Sheetname <> '') and HaveSheet(AXLApp, Sheetname) then begin
        Sheet := AXLApp.WorkSheets[Sheetname];
      end else
        Sheet := AXLApp.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.select;
// Sheet.ShowAllData;
      Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
      X := AXLApp.ActiveCell.Row; // Get the value of the last row
      Y := AXLApp.ActiveCell.Column; // Get the value of the last column

      // Set Stringgrid's row &col dimensions.
      AGrid.RowCount := X; // Excel-Row 1 = Header
      AGrid.ColCount := Y;

      RangeMatrix := Sheet.Range['A1', Sheet.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 := 0;
      While (K < X) do begin
        Inc(K);
        for r := 1 to y do
          AGrid.Cells[(R - 1), (K - 1)] := RangeMatrix[K, R];
      end;
      Result := True;
    except
      // Logging...
    end;
  finally
    RangeMatrix := Unassigned;
    Sheet := Unassigned;
  end;
end;

Geändert von Moombas ( 7. Sep 2018 um 08:09 Uhr)
  Mit Zitat antworten Zitat