Autor Beitrag
cromos
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 219


Delphi 7
BeitragVerfasst: Sa 23.08.03 16:42 
Hallo.

ich möchte den Inhalt eines ListView in einer CSV-Datei schreiben, damit Exel diese Lesen kann.

mit:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
 index:=ListView1.Items.Count - 1;
      For il := 0 to Index do
        begin
          TestStr := ListView1.Items.Item[il].Caption + ';' 
           + Listview1.Items.Item[il].SubItems[0] + ';' 
           + Listview1.Items.Item[il].SubItems[1] + ';' 
           + Listview1.Items.Item[il].SubItems[2] + ';'
           + Listview1.Items.Item[il].SubItems[3] + ';'
           + Listview1.Items.Item[il].SubItems[4];
          
          SaveStringToFile(ProgrammOrdner + 'test.csv', Teststr);
          label6.Caption := TestStr;
          index := Index +1;
        end;

schreibt er immer nur den letzten Eintrag rein.

Wo ist der Fehler?

Moderiert von user profile iconTino: Titel geändert; Delphi-Tags hinzugefügt.


Zuletzt bearbeitet von cromos am Di 26.08.03 00:22, insgesamt 1-mal bearbeitet
MSCH
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1448
Erhaltene Danke: 3

W7 64
XE2, SQL, DevExpress, DevArt, Oracle, SQLServer
BeitragVerfasst: Sa 23.08.03 17:07 
Titel: Re: For??
cromos hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
 index:=ListView1.Items.Count - 1;
-->      For il := 0 to Index do
-->          index := Index +1; wech damit !!
        end;


anders:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
  asignFile(F,'Test.csv');
     For il := 0 to istView1.Items.Count - 1 do begin
       writeln(F,...,...,....,);
     end;
  Closefile(F);

1. schau dir mal dein code an !
2. siehe die Ergänzung

grez
msch

Moderiert von user profile iconTino: Delphi-Tags hinzugefügt.
cromos Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 219


Delphi 7
BeitragVerfasst: Sa 23.08.03 19:15 
Keine Ahnung was Du meinst. Werde nicht wirklich schlau daraus.
Was soll denn da anders ein?

Was meinst du mit

Zitat:

writeln(F,...,...,....,);


Was kommt da hin?
Als was wird F Deklariert? In der OH steht das es String sein muss.
Bekomme aber ein Fehler "Inkompatible Datentypen"

:?:
MSCH
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1448
Erhaltene Danke: 3

W7 64
XE2, SQL, DevExpress, DevArt, Oracle, SQLServer
BeitragVerfasst: Sa 23.08.03 20:01 
erstens erhöhst du die ForSchleifenendbedingung "Index" innerhalb der Schleife. Zweitens sicherst du nur einen String "Teststring" via unbekannter Procedure SaveStringToFile(). Imho überschreibst du damit immer den vorhergehenden Satz und behälst somit nur den letzten.

F ist eine Textvariable
Writeln (F, ...,..,..) sollte andeuten, dass du via WriteLn in die Textdatei anfügst und zwar den Text (....). also
writeln(F,ListView.Item.Caption,';',ListView.Item.SubItem[1]..u.s.w.).

ok?
grez
msch

_________________
ist das politisch, wenn ich linksdrehenden Joghurt haben möchte?
cromos Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 219


Delphi 7
BeitragVerfasst: So 24.08.03 12:04 
Also,
danach sollte es so richtig sein???!!!

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
var TestStr : String;
var F : TextFile;
var il : integer;
begin
ProgrammOrdner:=ExtractFilePath(application.ExeName);
TestStr := ListView1.Items.Item[il].Caption + ';' 
  + Listview1.Items.Item[il].SubItems[0] + ';' 
  + Listview1.Items.Item[il].SubItems[1] + ';' 
  + Listview1.Items.Item[il].SubItems[2] + ';'
  + Listview1.Items.Item[il].SubItems[3] + ';'
  + Listview1.Items.Item[il].SubItems[4];

