Entwickler-Ecke

Datenbanken - Problem beim einlesen einer Excel Datei mit Benutzerdef.Feld


Termi11 - Fr 23.05.08 16:38
Titel: Problem beim einlesen einer Excel Datei mit Benutzerdef.Feld
Hallo,

ich verwende folgende Anweisung, um eine Excel Datei auszulesen.
Doch leider hab ich bei einer Kolonne ein Problem.
Die Kolonne ist als "Benutzerdefiniert hh:mm", also Zeitangabe definiert.
Wenn ich also die Tabelle auslese, erhalte ich anstelle von zb. 06:00 Uhr 0.25 im Stringgrid.
Leider bin ich noch immer nicht so fit in Delphi, und hab den nötigen Tip noch nirgends gefunden. Eventuell könnte hier ja jemand mal nen Blick drauf werfen.
Das Feld ist a[5]. Auch ohne den Umweg über's Stringlist, also sofort in's Stringgrid geht's nicht.

Besten Dank im voraus!




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:
68:
69:
function Xls_To_StringGrid(AXLSFile: string): Boolean;
const
  xlCellTypeLastCell = $0000000B;
var
  XLApp, Sheet: OLEVariant;
  RangeMatrix: Variant;
  x, y, k, r,rowcount: Integer;
  A: TStringList;

begin
  Result := False;
  // Create Excel-OLE Object
  XLApp := CreateOleObject('Excel.Application');
  A := TStringList.Create;

  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[2];

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


    // 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 := 4;
    repeat
      for r := 2 to y do
        a.Add(RangeMatrix[K, R]);
        if (a[0] = form1.combobox2.text) then begin
          rowcount := form1.stringgrid1.RowCount;
          form1.StringGrid1.Cells[0,rowcount-1] := formatdatetime('dd-mm-yyyy',now);
          form1.StringGrid1.Cells[1,rowcount-1] := a[5];
          form1.StringGrid1.Cells[2,rowcount-1] := a[10];
          form1.stringgrid1.RowCount := rowcount+1;

        end;
      Inc(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;


Michael Stenzel - Fr 23.05.08 23:49

Hi Termi11.

Ich habe Dir eine Funktion geschrieben, die die Uhrzeiten umwandelt.


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
function ZeitStr(ExcelZeit : double) :string;
var Minute,Std,Sec : Integer;
begin
     ExcelZeit := Frac(ExcelZeit); //  falls vorhanden,Datum von Uhrzeit Trennen
     Std := Trunc(ExcelZeit*24); // 24 Std
     Minute := Trunc(ExcelZeit*1440-Std*60); // 1440 Min = 24 Std
     Sec := Trunc(ExcelZeit*86400-Std*3600-Minute*60); // 86400 Sec = 24 Std, 3600 Sec = 1 Std, 1 Min = 60 Sec
     Result := Format('%0.2d:%0.2d:%0.2d',[Std,Minute,Sec]);
     // Ohne Sekunden = Result := Format('%0.2d:%0.2d',[Std,Minute]);
end;




Benutzung auf eigene Gefahr :wink:

Michael.