| Autor |
Beitrag |
elduchte
      
Beiträge: 199
Win 2000, Win XP
Delphi 7
|
Verfasst: Do 27.02.03 16:46
Hallo,
bin so ziemlich neu was das Programmieren mit Delphi angeht, daher habe ich auch folgendes Problem:
Ich trage in einem TStringGrid Daten ein. Diese Daten möchte ich in eine Datei abspeichern und auch wieder laden können (zum ändern, erweitern etc). Auch sollen die Daten in tabellarischer form ausgedruckt werden können.
Ich habe schon so einiges probiert, bekomme es aber nicht hin. Ist das überhaupt möglich, oder habe ich einen Denkfehler? Kann mir jemand helfen?
Vielen Dank und Gruß
Andreas
|
|
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1
|
Verfasst: Do 27.02.03 16:55
StringGrid drucken (nicht von mir):
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:
| procedure GridDruck(grd:TStringGrid; links,oben, vSpalte,bSpalte,vZeile,bZeile:integer; scal:double; farbig:boolean); var x,y,li,ob,re,un,waag,senk,a:integer; fix,grund,schrift:TColor; r:Trect; function rech(i,j:integer):integer; begin result:=round(((i*j) / 72) * scal); end;
begin if vZeile < 0 then vZeile:=0; if vSpalte < 0 then vSpalte:=0; if (bZeile >= grd.rowcount)or(bZeile < 0) then bZeile:=grd.rowcount - 1; if (bSpalte >= grd.colcount)or(bSpalte < 0) then bSpalte:=grd.colcount - 1; if vZeile > bZeile then begin a:=vZeile;vZeile:=bZeile;bZeile:=a; end; if vSpalte > bSpalte then begin a:=vSpalte;vSpalte:=bSpalte;bSpalte:=a; end;
if (scal > 0)and(vZeile < grd.rowcount)and(vSpalte < grd.colcount) then begin if farbig then begin fix:=grd.fixedcolor; grund:=grd.color; schrift:=grd.font.color; end else begin fix:=clsilver; grund:=clwhite; schrift:=clblack; end; waag:=getdevicecaps(printer.handle,logpixelsx); senk:=getdevicecaps(printer.handle,logpixelsy); links:=rech(links,waag); oben:=rech(oben, senk); li:=getdevicecaps(printer.handle,physicaloffsetx)+1+links; a:=rech(3,waag); with printer do begin title:='Grid-Druck'; BeginDoc; if grd.gridlinewidth > 0 then begin canvas.pen.color:=$333333; canvas.pen.width:=1; canvas.pen.style:=pssolid end else canvas.pen.style:=psclear; canvas.font:=grd.font; canvas.font.color:=schrift; canvas.font.size:=round((grd.font.size / 0.72) * scal); for x:=vSpalte to bSpalte do begin ob:=getdevicecaps(printer.handle,physicaloffsety)+1+oben; re:=li+rech(grd.ColWidths[x]+1,waag); for y:=vZeile to bZeile do begin un:=ob+rech(grd.RowHeights[y]+1,senk); if (x < grd.fixedcols)or(y < grd.fixedrows) then canvas.brush.color:=fix else canvas.brush.color:=grund; canvas.rectangle(li,ob,re+2,un+2); r:=rect(li+a,ob+1,re-a,un-2); drawtext(canvas.handle,pchar(grd.Cells[x,y]),length(grd.Cells[x,y]), r,DT_SINGLELINE or DT_VCENTER); ob:=un; end; li:=re; end; enddoc; end; end; end; procedure TForm1.Button1Click(Sender: TObject); begin griddruck(stringgrid1,0,0,-1,-1,-1,-1,1,true); end; procedure TForm1.Button2Click(Sender: TObject); begin griddruck(stringgrid1,stringgrid1.left,stringgrid1.top,0,2,0,2,1,true); end; procedure TForm1.Button1Click(Sender: TObject); begin griddruck(stringgrid1,0,0,0,0,-1,-1,0.5,false); end; procedure TForm1.Button1Click(Sender: TObject); var s,z:integer; begin s:=stringgrid1.ColCount-1; z:=stringgrid1.RowCount-1; griddruck(stringgrid1,25,30,0,s div 2,0,z div 2,1,true); griddruck(stringgrid1,25,30,s div 2 + 1,s,0,z div 2,1,true); griddruck(stringgrid1,25,30,0,s div 2,z div 2 + 1,z,1,true); griddruck(stringgrid1,25,30,s div 2 + 1,s,z div 2 + 1,z,1,true); end; |
Laden / Speichern (auch nicht von mir):
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:
| procedure gridspeichern(grd:TStringGrid;Datei:string); var sl:TStringlist; x,y:integer; begin sl:=TStringlist.create; sl.add(inttostr(grd.colcount)); sl.add(inttostr(grd.rowcount)); for x:=0 to grd.ColCount-1 do for y:=0 to grd.RowCount-1 do sl.add(grd.cells[x,y]); for x:=0 to grd.ColCount-1 do sl.add(inttostr(grd.ColWidths[x])); for x:=0 to grd.RowCount-1 do sl.add(inttostr(grd.RowHeights[x])); sl.add(inttostr(grd.clientwidth)); sl.add(inttostr(grd.clientheight)); sl.add(inttostr(ord(grd.ScrollBars))); sl.savetofile(datei); sl.free; end; procedure gridladen(grd:TStringGrid;Datei:string;angleichen:boolean); var sl:TStringlist; x,y,z:integer; begin sl:=TStringlist.create; sl.loadfromfile(datei); grd.colcount:=strtoint(sl.strings[0]); grd.rowcount:=strtoint(sl.strings[1]); z:=2; for x:=0 to grd.ColCount-1 do for y:=0 to grd.RowCount-1 do begin grd.cells[x,y]:=sl.strings[z]; inc(z); end; if angleichen then begin for x:=0 to grd.ColCount-1 do begin grd.ColWidths[x]:=strtoint(sl.strings[z]); inc(z); end; for x:=0 to grd.RowCount-1 do begin grd.RowHeights[x]:=strtoint(sl.strings[z]); inc(z); end; grd.clientwidth:=strtoint(sl.strings[z]); grd.clientheight:=strtoint(sl.strings[z+1]); grd.ScrollBars:=TScrollStyle(strtoint(sl.strings[z+2])); end; sl.free; end;
procedure TForm1.Button1Click(Sender: TObject); begin gridspeichern(StringGrid1,'c:\test.grd'); end; procedure TForm1.Button2Click(Sender: TObject); begin gridladen(StringGrid1,'d:\test2.grd',true); end; |
Moderiert von DeCodeGuru: Code- durch Delphi-Tags ersetzt
|
|
Böser Borstel
      
