DBGRID能不能打印?
2楼: 当然可以打印
有个控件叫printatonce
或者reportmachine里也有 如免费库存管理软件
3楼: 能 自己写代码或用fastreport 中的控件 TfrPrintGrid
4楼: 你可以用 cxgrid 控件来做,也可以 用数据集的方式,通过FastReport来传入。
建议用cxgrid简单,好用,象Excel 一样。。
5楼: type
TForm1 = class(TForm)
Button1: TButton;
DBGrid1: TDBGrid;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
{...}
procedure TForm1.Button1Click(Sender: TObject);
var
MyTextFile : TextFile;
tmpStr,OldFont : String;
begin
{ set to default printer }
Printer.PrinterIndex := -1;
{ save the printers old font }
OldFont := Printer.Canvas.Font.Name;
{ set the printers font name to a fixed
width font so the printout will have
even looking columns }
Printer.Canvas.Font.Name := ''Courier New'';
{ assign our textfile to the printer }
AssignPrn(MyTextFile);
try
{ initialize our textfile }
Rewrite(MyTextFile);
{ format and print out our header according
to the fieldnames, I only have 2 fields in
my table..you will have to add entries to
the Format() if you have more fields }
tmpStr := Format(''%-20.20s %-20.20s'',
[dbGrid1.DataSource.DataSet.Fields[0].FieldName,
dbGrid1.DataSource.DataSet.Fields[1].FieldName]);
Writeln(MyTextFile, tmpStr);
{ format and print out a seperator between
the header and rows }
tmpStr := Format(''%-20.20s %-20.20s'',
[''--------------------'',
''--------------------'']);
Writeln(MyTextFile, tmpStr);
{ start at the first record in the DBGrid''s table }
dbGrid1.DataSource.DataSet.First;
{ loop through the DBGrid''s table, if the table is
too large then you may want to filter it down }
while ( not dbGrid1.DataSource.DataSet.EOF ) do
begin
{ format each row and print them out }
tmpStr := Format(''%-20.20s %-20.20s'',
[dbGrid1.DataSource.DataSet.Fields[0].AsString,
dbGrid1.DataSource.DataSet.Fields[1].AsString]);
Writeln(MyTextFile, tmpStr);
{ go to the next record in the DBGrid''s table }
dbGrid1.DataSource.DataSet.Next;
end;
finally
{ close our textfile }
CloseFile(MyTextFile);
{ reset the printers font name }
Printer.Canvas.Font.Name := OldFont;
end;
end;
6楼: DBGrid 变换成 HTML 表格
(*//
标题 : 数据网格处理成超文本表格
说明 : 支持对齐、字体、背景颜色 ; 做打印又有一种新方法了!
设计 :Zswang
日期 :2002-05-19
支持 :wjhu111@21cn.com
//*)
///////Begin Source
function ColorToHtml(mColor: TColor): string;
begin
mColor := ColorToRGB(mColor);
Result := Format(''#%.2x%.2x%.2x'',
[GetRValue(mColor), GetGValue(mColor), GetBValue(mColor)]);
end; { ColorToHtml }
function StrToHtml(mStr: string; mFont: TFont = nil): string;
var
vLeft, vRight: string;
begin
Result := mStr;
Result := StringReplace(Result, ''&'', ''&'', [rfReplaceAll]);
Result := StringReplace(Result, ''<'', ''<'', [rfReplaceAll]);
Result := StringReplace(Result, ''>'', ''>'', [rfReplaceAll]);
if Result ='''' then Result:=''-'';
if not Assigned(mFont) then Exit;
vLeft := Format('''',
[mFont.Name, ColorToHtml(mFont.Color)]);
vRight := '''';
if fsBold in mFont.Style then begin
vLeft := vLeft + '''';
vRight := '''' + vRight;
end;
if fsItalic in mFont.Style then begin
vLeft := vLeft + '''';
vRight := '''' + vRight;
end;
if fsUnderline in mFont.Style then begin
vLeft := vLeft + '''';
vRight := '''' + vRight;
end;
if fsStrikeOut in mFont.Style then begin
vLeft := vLeft + ''
vRight := ''
end;
Result := vLeft + Result + vRight;
end; { StrToHtml }
function DBGridToHtmlTable(mDBGrid: TDBGrid; mStrings: TStrings;
mCaption: TCaption = ''''): Boolean;
const
cAlignText: array[TAlignment] of string = (''LEFT'', ''RIGHT'', ''CENTER'');
var
vColFormat: string;
vColText: string;
vAllWidth: Integer;
vWidths: array of Integer;
vBookmark: string;
I, J: Integer;
begin
Result := False;
if not Assigned(mStrings) then Exit;
if not Assigned(mDBGrid) then Exit;
if not Assigned(mDBGrid.DataSource) then Exit;
if not Assigned(mDBGrid.DataSource.DataSet) then Exit;
if not mDBGrid.DataSource.DataSet.Active then Exit;
vBookmark := mDBGrid.DataSource.DataSet.Bookmark;
mDBGrid.DataSource.DataSet.DisableControls;
try
J := 0;
vAllWidth := 0;
for I := 0 to mDBGrid.Columns.Count - 1 do
if mDBGrid.Columns[I].Visible then begin
Inc(J);
SetLength(vWidths, J);
vWidths[J - 1] := mDBGrid.Columns[I].Width;
Inc(vAllWidth, mDBGrid.Columns[I].Width);
end;
if J <= 0 then Exit;
mStrings.Clear;
mStrings.Add(Format(''
DisplayText%d | ''#13#10,%s | ''#13#10,
finally
mDBGrid.DataSource.DataSet.Bookmark := vBookmark;
mDBGrid.DataSource.DataSet.EnableControls;
vWidths := nil;
end;
Result := True;
end; { DBGridToHtmlTable }
///////End Source
{ uses ShellApi; }
///////Begin Demo
procedure TForm1.Button1Click(Sender: TObject);
begin
DBGridToHtmlTable(DBGrid1, Memo1.Lines, Caption);
Memo1.Lines.SaveToFile(''c:\temp.htm'');
ShellExecute(Handle, nil, ''c:\temp.htm'', nil, nil, SW_SHOW);
end;
///////End Demo
仓库管理软件版7楼: 用ReportMachine里面有GridReport超简单易用,功能也挺不错!
8楼: 我也是用reprotmachie,挺好用的,功能 也挺强
9楼: 打印dbgrid里的内容:
定义打印过程
procedure PrintDbGrid(DataSet:TDataSet;DbGrid:TDbGrid;Title:String);
var
PointX,PointY:integer;
ScreenX:integer;
i,lx,ly:integer;
px1,py1,px2,py2:integer;
RowPerPage,RowPrinted:integer;
ScaleX:Real;
THeight:integer;
TitleWidth:integer;
SumWidth:integer;
PageCount:integer;
SpaceX,SpaceY:integer;
RowCount:integer;
begin
PointX:=Round(GetDeviceCaps(printer.Handle,LOGPIXELSX)/2.54);
PointY:=Round(GetDeviceCaps(printer.Handle,LOGPIXELSY)/2.54);
ScreenX:=Round(Screen.PixelsPerInch/2.54);
ScaleX:=PointX/ScreenX;
RowPrinted:=0;
SumWidth:=0;
printer.BeginDoc;
With Printer.Canvas do
begin
DataSet.DisableControls;
DataSet.First ;
THeight:=Round(TextHeight(''我'')*1.5);//设定每行高度为字符高的1.5倍
SpaceY:= Round(TextHeight(''我'')/4);
SpaceX:=Round(TextWidth(''我'')/4);
RowPerpage:=Round((printer.PageHeight-5*PointY)/THeight); //上下边缘各2厘米
ly:=2*PointY;
PageCount:=0;
while not DataSet.Eof do
begin
if (RowPrinted=RowPerPage) or (RowPrinted=0) then
begin
if RowPrinted<>0 then
Printer.NewPage;
RowPrinted:=0;
PageCount:=PageCount+1;
Font.Name:=''宋体'';
Font.size:=16;
Font.Style:=Font.Style+[fsBold];
lx:=Round((Printer.PageWidth-TextWidth(Title))/2);
ly:=2*PointY;
TextOut(lx,ly,Title);
Font.Size:=11;
Font.Style:=Font.Style-[fsBold];
lx:=Printer.PageWidth-5*PointX;
ly:=Round(2*PointY+0.2*PointY);
if RowPerPage*PageCount>DataSet.RecordCount then
RowCount:=DataSet.RecordCount
else
RowCount:=RowPerPage*PageCount;
TextOut(lx,ly,''第''+IntToStr(RowPerPage*(PageCount-1)+1)+''-''+IntToStr(RowCount)+''条,共''+IntToStr(DataSet.RecordCount)+''条'');
lx:=2*PointX;
ly:=ly+THeight*2;
py1:=ly-SpaceY;
if RowCount=DataSet.RecordCount then
py2:=py1+THeight*(RowCount-RowPerPage*(PageCount-1)+1)
else
py2:=py1+THeight*(RowPerPage+1);
SumWidth:=lx;
for i:=0 to DBGrid.Columns.Count-1 do
begin
px1:=SumWidth-SpaceX;
px2:=SumWidth;
MoveTo(px1,py1);
LineTo(px2,py2);
TitleWidth:=TextWidth(DBGrid.Columns[i].Title.Caption);
lx:=Round(SumWidth+(DBGrid.Columns[i].width*scaleX-titleWidth)/2);
TextOut(lx,ly,DBGrid.Columns[i].Title.Caption);
SumWidth:=Round(SumWidth+DBGrid.Columns[i].width*scaleX)+SpaceX*2;
end;
px1:=SumWidth; //画最后一条竖线
px2:=SumWidth;
MoveTo(px1,py1);
LineTo(px2,py2);
px1:=2*PointX; //画第一条横线
px2:=SumWidth;
py1:=ly-SpaceY;
py2:=ly-SpaceY;
MoveTo(px1,py1);
LineTo(px2,py2);
py1:=py1+THeight;
py2:=py2+THeight;
MoveTo(px1,py1);
LineTo(px2,py2);
end;
lx:=2*PointX;
ly:=ly+THeight;
px1:=lx;
px2:=SumWidth;
py1:=ly-SpaceY+THeight;
py2:=ly-SpaceY+THeight;
MoveTo(px1,py1);
LineTo(px2,py2);
for i:=0 to DBGrid.Columns.Count-1 do
begin
TextOut(lx,ly,DataSet.FieldByname(DBGrid.Columns[i].Fieldname).AsString);
lx:=Round(lx+DBGrid.Columns[i].width*ScaleX+SpaceX*2);
end;
RowPrinted:=RowPrinted+1;
DataSet.next;
end;
DataSet.first;
DataSet.EnableControls;
end;
printer.EndDoc;
end;
调用打印过程:
procedure TForm1.Button1Click(Sender: TObject);
begin
PrintDbGrid(adoquery1,dbgrid1,''sdfsd'');
end;
10楼: 通不过呀!??
11楼: dbgrideh就可以了。
12楼: 不想用三方控件 如免费记账软件
13楼: 你换一个思路行不行?
我不用DBGrid打印,而是把查询结果导出到预先做好的excel文件,然后打印这个excel文件,这样的打印控制非常简单,还可以预览,还有一个有点,随时可以根据客户要求变更部分打印内容或格式,方便。象baobao2601那种方法太难维护了!
仓库管理软件版14楼: delphi自带的quickReport控件,按dbgrid的字段排一下版,可以加标题、表头、页尾等信息,一般功能都有的
15楼: fastreport 就可以。还省事
16楼: 其实我想用DBGRID实现简单的打印
17楼: 有办法能使DBGrid 实现简单的打印吗?
18楼: 我喜欢用电子表格!
19楼: //开始打印
with printer do
begin
BeginDoc;
Canvas.Font.Size :=10;
Canvas.Font.name:=''宋体'';
i:=2;
with dbgrid.DataSource.DataSet do
begin
//在此输出报表标题
Canvas.TextOut(0 ,20,'' 我的报表 '');
Canvas.TextOut(20 ,20,''姓名 性别 生日 .....'');
first;
while not eof do
begin
Canvas.TextOut(i*20,20,FieldByName (字段1).asstring+FieldByName (字段2).asstring);
next;
i:=i+1;
end;
end;
enddoc;
end ;
20楼: quickReport控件
仓库管理软件版21楼: 自由界面和报表的完美解决方案
http://www.anylib.com
22楼: DBGrid能直接输出吗? 如记账本软件免费版