Autor Beitrag
MarioL
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23

Win XP, Win 7
Delphi 6
BeitragVerfasst: So 19.12.10 18:08 
Hallo, ich möchte eine ComboBox und 3 StringListen mit Daten aus einer Excel-Datei füllen und habe folgende Frage:

wie kann ich ein bestimmtes Sheet auswählen ?
und wie kann ich die letzte Zeile einer bestimmten Spalte (B) ermitteln ?

hier mein code:
(Teile davon sind von 'DBR Delphi-Ecke' www.s170867368.onlin...e/delphi/lastrow.php)


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:
var
  xcl : OleVariant;
  sl1, sl2, sl3 : TStringlist;

const
  xlCellTypeLastCell = 11;

//Laden aus Excel
procedure Laden(Datei: string; list: TStrings);
var i, r: integer;
begin
  list.clear;
  xcl := createOleObject('Excel.Application');
  xcl.Workbooks.Open(Datei);
  r := xcl.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Row; 
       //hier benötige ich die letzte Zeile von Spalte B (Spalte A enthält mehr Daten als Spalte B)
  for i := 3 to r do begin
    list.add(xcl.range['B' + inttostr(i)]);
    sl1.add(xcl.range['C' + inttostr(i)]);
    //sl2 ...
  end;
  xcl.quit;
end;

procedure TForm1.Button1Click(Sender: TObject);
var Datei: string;
begin
  if OpenDialog1.Execute then begin
    sl1 := TStringlist.create;
    //sl2 ...
    Datei := OpenDialog1.FileName;
    Laden(Datei, ComboBox1.items);
    ComboBox1.ItemIndex := 0;
    Edit1.Text := sl1.Strings[0];
    // ...
  end;
end;




MfG Mario

Moderiert von user profile iconMartok: Delphi-Tags gesetzt
MarioL Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23

Win XP, Win 7
Delphi 6
BeitragVerfasst: So 19.12.10 18:57 
Hallo, Problem 1 konnte ich lösen.


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
//Laden aus Excel
procedure Laden(Datei: string; list: TStrings);
var i, r: integer;
    s, x : string;
begin
  list.clear;
  xcl := createOleObject('Excel.Application');
  xcl.Workbooks.Open(Datei);
  for i := 1 to xcl.Sheets.Count do begin
    s := s + chr(13) + IntToStr(i) + ' - ' +xcl.Sheets[i].name
  end;
  InputQuery('Tabelle auswählen', s, x);
  r := xcl.Sheets[StrToInt(x)].Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Row;
  for i := 3 to r do begin
    list.add(xcl.Sheets[StrToInt(x)].range['B' + inttostr(i)]);
    sl1.add(xcl.Sheets[StrToInt(x)].range['C' + inttostr(i)]);
    //sl2 ...
  end;
  xcl.quit;
end;




MfG Mario

Moderiert von user profile iconMartok: Delphi-Tags gesetzt
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: So 19.12.10 19:12 
Hallo!

Bitte verwende für Quellcode die entsprechenden [delphi]-Tags, dann wird er "schöner" dargestellt als im Fließtext. Beispiel:
ausblenden Quelltext
1:
<span class="inlineSyntax"><span class="codecomment">{PROTECTTAGd6f0893982771bbe9faced57b60e8924}</span></span>					

Wird:
ausblenden Delphi-Quelltext
1:
var Test: integer;					


Ich habe das mal für dich gemacht, also für die Zukunft dann ;)

Viele Grüße,
Martok

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
Gerd Kayser
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 632
Erhaltene Danke: 121

Win 7 32-bit
Delphi 2006/XE
BeitragVerfasst: So 19.12.10 19:40 
user profile iconMarioL hat folgendes geschrieben Zum zitierten Posting springen:
wie kann ich ein bestimmtes Sheet auswählen ?
und wie kann ich die letzte Zeile einer bestimmten Spalte (B) ermitteln ?
Siehe hier:
www.ozgrid.com/forum...p?t=65705&page=1
www.djpate.freeserve.co.uk/Automation.htm
Chemiker
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 194
Erhaltene Danke: 14

XP, Vista 32 Bit, Vista 64 Bit, Win 7 64 Bit
D7, BDS 2006, RAD Studio 2009+C++, Delphi XE2, XE3, VS 2010 Prof.
BeitragVerfasst: So 19.12.10 22:50 
Hallo MarioL,

hat jetzt nicht direkt was mit Deinem Problem zu tun, aber nach den:

ausblenden Delphi-Quelltext
1:
xcl.quit;					

sollte unbedingt noch ein:

ausblenden Delphi-Quelltext
1:
xcl:= Unassigned;					

folgen. Sonst bleibt die Excel-Instanz weiter im Speicher. Kann man schön beobachten, wenn man nach dem Schließen in den Taskmanger schaut.

Bis bald Chemiker