Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Excel-Tabelle in Stringgrid einlesen! (https://www.delphipraxis.net/4365-excel-tabelle-stringgrid-einlesen.html)

Chriss 26. Apr 2003 09:46


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

Touchdown 26. Apr 2003 10:03

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.

Chriss 26. Apr 2003 10:13

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!

Darty 26. Apr 2003 10:39

Ich habe es zwar noch nicht ausprobiert, aber vielleicht hilft Dir dieses Posting Daten von Excel nach Delphi und Feldformatierungen für den Start weiter ... Oder dieses Posting in der auch einige Urls drin sind Text im StringGrid formatieren ...

Chriss 26. Apr 2003 10:45

hmm, hat mehr mit der gestaltung zu tun....
ich guck mal weiter

toms 26. Apr 2003 10:46

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;

Chriss 26. Apr 2003 11:11

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:

stoxx 30. Jan 2007 19:12

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!

Hansa 30. Jan 2007 19:17

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)

stoxx 30. Jan 2007 19:24

Re: Excel-Tabelle in Stringgrid einlesen!
 
Zitat:

Zitat von Hansa
Zahlentyp integer hört da auf. Hast Delphi-Version leider vergessen anzugeben. Wird wohl heutzutage als ShortInt deklariert sein. 8)


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 10:07 Uhr.
Seite 1 von 2  1 2      

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