Beiträge: 154
|
Verfasst: Di 20.01.04 16:05
Stringgrid drucken mit Seite am Ende, dann neue Seite mit Titelzeile
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:
| procedure GridDruck(grd:TStringGrid; links, oben: Integer; scal:double; farbig:boolean); var x, y, li, ob, re, un, waag, senk, a, vSpalte, bSpalte, vZeile, bZeile: integer; fix, grund, schrift: TColor; r: TRect;
function rech(i,j:integer):integer; begin result:=round(((i*j) / 72) * scal); end;
begin vZeile := 0; vSpalte := 0; bZeile := grd.rowcount - 1; bSpalte := grd.colcount - 1; if (scal > 0) and (vZeile < grd.rowcount) and (vSpalte < grd.colcount) then begin if farbig then begin fix := grd.fixedcolor; grund := grd.color; schrift := grd.font.color; end else begin fix := clsilver; grund := clwhite; schrift := clblack; end; waag := GetDeviceCaps(Printer.Handle, LogPixelSX); senk := GetDeviceCaps(Printer.Handle, LogPixelSY); links := rech(links, waag); oben := rech(oben, senk); li := GetDeviceCaps(Printer.Handle, PhysicalOffsetX) + 1 + links; a := rech(3, waag); with Printer do begin Title := 'Statistik'; Orientation := poLandscape; BeginDoc; if grd.gridlinewidth > 0 then begin Canvas.Pen.color := $333333; Canvas.Pen.width := 1; Canvas.Pen.Style := psSolid end else Canvas.Pen.Style := psClear; Canvas.Font := Grd.Font; Canvas.Font.Color := Schrift; Canvas.Font.Size := round((Grd.Font.Size / 0.72) * scal); ob := GetDeviceCaps(Printer.Handle, PhysicalOffsetY) + 1 + oben; for y := vZeile to bZeile do begin un := ob + rech(Grd.RowHeights[y]+1, senk); if (un > Printer.PageHeight) and (Printing) then begin EndDoc; BeginDoc; ob := GetDeviceCaps(Printer.Handle, PhysicalOffsetY) + 1 + oben; un := ob + rech(Grd.RowHeights[y]+1, senk); for x := vSpalte to bSpalte do begin Canvas.Brush.Color := fix; re := li + rech(Grd.ColWidths[x] + 1, waag); Canvas.Rectangle(li, ob, re + 2, un + 2); r := rect(li + a, ob + 1, re - a, un - 2); DrawText(Canvas.Handle, PChar(Grd.Cells[x,0]), length(Grd.Cells[x,0]), r, DT_SINGLELINE or DT_VCENTER); li := re; end; li := GetDeviceCaps(Printer.Handle, PhysicalOffsetX) + 1 + links; ob := un; end; un := ob + rech(Grd.RowHeights[y]+1, senk); for x := vSpalte to bSpalte do begin if (x < Grd.FixedCols) or (y < Grd.FixedRows) then Canvas.Brush.Color := fix else Canvas.Brush.Color := Grund; re := li + rech(Grd.ColWidths[x] + 1, waag); Canvas.Rectangle(li, ob, re + 2, un + 2); r := rect(li + a, ob + 1, re - a, un - 2); DrawText(Canvas.Handle, PChar(Grd.Cells[x,y]), length(Grd.Cells[x,y]), r, DT_SINGLELINE or DT_VCENTER); li := re; end; ob := un; li := GetDeviceCaps(Printer.Handle, PhysicalOffsetX) + 1 + links; end; if Printing then EndDoc; end; end; end; |
Moderiert von DeCodeGuru: Code- durch Delphi-Tags ersetzt
|
|
agent_x
      
