给你一段代码参考
procedure TF_Date_TJ.B_PrintClick(Sender: TObject);
var
I,J:Integer;
TmpFileName:String;
SelField,SelRec:Integer;
PrintAll:Boolean;
XlsApp,XlsSheet:Variant;
begin
Fmain.SBar.Panels[0].Text := '';
PrintAll := false;
if SG_User.Row = 0 then
begin
if MessageDlg('当前没有选定打印的记录,是否全部打印?',mtconfirmation,[Mbyes,Mbno],0) = mrno then
begin
Application.MessageBox(pchar('没有选定的数据记录,打印终止!'),pchar('注意'),mb_ok);
Abort;
end
else
PrintAll := true;
end;
try
Fmain.SBar.Panels[0].Text := '正在启动Excel...';
//if VarIsEmpty(XlsApp) then
XlsApp := CreateOleObject('Excel.Application');
// XlsApp.ActiveSheet.Name := 'DataTransTmp';
XLsApp.Workbooks.Add;
XlsSheet := XLsApp.Worksheets['Sheet1'];
// XlsSheet.Activesheets.Name := 'DataTransTmp';
except
Application.MessageBox(pchar('Ms Excel 无法启动,数据转入终止!'),pchar('注意'),mb_ok);
//BinExcel.Enabled := false;
Fmain.SBar.Panels[0].Text := 'Excel无法启动';
Abort;
end;
Fmain.SBar.Panels[0].Text := '数据正在转移至Excel';
SelField := 0;
for J := 0 to SG_User.RowCount-1 do
begin
//if (SG_User.Co<> false) and (SG_User.col.Color<> clgray) then
//begin
SelField := SelField + 1;
XlsSheet.Cells[3, SelField] := SG_User.Cells[J,0];
//end;
end;
SelRec := 0;
with Fmain.Q1 do
begin
close;
sql.Clear;
sql.Add('select * from cw_book_xzsrl');
open;
first;
for I := 0 to RecordCount-1 do
begin
if not PrintAll then
begin
if SG_user.Cells[1,1]<>'' then
begin
Next;
Continue
end;
end;
SelRec := SelRec + 1;
SelField := 0;
for J := 0 to SG_user.RowCount-1 do
begin
//if (Sg_user.Col.Visible<> false) and (SG_User.col.color<> clAqua) then
//begin
SelField := SelField + 1;
if StrToIntDef(Fields[J].AsString, -1)<>-1 then
XlsSheet.Cells[selRec + 3, SelField] := '''' + Fields[J].Asstring
else
XlsSheet.Cells(selRec + 3, SelField) := Fields[J].Asstring
//end;
end;
Next;
end;
end;
XlsApp.Visible := true;
Fmain.SBar.Panels[0].Text := '数据转移成功,请使用Excel打印所选定的数据';
{ Application.MessageBox(pchar('数据已转入至Excel文件中,请于下列路径中查看:' + #13#10
+ ' ' + TmpFileName),pchar('注意'),mb_ok);}
end;