您的位置:首页 > 其它

怎么样实现打印stringGrid中的数据

2008-10-20 10:29 176 查看
怎么样实现打印stringGrid中的数据 Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiDB/html/delphi_20061225135512119.html

怎么样才能实再打印stringGrid中的数据啊,那位高人指点一下,谢谢了

用PRINTER对象!

能不能详细的说一下啊

给你代码
===========================
打印StringGrid内容
Procedure TACDListerMain.PrintTable;
Var
margins: TRect;
spacing: Integer;
Cols: TList;
Dlg: TPrintProgressDlg;

Procedure SetColumnWidth;
Var
i, k, w: Integer;
Begin
Printer.Canvas.Font.Style := [ fsBold ];
For i := 0 To Pred( Grid.ColCount ) Do

Cols.Add( Pointer( Printer.Canvas.TextWidth( Grid.Cells[ i,0 ] )));

Printer.Canvas.Font.Style := [];
For i := 1 To Pred( Grid.RowCount ) Do
For k := 0 To Pred( Grid.ColCount ) Do Begin
w:= Printer.Canvas.TextWidth( Grid.Cells[ k, i ] );
If w > Integer( Cols[ k ] ) Then
Cols[ k ] := Pointer( w );
End; { For }

w := 2 * Printer.Canvas.Font.PixelsPerInch div 3;
margins :=
Rect( w, w, Printer.PageWidth-w, Printer.PageHeight - w );
spacing := Printer.Canvas.Font.PixelsPerInch div 10;

w := 0;
For i := 0 To Pred(cols.Count) Do
w := w + Integer( cols[ i ] ) + spacing;
w := w - spacing;
If w > (margins.right-margins.left ) Then Begin
w := w - (margins.right-margins.left );
cols[ cols.Count-2 ] :=
Pointer( Integer( cols[ cols.Count-2 ] ) - w );
End; { If }

w:= 0;
For i := 0 To Pred(cols.Count) Do
w := w + Integer( cols[ i ] ) + spacing;
margins.right := w - spacing + margins.left;
End; { SetColumnWidth }

Procedure DoPrint;
Var
i: Integer;
y: Integer;
Procedure DoLine(lineno: Integer);
Var
x, n: Integer;
r: TRect;
th: Integer;
Begin
If Length(Grid.Cells[0,lineno]) = 0 Then Exit;

x:= margins.left;
With Printer.Canvas Do Begin
th := TextHeight( '膟' );
For n := 0 To Pred( Cols.Count ) Do Begin
r := Rect( 0, 0, Integer(Cols[ n ]), th);
OffsetRect( r, x, y );
TextRect( r, x, y, Grid.Cells[ n, lineno ] );
x := r.right + spacing;
End; { For }
End; { With }
y := y + th;
End; { DoLine }
Procedure DoHeader;
Begin
y:= margins.top;
With Printer.Canvas Do Begin
Font.Style := [ fsBold ];
DoLine( 0 );
Pen.Width := Font.PixelsPerInch div 72;
Pen.Color := clBlack;
MoveTo( margins.left, y );
LineTo( margins.right, y );
Inc( y, 2 * Pen.Width );
Font.Style := [ ];
End; { With }
End; { DoHeader }
Begin
y:= 0;
For i := 1 To Pred( Grid.RowCount ) Do Begin
Dlg.Progress( i );
Application.ProcessMessages;
If FPrintAborted Then Exit;

If y = 0 Then
DoHeader;
DoLine( i );
If y >= margins.bottom Then Begin
Printer.NewPage;
y:= 0;
End; { If }
End; { For }
End; { DoPrint }

Begin
FPrintAborted := False;
Dlg := TPrintProgressDlg.Create( Application );
With Dlg Do
try
OnAbort := PrintAborted;
Display( cPrintPreparation );
SetProgressRange( 0, Grid.RowCount );
Show;
Application.ProcessMessages;
Printer.Orientation := poLandscape;

Printer.BeginDoc;
Cols:= Nil;
try
Cols:= TLIst.Create;
Printer.Canvas.Font.Assign( Grid.Font );
SetColumnWidth;
Display( cPrintProceeding );
Application.ProcessMessages;
DoPrint;
finally
Cols.Free;
If FPrintAborted Then
Printer.Abort
Else
Printer.EndDoc;
end;
finally
Close;
End; { With }
End; { TACDListerMain.PrintTable }

