[delphi]wydruk z dbgrid

0

mam tabele dbgrid w ktorej wylistowani sa klienci. ustawilem w niej mozliwosc zaznaczania kilka wierszy-multiple.
jak zrobic, aby delphi wydrukowalo zaznaczone wiersze ?

nie uzywam ravereports bo mi nie chodzi:/ jest moze inna metoda ?

0

free report
quickreport
malowanie po płótnie
inne

0

Przekazuję Ci procedurę wydruku, którą opracowałem po wskazówkach forumowicza Misiekd.
Sprawdzona w Turbo Delphi Explorer.

Dopasuj do swoich potrzeb.
Procedura drukuje dokumenty kasowe KP, Kw wprowadzone w pewnym okresie tworzące raport kasowy.

Warunki wstępne:
Kasa - nazwa tabeli .dbf
- w procedurze FormCreate, następuje filtrowanie potrzebnych rekordów, pokazanych w DBGrid
- automatycznie ustala format wydruku: Portret lub Landskape w zależności od ilości
i szerokości kolumn wydruku,
- drukuje i numeruje strony powyżej jednej,
- w oryginale w nagłówku kolumn drukuje nazwy kolumn tabeli. Ja nadałem swoje połne nazwy,

Edit3 - to pole z numerem raportu
Edit1, Edit2 - to pola z datą
Button2 - Zapisanie raportu

procedure TRapkas.Button1Click(Sender: TObject);

Const MaxNrColumns = 20; //ilość kolumn do wydruku
xMargin = 300; //margines poziomy lewy
yMargin = 500; //margines pionowy górny
DrawHGridLines = True;
DrawVGridLines = True;
var x, y, yTop, w, xTopLeft, xTopRight, th, tw, dy,
xColmargin, n, PageNr: integer;
r: TRect;
BM: TBookmark;
OrgOrientation: TPrinterOrientation;
ColPos: array[0..MaxNrColumns] of integer;
ColWidth: array[0..MaxNrColumns] of integer;
PrintCol: array[0..MaxNrColumns] of Boolean;

Procedure PrintField(ColIndex: integer; FieldText: string);
begin
with DBGrid1 do
if PrintCol[ColIndex] then
Printer.Canvas.TextOut(ColPos[ColIndex+1] - tw -
Printer.Canvas.TextWidth(FieldText), y, FieldText);
end; { PrintField }

Procedure PrintCaptions; //druk nagłówka, tytułu formularza
var aTitle: string;
i: integer;
begin
inc(PageNr);
y:= yMargin;
with Printer, DBGrid1 do
begin
if pageNr > 1 then
NewPage;
Printer.Canvas.Brush.Color := clWhite;
Printer.Canvas.Pen.Color := clBlack;
if length(Caption) > 0 then
begin
if pageNr > 1 then
aTitle:= Caption + ' ' + Edit3.Text + ' strona : ' + IntToStr(PageNr)
else
aTitle:= Caption + ' ' + Edit3.Text;
Printer.Canvas.Font.Size := 17;
Printer.Canvas.TextOut((xTopLeft + 100) + ((xTopRight-xTopLeft)-
Printer.Canvas.TextWidth(Title)) div 5, y, aTitle );
y:= y + th + dy + 100;
Printer.Canvas.Font.Size := 12;
Printer.Canvas.TextOut((xTopLeft) + ((xTopRight-xTopLeft)-
Printer.Canvas.TextWidth('')) div 5, y, 'od dnia: ' + Edit1.Text +
' do dnia: ' + Edit2.Text);
y:= y + th + dy + 100;
end;
yTop:= y;
x:= xTopLeft;
r := Rect(x, yTop, xTopRight, yTop + th + 4dy);
Printer.Canvas.Rectangle(r);
Printer.Canvas.Pen.Color := clBlack; // kolor ramki
y:= yTop + dy;
for i := 0 to DBGrid1.Columns.Count-1 do
Printer.Canvas.TextOut((x + 100),y, 'Pozycja');
Printer.Canvas.TextOut((x + 560),y, 'Data wpisu');
Printer.Canvas.TextOut((x + 1200),y, 'Nr. KP');
Printer.Canvas.TextOut((x + 1800),y, 'Nr. KW');
Printer.Canvas.TextOut((x + 2400),y, 'Wpłata');
Printer.Canvas.TextOut((x + 2900),y, 'Wypłata');
Printer.Canvas.TextOut((x + 3500),y, 'Saldo');
y:= y + th + 4
dy; //wysokość wiersza nagłówka
Printer.Canvas.Brush.Color := clWhite;
end;
end; { PrintCaptions }

