Autor Beitrag
Gintonik
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 66

Win MCE 2005
Delphi7 Enterpr.
BeitragVerfasst: Do 07.12.06 16:13 
Hallo,

wie kann ich Daten aus einer bestehenden Excel-Tabelle in Delphi einbinden bzw. womit sollte ich die Daten in Delphi verabeiten?

In einer Listbox oder gibt es noch bessere Möglichkeiten ?

Thx for every Post...
Jakob Schöttl
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 929
Erhaltene Danke: 1


Delphi 7 Professional
BeitragVerfasst: Do 07.12.06 17:58 
Also, es gibt TStringGrid, mit dem du Tabellen bearbeiten kannst, aber es ist total schwierig eine Xls-Datei dort hinein zu laden, weil eine Excel-Tabelle glaub ich eine komprimierte XML-Datei ist. Es ist also ersten schwierig die zu parsen, und du weißt ja den komprimierungsalgorithmus, und außerdem darfst du die Formatierungen und Funktionen nicht vergessen.

Also empfehle ich dir bei Delphi7 "TExcelApplication" für Excel 2000, Registerkarte "Server" (ich glaub das heißt so). Damit kannst du mit der Excel-API die Datei bearbeiten.
espen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 90
Erhaltene Danke: 1


D6 Prof./D7 Prof. MSSQL, MySQL
BeitragVerfasst: Mo 11.12.06 13:59 
wurzel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 75

Win 2000, Win XP
BDS 2006 (Delphi, Delphi .NET, C++)
BeitragVerfasst: Fr 22.12.06 09:13 
user profile iconespen hat folgendes geschrieben:
Gruezi...
www.swissdelphicente...showcode.php?id=1728


Das funzt net, da kommt folgende Meldung wenn ich auf den Button klicke

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

Es wird dann auf diese Zeile im Delphi hingewiesen

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
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.Button2Click(Sender: TObject);
begin
  if Xls_To_StringGrid(StringGrid1, 'C:\Mappe1.xls'then
    ShowMessage('Table has been exported!');
end;

_________________
"Könntest du deinen Egotrip mal kurz unterbrechen? Es ist was wichtiges passiert." - "Wenn es irgendwas wichtigeres als mein Ego gibt, verlange ich, dass man es auf der Stelle verhaftet und erschießt."
uwewo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 154



BeitragVerfasst: Fr 22.12.06 10:49 
Hast Du Excel installiert und wird es gestartet?
wurzel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 75

Win 2000, Win XP
BDS 2006 (Delphi, Delphi .NET, C++)
BeitragVerfasst: Fr 22.12.06 12:07 
Klar hab ich excel installiert. Und es wird garnichts gestartet, es kommt nur gleich ne meldung von excel, ob ich das dokument speichern will. aber ich sehe garkein dokument

_________________
"Könntest du deinen Egotrip mal kurz unterbrechen? Es ist was wichtiges passiert." - "Wenn es irgendwas wichtigeres als mein Ego gibt, verlange ich, dass man es auf der Stelle verhaftet und erschießt."
uwewo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 154



BeitragVerfasst: Fr 22.12.06 13:38 
Teste mal den Anhang, kopiere die Mappe1.xls vorher in C:\
Einloggen, um Attachments anzusehen!
Gintonik Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 66

Win MCE 2005
Delphi7 Enterpr.
BeitragVerfasst: Di 27.03.07 16:41 
Super Jungs,

ich habe einfach die Zeilen kopiert und es hat auf Anhieb gefunzt. Ich habe Excel installiert.
THX für den Code...

_________________
Man sagt, er habe magische Kräfte!