AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren

Excel import in stringgrid

Ein Thema von youuu · begonnen am 12. Jul 2009 · letzter Beitrag vom 4. Okt 2009
Antwort Antwort
youuu

Registriert seit: 2. Sep 2008
Ort: Kleve
822 Beiträge
 
Delphi 2010 Professional
 
#1

Excel import in stringgrid

  Alt 12. Jul 2009, 16:51
Hi,

ich möchte eine Excel Tabelle in ein Stringgrid importieren und verwende den Code von SwissDelphi

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

    // Open the Workbook
    XLApp.Workbooks.Open(AXLSFile);

    // Sheet := XLApp.Workbooks[1].WorkSheets[1];
    Sheet := XLApp.Workbooks[ExtractFileName(AXLSFile)].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;
    // 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.ColCount := 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 := 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;
Allerdings bekomme ich immer wieder den Fehler

Erste Gelegenheit für Exception bei $76F142EB. Exception-Klasse EOleException mit Meldung 'Die Activate-Methode des Range-Objektes konnte nicht ausgeführt werden'. Prozess ExcelTostringgrid.exe (8108)
Steven
  Mit Zitat antworten Zitat
GHorn

Registriert seit: 24. Mär 2009
Ort: NRW
124 Beiträge
 
#2

Re: Excel import in stringgrid

  Alt 13. Jul 2009, 10:12
Habe mal gerade eine kleine Test-App geschrieben.
Läuft ohne Fehlermeldung durch und lädt die Daten ins
Stringgrid.

Excel 2003, D2007.

Wann / Wo kommt denn der Fehler (welche Zeile)?

Gruß,
Gerald
  Mit Zitat antworten Zitat
Blup

Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.429 Beiträge
 
Delphi 10.4 Sydney
 
#3

Re: Excel import in stringgrid

  Alt 15. Jul 2009, 09:16
Auf das Activate kann man verzichten, wenn mit UsedRange gearbeitet wird:

Delphi-Quellcode:
x := XLSheet.UsedRange.Columns.Count;
y := XLSheet.UsedRange.Rows.Count;
[edit=mkinzler]Delphi-Tag eingefügt Mfg, mkinzler[/edit]
  Mit Zitat antworten Zitat
angos

Registriert seit: 26. Mai 2004
Ort: Rheine
549 Beiträge
 
Delphi 11 Alexandria
 
#4

Re: Excel import in stringgrid

  Alt 15. Jul 2009, 11:25
Hi

ich hatte das gleiche Problem und habe mir mit einer bescheidenen Krücke geholfen: Die letzte Zelle selber step by step ermitteln

Das Problem tritt nicht mit jedem Excelfile auf (weis aber nicht wann und wann nicht)

Zitat von Blup:
Auf das Activate kann man verzichten, wenn mit UsedRange gearbeitet wird:
Kannst du mir sagen wozu das Activate da ist und ggf erklären warum man sich das dann sparen kann?

Grüße
Ansgar
Ansgar
  Mit Zitat antworten Zitat
Blup

Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.429 Beiträge
 
Delphi 10.4 Sydney
 
#5

Re: Excel import in stringgrid

  Alt 16. Jul 2009, 08:17
Excel erzeugt nur so viele Spalten und Zeilen wie tatsächlich benötigt werden, auch wenn mehr leere Spalten angezeigt werden. Ruft man Methoden von Zell-Objekten auf die nicht existieren, gibt es Zugriffsverletzungen. Um die tatsächlich vorhandenen Zeilen und Spalten zu ermitteln, wird in dem Beispiel Excel aufgefordert, die letzte vorhandene Zelle auszuwählen. Anschließend wird die Position der aktiven Zelle ermittelt.

Sicherer ist es direkt den genutzten Bereich (UsedRange) des XLSheet abzufragen.
Dieses Range-Objekt kann man auch sofort nutzen, um an die Daten der Zellen zu gelangen.
In dem Beispiel wird dafür erst ein neues Range-Objekt (RangeMatrix) erzeugt.
  Mit Zitat antworten Zitat
Kostas

Registriert seit: 14. Mai 2003
Ort: Gerstrhofen
1.058 Beiträge
 
Delphi 10 Seattle Enterprise
 
#6

Re: Excel import in stringgrid

  Alt 4. Okt 2009, 21:58
Hallo Zusammen,

auch ich beschäftige mich gerade mit dem Beispiel von SwissDelphi.
Normale Execl files lassen sich damit recht einfach ansprechen.
Aktuell habe ich ein file das schön Bund gemischt einzelen Zellen zusammenverbunuden sind.
Das problem ist nun, das nicht elle Zellen ausgelesen werdne können.
Ich kann das Excel-File leider nicht als csv wegschreiben, denn ich benötige auch die Farbinformation
einzelner Zellen. Also nicht nur die Zelleninhalte sonder auch die Attribute. Ich muss also
aus das excel-File zugreifen können.

ich habe es mit XLApp.ActiveCell... Sheet.UsedRange.Columns...
hilft beides nicht.

Generell mal die Frage, gibt es eine Möglichkeit auf alle Zellen des Sheets zuzugreifen?
Im screen shot ist eine einfaches excel sheet aufgebaut. Sobald zwei Zellen zusammen verbunden sind
funktioniert es nicht mehr. Das Grid wird nicht vollständig aufgebaut und es kommt eine Fehlermeldung
das Excel eine Problem verursacht hat.

Hat jemand eine Idee?



Das Ist meine source.
Delphi-Quellcode:
function TForm1.Xls_To_StringGrid(AGrid: TStringGrid; AXLSFile: string): Boolean;
const
  xlCellTypeLastCell = $0000000B;
var
  XLApp, Sheet: OLEVariant;
  RangeMatrix: Variant;
  x, y, k, r: Integer;
begin
  Result := False;
  // Create Excel-OLE Object
  XLApp := CreateOleObject('Excel.Application');
  try
    // Hide Excel
    XLApp.Visible := False;

    // Open the Workbook
    XLApp.Workbooks.Open(AXLSFile);

    // Sheet := XLApp.Workbooks[1].WorkSheets[1];
// bsm(XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets.Count);
    Sheet := XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets[1];
// bsm(Sheet.name);

    // 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;

    x := Sheet.UsedRange.Columns.Count;
    y := Sheet.UsedRange.Rows.Count;

    // Set Stringgrid's row &col dimensions.

    AGrid.RowCount := x;
    AGrid.ColCount := 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 := 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;
Gruß Kostas.
Miniaturansicht angehängter Grafiken
excel2_113.png   excel_151.png  
  Mit Zitat antworten Zitat
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

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 16:03 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz