通过Excel控件进行数据的打印或者预览函数包括了PrintView、PrintOut
参数含义:
FileName:需要打印的Excel文件路径
AUtoFile:是否列宽字段适应
Range:指定某一个格子选择 默认是空
具体是否页面垂直或者水平居中,看实际的情况
--------------------------------------------------------------------------------------------------
unit uExcelPrint;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ComObj;
procedure PrintView(FileName:String;const Range:String = '';const AutoFit:boolean = False);
procedure PrintOut(FileName:String;const Range:String = '';const AutoFit:boolean = False);
implementation
procedure PrintView(FileName:String;const Range:String = '';const AutoFit:boolean = False);
var
ExcelApp: OleVariant;
begin
try
ExcelApp := CreateOleObject('EXCEL.application');
except
Application.MessageBox('请安装EXCEL再打印!', '提示',MB_ok + MB_iconinformation + mb_applmodal);
Exit;
end;
try
try
ExcelApp.Visible := True;
ExcelApp.Workbooks.open(FileName);
ExcelApp.ActiveSheet.PageSetup.Orientation := 2;
ExcelApp.ActiveSheet.PageSetup.CenterHorizontally := 2; //页面水平居中
// ExcelApp.ActiveSheet.PageSetup.CenterVertically := 2; //页面垂直居中
//ExcelApp.Cells.Select;
if Range = '' then
ExcelApp.Cells.Select
else
ExcelApp.Range[ Range ].Select;
ExcelApp.Selection.Font.Size := 11;
ExcelApp.Selection.borders.LineStyle := 7;
ExcelApp.Selection.RowHeight := 1/0.035;
ExcelApp.Selection.Rows[3].RowHeight := 1.5/0.035; // 1厘米
ExcelApp.Selection.Rows[3].WrapText := True;
if AutoFit then
begin
if Range = '' then
ExcelApp.Cells.Select
else
ExcelApp.Range[ StringReplace(Range,'A1','A3',[]) ].Select;
ExcelApp.Selection.WrapText := True;
ExcelApp.Selection.Rows.AutoFit; //设为自动列宽,
//FXLSRange:='A1:H'+IntToStr(cdsSTAT.RecordCount+2);
if Range = '' then
ExcelApp.Cells.Select
else
ExcelApp.Range[ Range ].Select;
end;
ExcelApp.ActiveSheet.PrintPreview;
//ExcelApp.ActiveSheet.PrintOut;
except
end;
finally
ExcelApp:=Null;
end;
end;
procedure PrintOut(FileName:String;const Range:String = '';const AutoFit:boolean = False);
var
ExcelApp: OleVariant;
begin
try
ExcelApp := CreateOleObject('EXCEL.application');
except
Application.MessageBox('请安装EXCEL再打印!', '提示',MB_ok + MB_iconinformation + mb_applmodal);
Exit;
end;
try
try
ExcelApp.Visible := True;
ExcelApp.Workbooks.open(FileName);
//ExcelApp.ActiveSheet.PrintPreview;
ExcelApp.ActiveSheet.PageSetup.Orientation := 2;
if Range = '' then
ExcelApp.Cells.Select
else
ExcelApp.Range[ Range ].Select;
ExcelApp.Selection.Font.Size := 11;
ExcelApp.Selection.borders.LineStyle := 7;
ExcelApp.Selection.RowHeight := 1/0.035;
ExcelApp.Selection.Rows[3].RowHeight := 1.5/0.035; // 1厘米
ExcelApp.Selection.Rows[3].WrapText := True;
if AutoFit then
begin
if Range = '' then
ExcelApp.Cells.Select
else
ExcelApp.Range[ StringReplace(Range,'A1','A3',[]) ].Select;
ExcelApp.Selection.WrapText := True;
ExcelApp.Selection.Rows.AutoFit; //设为自动列宽,
//FXLSRange:='A1:H'+IntToStr(cdsSTAT.RecordCount+2);
if Range = '' then
ExcelApp.Cells.Select
else
ExcelApp.Range[ Range ].Select;
end;
ExcelApp.ActiveSheet.PrintOut;
except
end;
finally
ExcelApp:=Null;
end;
end;
end.
|
|