var
TextF:Textfile;
S:String;
I:integer;
begin
try
AssignFile(TextF,'D:\PrintText.txt');
reWrite(TextF);
For i:=0 to StringGrid1.RowCount-1 do
begin
S:=StringGrid1.Rows[I].Text;
S:=StringReplace(S,#$D#$A,', ',[rfReplaceAll,rfIgnoreCase]);
Showmessage(S);
writeln(TextF,S);
end;
finally
CloseFile(TextF);
end;
end;

在给你一个导入到EXCEL的代码,我自己写的! 仅供参考
function GetExcelCoulmnCaption(num: Cardinal): string;
var
mod_num,div_num:Cardinal;
begin
if num=0 then exit;
if (num mod 26=0) then mod_num:=26
else mod_num:=num mod 26;
div_num:=num div 26;
if mod_num=26 then DEC(div_num);
if div_num=0 then
Result:=Chr(64+mod_num)
else Result:=Chr(64+div_num)+Chr(64+mod_num);
end;

procedure PrintSqlDataToExcel;
var
I:integer;
Range,ExcelApp,V:variant;
begin
Try
ExcelApp:=CreateOleObject('Excel.application');
Except
MessageDlg('没有安装Office 办公软件Excel!',mtinformation,[MBOK],0);
exit;
End;

try
ExcelApp.WorkBooks.add(Null);
V:=ExcelApp.WorkBooks[1].WorkSheets[1];

//*开始设计标题*/
Range:=V.Range['A1',GetExcelCoulmnCaption(DataSet.Fields.Count)+'1'];
Range.MergeCells:=true;
Range.RowHeight:=24;
Range.HoriZontalAlignMent:=xlCenter;
Range.VerticalAlignMent:=xlCenter;
Range.Font.Name:='新宋体';
Range.Font.size:=16;
Range.Font.FontStyle:='加粗';
Range.Value:=ExcelTitle; //定义EXCEL的标题是什么? EXCELTitle是字符型,需要自己定义
Range.Borders.LineStyle:=xlContinuous; //边框
Range.Borders.Weight:=xlThin;
Range.Borders.ColorIndex:=xlAutomatic;

//显示标题
For i:=0 To DataSet.Fields.Count-1 Do
begin
Range:=V.Range[GetExcelCoulmnCaption(I+1)+'2',GetExcelCoulmnCaption(I+1)+'2'];
Range.RowHeight:=24;
Range.HoriZontalAlignMent:=xlCenter;
Range.VerticalAlignMent:=xlCenter;
Range.Font.Name:='新宋体';
Range.Font.size:=9;
Range.Font.FontStyle:='加粗';
Range.Columns.AutoFit;
Range.Value:=DataSet.Fields[I].FieldName;
Range.Borders.LineStyle:=xlContinuous; //边框
Range.Borders.Weight:=xlThin;
Range.Borders.ColorIndex:=xlAutomatic;
end;
//显示内容
//set
Range:=V.Range['A3',GetExcelCoulmnCaption(DataSet.FieldCount)+IntToStr(DataSet.recordcount+2)];
Range.NumberFormatLocal:= '@';
Range.RowHeight:=20;
Range.HoriZontalAlignMent:=xlCenter;
Range.VerticalAlignMent:=xlCenter;
Range.Borders.LineStyle:=xlContinuous; //边框
Range.Borders.Weight:=xlThin;
Range.Borders.ColorIndex:=xlAutomatic;
Range.Font.Name:='新宋体';
Range.Font.size:=9;
Range.Columns.AutoFit;

DataSet.First;
While (Not DataSet.Eof) do
begin
For i:=0 To DataSet.Fields.Count-1 Do
begin
Range:=V.Range[GetExcelCoulmnCaption(I+1)+IntToStr(DataSet.RecNo+2),GetExcelCoulmnCaption(I+1)+IntToStr(DataSet.RecNo+2)];
if DataSet.Fields[I].IsNull then
Range.Value:=' '
else
Range.Value:=DataSet.Fields[I].AsString;
Range.Borders.LineStyle:=xlContinuous; //边框
Range.Borders.Weight:=xlThin;
Range.Borders.ColorIndex:=xlAutomatic;
end;
DataSet.next;
end;

//显示Excel文档界面
ExcelApp.visible:=true;
V.Activate;
finally
//释放接口对象
ExcelApp:=unassigned;
V:= unassigned;
Range:=unassigned;
end;
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