Autor Beitrag
fo3tus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22



BeitragVerfasst: Mo 04.06.07 11:35 
hi zusammen
ich lese Daten aus einer Textdatei aus und möchte diese in eine ExcelTabelle schreiben...

habe sie zuvor in eine Textdatei geschrieben und alles durch Kommas voneinander getrennt. als ich die Daten nun in eine ExcelTabelle schreiben wollte, schrieb er mir alles jeweils in die erste Zelle statt jedes Word/Zeichenkette in eine Zeile zu schreiben...

jetzt müsst ich, glaub ich zumindest, einfach Komma durch TABs ersetzen...aber wie...!?
kann mir wer helfen?

danke im voraus

lg
webbi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 142

Win XP Pro SP2, Debian GNU/Linux 2.6
D5 Pro, D7 Pro, BDS2006, Kylix3, MS-VS 2005
BeitragVerfasst: Mo 04.06.07 14:56 
Diese Funktion dürfte dir helfen:
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:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
function StrTran(sStr, sFind, sReplace: String; iStart :Longint=0; iNum: LongInt = 0): String;
var
  bAll: Boolean;
  iPos : LongInt;
  iCtr : LongInt;
  iRep : LongInt;

  function Stuff( sSource: String; iStart, iDel: LongInt; sInsert: String):String;
  begin
    Result := '';

       { Do we have characters to be deleted?  If so, do it! }
    if iDel > 0 then Delete(sSource, iStart, iDel);

       { Same with characters to insert - are there any? }
    if sInsert <> '' then
    begin
      SetLength(Result, Length(sSource) + Length(sInsert));
      Insert( sInsert, sSource, iStart );
    end;

       { Return the altered string }
    Result := sSource;
  end;

  function Substr( sStr: String; iStart, iCount: LongInt) : String;
  begin
    Result := '';
     {  Is the starting position past the end of the string?  If so,
        handle the same way as Clipper - return a null string  }

    if (iStart > Length(sStr)) then exit;

     { If zero iCount passed, they want the rest of string from iStart.  If
       a negative number is passed, we'll default to zero. }

    if (iCount < 1) then iCount := Length(sStr);

    Result := Copy( sStr, iStart, iCount );
  end;

begin
  Result := '';
  bAll := (iNum = 0);
  iPos := Pos(sFind, sStr);
  if iPos = 0 then
  begin
    Result := sStr;
    exit;
  end;
  if Length(sReplace) > Length(sFind) then iRep := Length(sReplace) - Length(sFind)
  else if Length(sReplace) = Length(sFind) then  iRep := 0
  else iRep := Length(sReplace);

  if (iStart = 1) then
  begin
    sStr := Stuff( sStr, iPos, Length(sFind), sReplace );
    if iNum = 1 then
    begin
      Result := sStr;
      exit;
    end;
    AppendStr(Result, SubStr(sStr, 1, iPos + iRep));
    sStr := SubStr(sStr, iPos + iRep + 1, 0);
  end
  else
  begin
    for iCtr := 1 to (iStart - 1) do
    begin
      AppendStr(Result, SubStr( sStr, 1, iPos + iRep));
      sStr := SubStr( sStr, iPos + iRep + 1, 0 );
      iPos := Pos( sFind, sStr );
    end;
  end;

  if (bAll) then
  begin
    iPos := Pos( sFind, sStr );
    while iPos > 0 do
    begin
      sStr := Stuff( sStr, iPos, Length(sFind), sReplace );
      AppendStr( Result, SubStr( sStr, 1, iPos + iRep));
      sStr := SubStr( sStr, iPos + iRep + 1, 0);
      iPos := Pos(sFind, sStr);
    end;
    AppendStr( Result, sStr );
  end
  else
  begin
    { If we started with 1st find, we've done one replace already...}
    if iStart = 1 then iCtr := 1 else iCtr := 0;
    iPos := Pos( sFind, sStr );
    while True do
    begin
      if iPos = 0 then break;
      sStr := Stuff( sStr, iPos, Length(sFind), sReplace );
      AppendStr( Result, SubStr( sStr, 1, iPos + iRep));
      sStr := SubStr( sStr, iPos + iRep + 1, 0);
      Inc( iCtr );
      if iCtr = iNum then break;
      iPos := Pos( sFind, sStr );
    end;
    AppendStr( Result, sStr );
  end;
end;

_________________
Programmieren ist der Wettkampf zwischen Programmierer
die immer noch einfachere Programme schreiben und Anwender die immer noch dümmer werden...


Zuletzt bearbeitet von webbi am Mo 04.06.07 15:04, insgesamt 1-mal bearbeitet
fo3tus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22



BeitragVerfasst: Mo 04.06.07 15:01 
ja ich weiß...sehr chaotisch und meine Kenntnisse sind auch noch nicht die besten...
sry, dafür...

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:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
unit main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ShellApi, ubZipUtils, ubFILESUtils;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Button1: TButton;
    Label2: TLabel;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    x:string;
    error:string;

end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

PROCEDURE TForm1.Button1Click(Sender: TObject);

     VAR sl_commandos, sl_dat, sl_currLns: TStringList;

     VAR i, j, l, posi, m, n, o: Integer;

     VAR currLn, ID, id_zs, Kommando, kommando_zs, StartTime, st_zs, EndTime, et_zs, NR, nr_zs: STRING;

     VAR StartZeit, EndZeit, endevorlaeufer, Laufzeit: TDateTime;

     VAR result: TextFile;

     VAR temp, s1, s2, s3, hilf, punkt, noData, RunTime, endtime_vorlaeufer, id_vorlaeufer: String;

     VAR anz_dat, anz_lns, anz_kommandos: Integer;

     VAR exit: Boolean;

BEGIN
     punkt:='.';
     exit:=True;
         // TStringList erzeugen, anschließend füllen
     TRY
        sl_commandos:=TStringList.Create;
        sl_commandos.LoadFromFile('C:\D-Dateien\commandos.txt');

     TRY
        sl_dat:=TStringList.Create;
          // TStringList bekommt Namen der zip-Dateien als Werte
     fGetfiles(sl_dat,'C:\D-Dateien','*.zip', true,false);

     anz_dat:=sl_dat.count-1;

     AssignFile(result, 'C:\D-Dateien\Auswertung\results_runtime-'+DateTimeToStr(Date)+'.xls');

     IF FileExists('C:\D-Dateien\Auswertung\results_runtime-'+DateTimeToStr(Date)+'.xls') = True THEN
     BEGIN
          Rewrite(result);
          writeln(result, 'VSTKNR,Kommando,ID,Startzeit,Endzeit,Laufzeit');
     END
     ELSE
     BEGIN
          Rewrite(result);
          writeln(result, 'VSTKNR,Kommando,ID,Startzeit,Endzeit,Laufzeit');
     END;

     FOR i:=0 TO anz_dat DO
     BEGIN
              // entpackt nur die d1-Dateien
          fbUnZipSingleFile('*.d1', sl_dat[i], 'C:\D-Dateien\d1-dateien\', x, error);

          TRY
             sl_currLns:=TStringList.Create;
                     // füllen der TStringList mit Inhalt der d1-Dateien
             sl_currLns.LoadFromFile('C:\D-Dateien\d1-dateien\'
                + ChangeFileExt(ExtractFileName(sl_dat[i]), '.d1'));


             anz_lns:=sl_currLns.count-1;

             anz_kommandos:=sl_commandos.count-1;

             Application.ProcessMessages;   //zum ungestörten weiter arbeiten während Ausführung

             // VSTKNR aus Dateinnamen filtern und formatieren
             nr_zs:=ExtractFileName(sl_dat[i]);
             hilf:=Copy(nr_zs, 0, 9);
             IF pos(punkt, Copy(hilf,9,1)) > 0 THEN
             BEGIN
                  nr_zs:=Copy(hilf, 0, 8);
                  // Format: . zu -
                  temp:=Copy(nr_zs, 0, 3);
                  hilf:=temp+'--';
                  temp:=Copy(nr_zs, 4, 4);
                  hilf:=hilf+temp+'-';
                  temp:=Copy(nr_zs, 8, 1);
                  NR:=hilf+temp;
             END
             ELSE IF pos(punkt, Copy(hilf,8,1)) > 0 THEN
                  BEGIN
                       nr_zs:=Copy(hilf, 0, 7);
                       temp:=Copy(nr_zs, 0, 3);
                       hilf:=temp+'--';
                       temp:=Copy(nr_zs, 4, 3);
                       hilf:=hilf+temp+'--';
                       temp:=Copy(nr_zs, 7, 1);
                       NR:=hilf+temp;
                  END
                  ELSE
                  BEGIN
                       nr_zs:=Copy(hilf, 0, 6);
                       temp:=Copy(nr_zs, 0, 3);
                       hilf:=temp+'--';
                       temp:=Copy(nr_zs, 4, 2);
                       hilf:=hilf+temp+'---';
                       temp:=Copy(nr_zs, 6, 1);
                       NR:=hilf+temp;
                  END;

             //try
             FOR l:=0 TO anz_kommandos DO
             BEGIN
                  FOR j:=0 TO anz_lns DO
                  BEGIN
                       Application.ProcessMessages;   //zum ungestörten weiter arbeiten während Ausführung

                       IF pos(sl_commandos[l], sl_currLns[j]) > 0 THEN
                       BEGIN
                                     // Substring ausschneiden
                            currLn:= Copy(sl_currLns[j],0,Length(sl_commandos[l]));

                            kommando_zs:=currLn;
                            IF(kommando_zs = Kommando) = False THEN
                            BEGIN
                                 Kommando:=kommando_zs;
                                 exit:=False;


                            currLn:= Copy(sl_currLns[j-3],55,18);
                            IF (currLn = st_zs) = False THEN
                            BEGIN
                                 st_zs:=currLn;
                                 StartTime:=st_zs;

                                 temp:=copy(StartTime,7,2);
                                 s1:=temp+'.';
                                 temp:=copy(StartTime,4,2);
                                 s1:=s1+temp+'.';
                                 temp:=copy(StartTime,1,2);
                                 s1:=s1+temp+' ';
                                 temp:=copy(StartTime,10,9);
                                 s1:=s1+temp;
                                 StartZeit:=StrToDateTime(s1);

                                 temp:='';

                            end;
                            END;

                            {currLn:= Copy(sl_currLns[j+2],0,46);
                            IF (currLn = 'NO (MORE) DATA FOR DISPLAY AVAILABLE.') = True THEN
                            BEGIN
                                 et_zs:=StartTime;
                                 noData:=Copy(sl_currLns[j+2],0,46);
                            END;}


                            currLn:= Copy(sl_currLns[j-2],0,4);
                            IF(currLn = id_zs) = False THEN
                            BEGIN
                                 id_zs:=currLn;
                                 ID:=id_zs;
                            END;

                       END;

                       try

                       if exit=False Then
                       begin
                       //Endzeit ermitteln
                       if pos('END JOB '+ID, sl_currLns[j]) > 0 then
                       begin
                            for m:=j downto 0 do
                            begin
                                 if pos(ID, copy(sl_currLns[m],0,4)) > 0 then
                                 begin
                                      EndTime:=Copy(sl_currLns[m-1],55,18);

                                      temp:=copy(EndTime,7,2);
                                      s2:=temp+'.';
                                      temp:=copy(EndTime,4,2);
                                      s2:=s2+temp+'.';
                                      temp:=copy(EndTime,1,2);
                                      s2:=s2+temp+' ';
                                      temp:=copy(EndTime,10,9);
                                      s2:=s2+temp;
                                      EndZeit:=StrToDateTime(s2);
                                      temp:='';

                                      break;
                                 end;
                            end;

                            
                            for n:=j downto 0 do
                            begin
                                 if pos('END JOB ', sl_currLns[n]) > 0 then
                                 begin
                                      id_vorlaeufer:=copy(sl_currLns[n],9,4);

                                      for o:=n downto 0 do
                                      begin
                                           if pos(id_vorlaeufer, copy(sl_currLns[o],0,4)) > 0 then
                                           begin
                                                endtime_vorlaeufer:=copy(sl_currLns[o-1],55,18);

                                                temp:=copy(endtime_vorlaeufer,7,2);
                                                s3:=temp+'.';
                                                temp:=copy(endtime_vorlaeufer,4,2);
                                                s3:=s3+temp+'.';
                                                temp:=copy(endtime_vorlaeufer,1,2);
                                                s3:=s3+temp+' ';
                                                temp:=copy(endtime_vorlaeufer,10,9);
                                                s3:=s3+temp;
                                                endevorlaeufer:=StrToDateTime(s3);
                                                temp:='';

                                                exit:=True;
                                                //break;
                                           end;
                                      end;
                                      if exit=True then
                                      begin
                                           break;
                                      end;
                                 end
                                 else
                                 begin
                                      if pos('END TEXT JOB '+ID, sl_currLns[n]) > 0 then

                                           for o:=n downto 0 do
                                           begin
                                                if pos(ID, copy(sl_currLns[o],0,4)) > 0 then
                                                begin
                                                     endtime_vorlaeufer:=copy(sl_currLns[o-1],55,18);
                                                     temp:=copy(endtime_vorlaeufer,7,2);
                                                     s3:=temp+'.';
                                                     temp:=copy(endtime_vorlaeufer,4,2);
                                                     s3:=s3+temp+'.';
                                                     temp:=copy(endtime_vorlaeufer,1,2);
                                                     s3:=s3+temp+' ';
                                                     temp:=copy(endtime_vorlaeufer,10,9);
                                                     s3:=s3+temp;
                                                     endevorlaeufer:=StrToDateTime(s3);
                                                     temp:='';

                                                     exit:=True;
                                                     //break;
                                                end;
                                           end;
                                           if exit=True then
                                           begin
                                                break;
                                           end;
                                      end;
                                 end;
                            end;

                       end;

                       except
                            on EConvertError do RunTime:=' FEHLER!!!';
                       end;


                       //Laufzeit errechnen
                       if RunTime <> ' FEHLER!!!' then
                       begin
                       if EndZeit <> 0 then
                       begin
                            RunTime:=FormatDateTime('hh:nn:ss', EndZeit-endevorlaeufer);
                            StartTime:=DateTimeToStr(StartZeit);
                            EndTime:=DateTimeToStr(EndZeit);
                            //showmessage(EndTime);
                       end;
                       end;
                       
                  END;

                  IF Kommando = sl_commandos[l] THEN
                  BEGIN
                       Write(result, NR+',');
                       Write(result, Kommando+',');
                       Write(result, ID+',');
                       Write(result, StartTime+',');
                       Write(result, EndTime+',');
                       WriteLn(result, RunTime);
                       //writeln(result, '   '+ noData);
                       noData:='';
                       //WriteLn(result, ' ');
                       RunTime:='';

                  END;
             END;

             {except
                   //HandleException;
                   on EConvertError do showmessage(Kommando);
             end;}


          FINALLY
                 freeandnil(sl_currLns);
          END;

     END;

     CloseFile(result);

         // Speicher wieder freigegeben
     FINALLY
          freeandnil(sl_dat);
     END;

     FINALLY
          freeandnil(sl_commandos);
     END;

     END;


lg

Moderiert von user profile iconraziel: Code- durch Delphi-Tags ersetzt
webbi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 142

Win XP Pro SP2, Debian GNU/Linux 2.6
D5 Pro, D7 Pro, BDS2006, Kylix3, MS-VS 2005
BeitragVerfasst: Mo 04.06.07 15:08 
Und dann natürlich noch
ausblenden Delphi-Quelltext
1:
StrTran(cText, ',', #9)					

_________________
Programmieren ist der Wettkampf zwischen Programmierer
die immer noch einfachere Programme schreiben und Anwender die immer noch dümmer werden...