![]() |
Excel-Tabelle in Stringgrid einlesen!
Liste der Anhänge anzeigen (Anzahl: 1)
Hi @ all!!
Ich habe folgendes vor: Ich habe ein Excel-Tabelle mit vorgegebenen Werten (so weit, so gut). DIESE tabelle soll nun mit Start des Programms in ein Stringrid eingelesen werden! und zwar OHNE die Spaltenüberschriften! WICHTIG ist auch, dass die Formate beibehalten werden müssen! Ich habe die Tabelle angehängt, damit ihr damit auch was anfangen könnt! (Die rote zeile gibt es später nicht) Ich habe schon mit memos und so weiter alles durchprobiert, funktionieren tuts, aber ich will hinterher mit den zahlen aus der tabelle auch noch rechnen!! DANKE CHRISS |
Möglich ist es auf jeden Fall, ich selbst hab mal sowas gemacht.
Damals hat mir googeln nach 'Delphi' und 'Excel' vollkommen ausgereicht ... Auch wenn man sich in die Materie etwas einarbeiten muss, es gibt recht brauchbare Hilfen im Internet. Dir alles zu erklären ist viel zu aufwendig, Detailfragen könnten wir hier sicher klären. |
hab ich teils schon versucht, das ergebnis war genau das gegenteil: von einem stringgrid in eine tabelle!!
ich möchte auch nur wissen, wie ich es anstelle, dass die zeilen und spalten in die stringgrid übernommen werden! |
Ich habe es zwar noch nicht ausprobiert, aber vielleicht hilft Dir dieses Posting
![]() ![]() |
hmm, hat mehr mit der gestaltung zu tun....
ich guck mal weiter |
Hab mal eine Funktion Xls_To_StringGrid() geschrieben:
Code:
uses
ComObj; 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; procedure TForm1.Button1Click(Sender: TObject); begin if Xls_To_StringGrid(StringGrid1, 'C:\Tabelle.xls') then ShowMessage('Table exported!'); end; |
UFF *platt sei* :coder:
da braucht man ja nen doktor für... erstmal danke, mal gucken, was sich damit anstellen lässt *fg*! :coder: :coder: |
wenn in der Excel Datei Worksheets enthalten sind, die bis zur letzten Zeile ( 65536) gefüllt sind, stürzt der Code ab bei:
Delphi-Quellcode:
Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
Mit der Meldung: " Die Activate-Methode des Range-Objektes konnte nicht ausgeführt werden." Weiß jemand warum ? besten Dank! |
Re: Excel-Tabelle in Stringgrid einlesen!
Zahlentyp integer hört da auf. Hast Delphi-Version leider vergessen anzugeben. Wird wohl heutzutage als ShortInt deklariert sein. 8)
|
Re: Excel-Tabelle in Stringgrid einlesen!
Zitat:
hmm .. versteh ich jetzt nich ganz, bis zu dieser Zeile wird doch noch gar nix mit integern gemacht .. Sind ja nur 4 effektive Zeilen mit Excel .... D2006 hab ich.
Delphi-Quellcode:
XLApp := CreateOleObject('Excel.Application');
try XLApp.Visible := False; XLApp.Workbooks.Open(AXLSFile); Sheet := XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets[1]; Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 06:18 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