assignFile(F,ProgrammOrdner + 'Test.csv');
     For il := 0 to ListView1.Items.Count - 1 do begin
       writeln(F,TestStr);
     end;
  Closefile(F);
end;


Tut aber nicht. Ausser

Exception
:?:

Moderiert von user profile iconTino: Delphi-Tags hinzugefügt.
Tryer
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 226
Erhaltene Danke: 7



BeitragVerfasst: So 24.08.03 12:13 
Vielleicht solltest Du "il" initialisieren.
Ansonsten ist die Aussage "nix ausser Exception" etwas schwach, es gibt nämlich mehr als eine ;)

MfG,
Tryer
cromos Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 219


Delphi 7
BeitragVerfasst: So 24.08.03 12:15 
Wenn ich es so mache:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
writeln(F,ListView1.Items.Item[il].Caption,';',
    Listview1.Items.Item[il].SubItems[0],';',
    Listview1.Items.Item[il].SubItems[1],';',
    Listview1.Items.Item[il].SubItems[2],';',
    Listview1.Items.Item[il].SubItems[3],';',
    Listview1.Items.Item[il].SubItems[4]
  );

bekomme ich auch ein Exception. E/A Fehler 103 oder sowas in der Art.

Wieso?

Moderiert von user profile iconTino: Delphi-Tags hinzugefügt.
MSCH
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1448
Erhaltene Danke: 3

W7 64
XE2, SQL, DevExpress, DevArt, Oracle, SQLServer
BeitragVerfasst: So 24.08.03 12:16 
es fehlt Rewrite(F) bzw. Append(F)

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
var TestStr : String
var F : TextFile; 
var il : integer; 
begin 
ProgrammOrdner:=ExtractFilePath(application.ExeName); 
assignFile(F,ProgrammOrdner + 'Test.csv'); 
rewrite(F); // überschreibt wenn vorhanden !!
For il := 0 to ListView1.Items.Count - 1 do begin 
  TestStr := ListView1.Items.Item[il].Caption + ';' 
    + Listview1.Items.Item [il].SubItems[0] + ';' 
    + Listview1.Items.Item[il].SubItems[1] + ';' 
    + Listview1.Items.Item[il].SubItems[2] + ';'
    + Listview1.Items.Item[il].SubItems[3] + ';'
    + Listview1.Items.Item[il].SubItems[4]; 
       writeln(F,TestStr); 
     end
  Closefile(F); 
end;


grez
msch

_________________
ist das politisch, wenn ich linksdrehenden Joghurt haben möchte?
cromos Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 219


Delphi 7
BeitragVerfasst: So 24.08.03 12:18 
Zitat:

Vielleicht solltest Du "il" initialisieren.


Was meinst Du damit?
cromos Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 219


Delphi 7
BeitragVerfasst: So 24.08.03 12:22 
Titel: Es hat Funktioniert
Ja, so klappt das ganze.

Also lag es nur an
ausblenden Quelltext
1:
rewrite(F);					



Vielen Dank für die Hilfe.

Gruss
Cromos
kat1
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 40

Win XP
D6 EntPr
BeitragVerfasst: Mo 25.08.03 18:54 
Ich wäre aber vorsichtig mit der Verwendung von ReWrite. Existiert das File schon, wird es einfach gelöscht, ohne dass Du eine Warnung erhältst, und überschrieben.

Vielleicht solltest Du noch eine Abfrage einbauen:

ausblenden Delphi-Quelltext
1:
2:
if FileExists('test.csv'then
 ....

und dort eine Fehlermeldung produzieren.

_________________
Mfg Uwe
Es ist eine Unsitte der Menschen, sich an alles zu gewöhnen.
cromos Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 219


Delphi 7
BeitragVerfasst: Di 26.08.03 00:21 
Zitat:

Ich wäre aber vorsichtig mit der Verwendung von ReWrite. Existiert das File schon, wird es einfach gelöscht, ohne dass Du eine Warnung erhältst, und überschrieben.


Ist in diesem Fall egal. Ich gehe ja über einen FileSave Dialog.

Gruss
Cromos