1、打开Excel文件
try
ExcelApplication1.Connect;
except
MessageDlg('没有装Excel',mtError,[mbOK],0);
Abort;
end;
ExcelApplication1.Visible[0] := False;
ExcelApplication1.Caption := 'Excel应用';
ExcelApplication1.WorkBooks.Open('c:/1.xls',
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,0);
ExcelWorkBook1.ConnectTo(ExcelApplication1.WorkBooks[1]);
ExcelWorkSheet1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);
{
...
//对该Excel文件进行填写
}
ExcelApplication1.WorkBooks[1].Close(1,EmptyParam,EmptyParam,0);
try
OleContainer1.CreateObjectFromFile('c:/1.xls',False);
OleContainer1.DoVerb(-1);
OLEContainer1.OleObject.CommandBars['Standard'].Visible := False;
OLEContainer1.OLEObject.CommandBars['formatting'].Visible := False;
except
form_syqdyl.OleContainer1.DestroyObject;
end;
2、依次打印Excel文件中的Sheet
for i := 1 to OLEContainer1.OleObject.Sheets.Count do
OLEContainer1.OleObject.Sheets.PrintOut;
OleContainer1.DoVerb(-1);
3、退出
OLEContainer1.OleObject.CommandBars['Standard'].Visible := True;
OLEContainer1.OLEObject.CommandBars['formatting'].Visible := True;
OLEContainer1.DestroyObject;
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
以上在Delphi5中已测试,使用Servers中组件连接Excel