use ComObj ; //==================================== //delphi导出Excel表 procedure saveToExcel(adoquery1 : tdataset); var E, workbook :variant; i,n:integer; begin //检查数据完整性 if not adoquery1.Active then exit; if adoquery1.RecordCount<=0 then exit; if application.MessageBox('确认导出excel表吗?','提示',mb_okcancel+mb_iconinformation)=idcancel then exit; //创建Excel工程 E := createoleobject('Excel.Application'); //增加一个工作表 E.workbooks.add; //写入表头 for i:=0 to dbgrid1.FieldCount-1 do begin E.cells[1,i+1]:=dbgrid1.Columns[i].Title.Caption; //左上角第一个方格是[1,1] end; //写入记录值 adoquery1.First; n:=2; //从第2行开始写内容 while not adoquery1.Eof do begin E.cells[n,1] := adoquery1.Fields[0].AsString; E.cells[n,2] := adoquery1.Fields[1].AsString; E.cells[n,3] := adoquery1.Fields[2].AsString; E.cells[n,4] := adoquery1.Fields[4].AsString; E.cells[n,6] :=' '; inc(n); adoquery1.Next; end; //提示完成 application.MessageBox('数据导出完成!','提示',mb_ok+mb_iconinformation); //显示Excel E.visible := true; end;use ComObj ; //============================== 你的当前访问异常,请进行认证后继续阅读剩余内容。 提交