Procedure PrintBoundary;
var i: integer;
begin
y:= y + dy;
r := Rect(xTopLeft,yTop,xTopRight,y);
with Printer.Canvas do
begin
Brush.Color := clBlack;
FrameRect(r);
if DrawVGridLines then
for i := 0 to DBGrid1.Columns.Count-2 do
if PrintCol[i] then
begin
MoveTo(ColPos[i+1],yTop);
LineTo(ColPos[i+1],y);
end;
end;
end; { PrintBoundary }
// procedura wydruku
begin
if PrintDialog1.Execute then
begin
with Printer, DBGrid1 do
begin
Screen.Cursor := crHourGlass;
OrgOrientation:= Orientation;
tw:= Printer.Canvas.TextWidth('N'); //szerokość tekstu
th:= Printer.Canvas.TextHeight('N'); //wysokość tekstu
dy:= (th div 5);
xColmargin:= tw; //margines kolumny
Printer.Canvas.Font.Name := 'Times New Roman';
Printer.Canvas.Font.Size := 12;
for n := 0 to Columns.Count-1 do //liczba kolumn
begin
PrintCol[n]:= (Columns[n].Width >0)and(Columns[n].Visible);
if PrintCol[n] then
ColWidth[n]:= Printer.Canvas.TextWidth(Columns[n].DisplayName) + 200
else
ColWidth[n]:= 0;
end;
BM:= Kasa.GetBookmark; //zapamiętaj rekord
Kasa.DisableControls;
try
Kasa.First;
while not Kasa.EOF do
begin
for n := 0 to (Kasa.FieldCount-1) do
if PrintCol[n] and
(Printer.Canvas.TextWidth(Kasa.Fields[n].AsString) > ColWidth[n]) then
ColWidth[n]:= Printer.Canvas.TextWidth(Kasa.Fields[n].AsString);
Kasa.Next;
end;
x:= xMargin;
for n := 0 to DBGrid1.Columns.Count-1 do
begin
ColPos[n]:= x;
if PrintCol[n] then
begin
ColWidth[n]:= ColWidth[n] + xColmargin;
x:= x + ColWidth[n];
end;
end;
ColPos[DBGrid1.Columns.Count]:= x; // koniec ostatniej kolumny
if (x > (PageWidth - xMargin)) and (Orientation<>poLandscape) then
Orientation:= poLandscape;
w:= (PageWidth-xMargin)-x; // wolne miejsce z prawej strony
if w>0 then // ustaw centralnie na stronie
begin
w:= w div 5;
xTopLeft:= xMargin + w;
for n := 0 to DBGrid1.Columns.Count do
ColPos[n]:= ColPos[n] + w;
end
else
xTopLeft:= xMargin;
xTopRight:= ColPos[DBGrid1.Columns.Count];
BeginDoc; // Start drukowania
PageNr:= 0;
PrintCaptions; //wydruk tytułu i nagłówka
Kasa.First;
while not Kasa.EOF do
begin
for n := 0 to (Kasa.FieldCount-1) do
PrintField(n,Kasa.Fields[n].AsString);
y:= y + th + 40;
Kasa.Next;
if DrawHGridLines And (not Kasa.EOF) then
begin
Printer.Canvas.MoveTo(xTopLeft,y);
Printer.Canvas.LineTo(xTopRight,y);
y:= y + 2 * dy;
end;
if (y > PageHeight-yMargin) and (not Kasa.EOF) then
begin
PrintBoundary;
PrintCaptions;
end;
end;
PrintBoundary;
Printer.Canvas.Brush.Color := clWhite;
Printer.Canvas.Pen.Color := clBlack;
Printer.Canvas.TextOut( 1400, y + 200, ' R a z e m : ');
Printer.Canvas.Font.Size := 15;
Printer.Canvas.TextOut(2800, y + 200, DBEdit1.Text);
Printer.Canvas.TextOut(3400, y + 200, DBEdit2.Text);
Printer.Canvas.Font.Size := 12;
EndDoc; // koniec wydruku
finally
Kasa.GotoBookmark(BM);
Kasa.EnableControls;
// przywrócenie domyślnych ustawień
Printer.Canvas.Brush.Color := clWhite;
Printer.Canvas.Pen.Color := clBlack;
Orientation:= OrgOrientation;
Screen.Cursor := crDefault;
end; { try }
end;
end;
Button2.Visible:= True;
end; { PrintDBGrid }

// koniec procedury wydruku

Orginalna procedura:

Procedure PrintCaptions;
var aTitle: string;
i: integer;
begin
inc(PageNr);
y:= yMargin;
with Printer, DBGrid1 do
begin
if pageNr>1 then
NewPage;
Printer.Canvas.Brush.Color := clWhite;
Printer.Canvas.Pen.Color := clBlack;
if length(Caption)>0 then
begin
if pageNr>1 then
aTitle:= Caption + ' (' + IntToStr(PageNr) + ')'
else
aTitle:= Caption;
Printer.Canvas.TextOut(xTopLeft + ((xTopRight-xTopLeft)-
Printer.Canvas.TextWidth(Title)) div 2, y, aTitle);
y:= y + th + dy;
end;
yTop:= y;
x:= xTopLeft;
Printer.Canvas.Brush.Color := clLtGray;
r := Rect(x, yTop, xTopRight, yTop + th + 2dy);
Printer.Canvas.Rectangle(r);
Printer.Canvas.Pen.Color := clLtGray; // for the gridlines
y:= yTop + dy;
for i := 0 to DBGrid1.Columns.Count-1 do
PrintField(i,Columns[i].DisplayName);
y:= y + th + 2
dy;
Printer.Canvas.Brush.Color := clWhite;
end;
end; { PrintCaptions }

0

dzieki,ale ona chyba drukuje calego grida ? a jak dac jej ograniczenie do wierszy zaznaczonych(selected) ??

0

Tu niestety musisz wykazać swoją intencję. Napisałem Ci, że musisz dostosować do swoich potrzeb.

1 użytkowników online, w tym zalogowanych: 0, gości: 1