AGB  ·  Datenschutz  ·  Impressum  







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

Excel import in stringgrid

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

Registriert seit: 14. Mai 2003
Ort: Gerstrhofen
1.112 Beiträge
 
Delphi 12 Athens
 
#6

Re: Excel import in stringgrid

  Alt 4. Okt 2009, 20: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.
Angehängte Grafiken
Dateityp: png excel2_113.png (1,9 KB, 37x aufgerufen)
Dateityp: png excel_151.png (1,9 KB, 34x aufgerufen)
  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 09:41 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