Beiträge: 62
WIN 2000, Linux Kubuntu 6.06
|
Verfasst: So 09.09.07 05:40
Hallo,
wo soll denn das hier rein?::
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:
| procedure GridDruck(grd:TStringGrid; links,oben, vSpalte,bSpalte,vZeile,bZeile:integer; scal:double; farbig:boolean); var x,y,li,ob,re,un,waag,senk,a:integer; fix,grund,schrift:TColor; r:Trect; function rech(i,j:integer):integer; begin result:=round(((i*j) / 72) * scal); end;
begin if vZeile < 0 then vZeile:=0; if vSpalte < 0 then vSpalte:=0; if (bZeile >= grd.rowcount)or(bZeile < 0) then bZeile:=grd.rowcount - 1; if (bSpalte >= grd.colcount)or(bSpalte < 0) then bSpalte:=grd.colcount - 1; if vZeile > bZeile then begin a:=vZeile;vZeile:=bZeile;bZeile:=a; end; if vSpalte > bSpalte then begin a:=vSpalte;vSpalte:=bSpalte;bSpalte:=a; end;
if (scal > 0)and(vZeile < grd.rowcount)and(vSpalte < grd.colcount) then begin if farbig then begin fix:=grd.fixedcolor; grund:=grd.color; schrift:=grd.font.color; end else begin fix:=clsilver; grund:=clwhite; schrift:=clblack; end; waag:=getdevicecaps(printer.handle,logpixelsx); senk:=getdevicecaps(printer.handle,logpixelsy); links:=rech(links,waag); oben:=rech(oben, senk); li:=getdevicecaps(printer.handle,physicaloffsetx)+1+links; a:=rech(3,waag); with printer do begin title:='Grid-Druck'; BeginDoc; if grd.gridlinewidth > 0 then begin canvas.pen.color:=$333333; canvas.pen.width:=1; canvas.pen.style:=pssolid end else canvas.pen.style:=psclear; canvas.font:=grd.font; canvas.font.color:=schrift; canvas.font.size:=round((grd.font.size / 0.72) * scal); for x:=vSpalte to bSpalte do begin ob:=getdevicecaps(printer.handle,physicaloffsety)+1+oben; re:=li+rech(grd.ColWidths[x]+1,waag); for y:=vZeile to bZeile do begin un:=ob+rech(grd.RowHeights[y]+1,senk); if (x < grd.fixedcols)or(y < grd.fixedrows) then canvas.brush.color:=fix else canvas.brush.color:=grund; canvas.rectangle(li,ob,re+2,un+2); r:=rect(li+a,ob+1,re-a,un-2); drawtext(canvas.handle,pchar(grd.Cells[x,y]),length(grd.Cells[x,y]), r,DT_SINGLELINE or DT_VCENTER); ob:=un; end; li:=re; end; enddoc; end; end; end; |
MfG agent_x
|
|
Reinhard Kern
      
