function ExcelDataSetPrint(DataSet:TDataSet):boolean; var TheExcel,Range:Variant; nrow,ncol,i:integer; begin try begin if not DataSet.Active then begin MessageDlg('不能打印关闭的数据集', mtInformation,[mbOk], 0); exit; end; TheExcel:=CreateOleObject('Excel.Application'); TheExcel.visible:=false; TheExcel.Workbooks.add; TheExcel.Workbooks[1].sheets[1].name:='打印单元'; nrow:=1; for ncol :=1 to form1.DBGridEh1.FieldCount do TheExcel.worksheets[1].cells[nRow,nCol]:=form1.DBGridEh1.Columns[ncol-1].fieldname; nrow:=nrow+1; DataSet.First; while not DataSet.Eof do begin for ncol :=1 to form1.DBGridEh1.Columns.Count do TheExcel.worksheets[1].cells[nRow,nCol]:=form1.DBGridEh1.Fields[ncol-1].AsString; DataSet.Next; nrow:=nrow+1; end; TheExcel.visible:=true; end; except begin TheExcel.Quit; end; end;
try
begin
if not DataSet.Active then begin
MessageDlg('不能打印关闭的数据集', mtInformation,[mbOk], 0); exit;
end;
TheExcel:=CreateOleObject('Excel.Application');
TheExcel.visible:=false;
TheExcel.Workbooks.add;
TheExcel.Workbooks[1].sheets[1].name:='打印单元'; nrow:=1; for ncol :=1 to form1.DBGridEh1.FieldCount do
TheExcel.worksheets[1].cells[nRow,nCol]:=form1.DBGridEh1.Columns[ncol-1].fieldname; nrow:=nrow+1; DataSet.First; while not DataSet.Eof do begin
for ncol :=1 to form1.DBGridEh1.Columns.Count do
TheExcel.worksheets[1].cells[nRow,nCol]:=form1.DBGridEh1.Fields[ncol-1].AsString;
DataSet.Next;
nrow:=nrow+1;
end;
TheExcel.visible:=true;
end;
except
begin
TheExcel.Quit; end; end;