通過Excel控件進(jìn)行數(shù)據(jù)的打印或者預(yù)覽函數(shù)包括了PrintView、PrintOut
參數(shù)含義:
FileName:需要打印的Excel文件路徑
AUtoFile:是否列寬字段適應(yīng)
Range:指定某一個(gè)格子選擇 默認(rèn)是空
具體是否頁面垂直或者水平居中,看實(shí)際的情況
--------------------------------------------------------------------------------------------------
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('請(qǐng)安裝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; //設(shè)為自動(dòng)列寬,
//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('請(qǐng)安裝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; //設(shè)為自動(dòng)列寬,
//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.
|
||||||||||||||||||||||||
|
|