Beiträge: 591
Erhaltene Danke: 14
|
Verfasst: So 09.09.07 07:14
elduchte hat folgendes geschrieben: | Hallo,
bin so ziemlich neu was das Programmieren mit Delphi angeht, daher habe ich auch folgendes Problem:
Ich trage in einem TStringGrid Daten ein. Diese Daten möchte ich in eine Datei abspeichern und auch wieder laden können (zum ändern, erweitern etc). Auch sollen die Daten in tabellarischer form ausgedruckt werden können.
Ich habe schon so einiges probiert, bekomme es aber nicht hin. Ist das überhaupt möglich, oder habe ich einen Denkfehler? Kann mir jemand helfen?
Vielen Dank und Gruß
Andreas |
Hallo,
ich habe vor einiger Zeit einige StringGrid-Hilfsfunktionen geschrieben, u.a.:
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:
| procedure TRKMainForm.StreamOutGrid (var G : TStringGrid; var TF : TTextFile; Title : PChar); var nextline : array [0..1023] of char; itemarr : array [0..100] of char; c,r,cmax,rmax : integer; begin StrCopy (nextline,'$$$+ '); StrCat (nextline,Title); TF.w_writeln (nextline); cmax := G.ColCount; rmax := g.RowCount; for r := 1 to rmax - 1 do begin StrCopy (nextline,'"'); for c := 0 to cmax - 1 do begin StrPCopy (itemarr,G.Cells[c,r]); StrLCat (nextline,itemarr,1024); if c < cmax - 1 then StrLCat (nextline,'","',1024) else StrLCat (nextline,'"',1024); end; TF.w_writeln (nextline); ProcessGauge.increment; end; StrCopy (nextline,'$$$- '); StrCat (nextline,Title); TF.w_writeln (nextline); end;
procedure TRKMainForm.StreamInGrid (var G : TStringGrid; var TF : TTextFile; append : boolean); var nextline : array [0..1023] of char; itemstr : string[100]; c,r,cmax,rmax : integer; pni : PChar; nlok,eog,something : boolean; ia : array [0..100] of char; fp : longint; procedure getitem; begin itemstr := ''; while (pni[0] <> '"') and (pni[0] <> chr(0)) do pni := pni + 1; if pni[0] = '"' then pni := pni + 1; while (pni[0] <> '"') and (pni[0] <> chr(0)) do begin itemstr := itemstr + pni[0]; pni := pni + 1; end; while (pni[0] <> '"') and (pni[0] <> chr(0)) do pni := pni + 1; if pni[0] = '"' then pni := pni + 1; end;
begin if append then begin PackSingleGrid (G); rmax := G.RowCount - 1; r := rmax - 1; end else begin ClearGrid (G); rmax := 1; G.RowCount := rmax + 1; r := 0; end; cmax := G.ColCount; eog := false; repeat nlok := TF.w_readln (nextline); fp := TF.get_position; ProcessGauge.Update (fp); longop_yield; something := false; pni := nextline; if nlok then begin eog := StrLComp (Nextline,'$$$-',4) = 0; if not eog then begin r := r + 1; if r > rmax then begin rmax := r; G.RowCount := rmax + 1; end; for c := 0 to cmax - 1 do begin getitem; if c > 0 then if Length (itemstr) > 0 then something := true; G.Cells [c,r] := itemstr; StrPCopy (ia,itemstr); end; if (G <> SG_Components) and (G <> SG_Nets) then if something then begin Str (r:1,itemstr); G.Cells [0,r] := itemstr; end; end; end; until eog or not nlok; end; |
Die Dateien sehen folgendermassen aus (CSV-ähnlich), sind also lesbar und auch anderweitig, z.B. in Excel, verwendbar, ausserdem gibt es natürlich auch Funktionen, um mehrere Stringgrids in eine Datei zu schreiben und wieder zu lesen, daher der Name des Grids nach $$$. Ein paar Sachen wie ProcessGauge (Fortschrittsbalken) und Yield (ProcessMessages) kannst du dir wegdenken, das sind alte Klassen von mir, bevor Borland Lösungen erstellt hat.
$$$+ PrComponents
"1","CN11","SZ_ATECONNS","","X11","
"2","CN12","SZ_ATECONNS","","X12","
"3","CN13","SZ_ATECONNS","","X13","
"4","CN14","SZ_ATECONNS","","X14","
"5","CN15","SZ_ATECONNS","","X15","
"6","CN16","SZ_ATECONNS","","X16","
"7","CN17","SZ_ATECONNS","","X17","
"8","CN18","SZ_ATECONNS","","X18","
"9","CN101","SZ_POGO8S","","X101","
"10","CN102","SZ_POGO12S","","X102","
"11","CN103","SZ_POGO12S","","X103","
$$$- PrComponents
Gruss Reinhard
|
|
agent_x
      
Beiträge: 62
WIN 2000, Linux Kubuntu 6.06
|
Verfasst: So 09.09.07 07:19
ja das weis ich aber wo soll denn derquellcode rein den ich oben gepostet habe?
|
|
|