新年到了,大家帮帮忙

zhy2003 2003-01-22 11:14:55
从delphi导数据到excel(从程序)太慢了,有没有更好的办法????
...全文
37 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhy2003 2003-01-22
  • 打赏
  • 举报
回复
Sfn:=ExtractFileDir(application.ExeName)+'\templates\Êý¾Ýµ¼³öÄ£°å.xls';
Dfn:=ExtractFileDir(application.ExeName)+'\reports\Êý¾Ýµ¼³öÄ£°å.xls';

opInfo.Wnd:=Handle;
opinfo.fAnyOperationsAborted:=false;
opInfo.lpszProgressTitle:='';
opInfo.pFrom:=pChar(sFn);
OPInfo.pTo:=pChar(dFn);

if (SHFileOperation(opinfo)<>0) then
begin
ShowMessage('Îļþ²Ù×÷ʧ°Ü£¬ÏµÍ³½«Í˳ö£¡');
exit;
end;

////////////////////////////////////////////////////////////////
try
ea.Connect;
except
end;
ea.Visible[0]:=false;
eb.ConnectTo(ea.Workbooks.Open(dfn,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,0));
ew.ConnectTo(eb.activesheet as _worksheet);

rs:=Tquery.Create(application);
rs.databasename :='cnqf';
if ((frmlogin.varqxinfo.dhhb=0) and(frmlogin.varqxinfo.dhzs=0) and (frmlogin.varqxinfo.xhhb=0) and(frmlogin.varqxinfo.xhzs=0) and(frmlogin.varqxinfo.xtgly=1)and(frmlogin.varqxinfo.leader=0))or ((frmlogin.varqxinfo.xhcx=0) and (frmlogin.varqxinfo.dhcx=0))then
begin
if checkbox1.Checked and checkbox2.Checked and checkbox4.Checked then
begin
str:='select qfxx.hh,qfxx.hm,yhda.dz,qfxx.dfyf,qfxx.sjje from qfxx,yhda where (yhda.hh=qfxx.hh) and (yhda.bmbs='+''''+combo_bmbs.text+''''+')and (qfxx.dfyf>='+''''+formatDateTime('yyyy-mm',DateTimePicker1.Date)+''''+' and qfxx.dfyf<='+''''+FormatDateTime('yyyy-mm',DateTimePicker2.Date)+''''+') and (qfxx.xzdjsj>='+''''+formatDateTime('yyyy-mm-dd',DateTimePicker3.Date)+''''+' and qfxx.xzdjsj<='+''''+FormatDateTime('yyyy-mm-dd',DateTimePicker4.Date)+''''+')';
rs.Active:=false;
rs.SQL.Clear;
rs.SQL.Add(str);
rs.Active:=true;
///////////////////////////////////////////////////////////////////
rs.DisableControls;
i:=2;
while not rs.eof do
begin
stmp:=rs.fieldbyname('hh').AsString;
ew.Cells.Item[i,1]:=stmp ;
ew.Cells.Item[i,2]:=rs.fieldbyname('hm').AsString;
ew.Cells.Item[i,3]:=rs.fieldbyname('dz').AsString;
ew.Cells.Item[i,4]:=rs.fieldbyname('dfyf').AsString;
ew.Cells.Item[i,5]:=rs.fieldbyname('sjje').AsString;
i:=i+1;
rs.Next;
end;

rs.EnableControls;
rs.Close;
rs.Destroy;
ea.DisplayFullScreen[0]:=true;
ea.DisplayFullScreen[0]:=false;
ea.Visible[0]:=true;
end;
if (frmlogin.varqxinfo.dhhb=1) or (frmlogin.varqxinfo.dhzs=1)then
begin
str:='select qfxx.hh,qfxx.hm,yhda.dz,qfxx.dfyf,qfxx.hdf from qfxx,yhda where (yhda.hh=qfxx.hh) and (yhda.bmbs='+''''+combo_bmbs.Text+''''+') and (qfxx.dfyf>='+''''+formatDateTime('yyyy-mm',DateTimePicker1.Date)+''''+' and qfxx.dfyf<='+''''+FormatDateTime('yyyy-mm',DateTimePicker2.Date)+''''+') and ((qfxx.xzdjsj<='+''''+formatDateTime('yyyy-mm-dd',DateTimePicker5.Date)+''''+') ) and (qfxx.xzbz='+''''+'δÏúÕÊ'+''''+')';
rs.Active:=false;
rs.SQL.Clear;
rs.SQL.Add(str);
rs.Active:=true;
///////////////////////////////////////////////////////////////////
rs.DisableControls;
i:=2;
while not rs.eof do
begin
stmp:=rs.fieldbyname('hh').AsString;
ew.Cells.Item[i,1]:=stmp ;
ew.Cells.Item[i,2]:=rs.fieldbyname('hm').AsString;
ew.Cells.Item[i,3]:=rs.fieldbyname('dz').AsString;
ew.Cells.Item[i,4]:=rs.fieldbyname('dfyf').AsString;
ew.Cells.Item[i,5]:=rs.fieldbyname('hdf').AsString;
i:=i+1;
rs.Next;
end;
这是我的一些代码,请看看我的问题,为什么这么慢?
zhy2003 2003-01-22
  • 打赏
  • 举报
回复
Sfn:=ExtractFileDir(application.ExeName)+'\templates\Êý¾Ýµ¼³öÄ£°å.xls';
Dfn:=ExtractFileDir(application.ExeName)+'\reports\Êý¾Ýµ¼³öÄ£°å.xls';

opInfo.Wnd:=Handle;
opinfo.fAnyOperationsAborted:=false;
opInfo.lpszProgressTitle:='';
opInfo.pFrom:=pChar(sFn);
OPInfo.pTo:=pChar(dFn);

if (SHFileOperation(opinfo)<>0) then
begin
ShowMessage('Îļþ²Ù×÷ʧ°Ü£¬ÏµÍ³½«Í˳ö£¡');
exit;
end;

////////////////////////////////////////////////////////////////
try
ea.Connect;
except
end;
ea.Visible[0]:=false;
eb.ConnectTo(ea.Workbooks.Open(dfn,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,0));
ew.ConnectTo(eb.activesheet as _worksheet);

rs:=Tquery.Create(application);
rs.databasename :='cnqf';
if ((frmlogin.varqxinfo.dhhb=0) and(frmlogin.varqxinfo.dhzs=0) and (frmlogin.varqxinfo.xhhb=0) and(frmlogin.varqxinfo.xhzs=0) and(frmlogin.varqxinfo.xtgly=1)and(frmlogin.varqxinfo.leader=0))or ((frmlogin.varqxinfo.xhcx=0) and (frmlogin.varqxinfo.dhcx=0))then
begin
if checkbox1.Checked and checkbox2.Checked and checkbox4.Checked then
begin
str:='select qfxx.hh,qfxx.hm,yhda.dz,qfxx.dfyf,qfxx.sjje from qfxx,yhda where (yhda.hh=qfxx.hh) and (yhda.bmbs='+''''+combo_bmbs.text+''''+')and (qfxx.dfyf>='+''''+formatDateTime('yyyy-mm',DateTimePicker1.Date)+''''+' and qfxx.dfyf<='+''''+FormatDateTime('yyyy-mm',DateTimePicker2.Date)+''''+') and (qfxx.xzdjsj>='+''''+formatDateTime('yyyy-mm-dd',DateTimePicker3.Date)+''''+' and qfxx.xzdjsj<='+''''+FormatDateTime('yyyy-mm-dd',DateTimePicker4.Date)+''''+')';
rs.Active:=false;
rs.SQL.Clear;
rs.SQL.Add(str);
rs.Active:=true;
///////////////////////////////////////////////////////////////////
rs.DisableControls;
i:=2;
while not rs.eof do
begin
stmp:=rs.fieldbyname('hh').AsString;
ew.Cells.Item[i,1]:=stmp ;
ew.Cells.Item[i,2]:=rs.fieldbyname('hm').AsString;
ew.Cells.Item[i,3]:=rs.fieldbyname('dz').AsString;
ew.Cells.Item[i,4]:=rs.fieldbyname('dfyf').AsString;
ew.Cells.Item[i,5]:=rs.fieldbyname('sjje').AsString;
i:=i+1;
rs.Next;
end;

rs.EnableControls;
rs.Close;
rs.Destroy;
ea.DisplayFullScreen[0]:=true;
ea.DisplayFullScreen[0]:=false;
ea.Visible[0]:=true;
end;
if (frmlogin.varqxinfo.dhhb=1) or (frmlogin.varqxinfo.dhzs=1)then
begin
str:='select qfxx.hh,qfxx.hm,yhda.dz,qfxx.dfyf,qfxx.hdf from qfxx,yhda where (yhda.hh=qfxx.hh) and (yhda.bmbs='+''''+combo_bmbs.Text+''''+') and (qfxx.dfyf>='+''''+formatDateTime('yyyy-mm',DateTimePicker1.Date)+''''+' and qfxx.dfyf<='+''''+FormatDateTime('yyyy-mm',DateTimePicker2.Date)+''''+') and ((qfxx.xzdjsj<='+''''+formatDateTime('yyyy-mm-dd',DateTimePicker5.Date)+''''+') ) and (qfxx.xzbz='+''''+'δÏúÕÊ'+''''+')';
rs.Active:=false;
rs.SQL.Clear;
rs.SQL.Add(str);
rs.Active:=true;
///////////////////////////////////////////////////////////////////
rs.DisableControls;
i:=2;
while not rs.eof do
begin
stmp:=rs.fieldbyname('hh').AsString;
ew.Cells.Item[i,1]:=stmp ;
ew.Cells.Item[i,2]:=rs.fieldbyname('hm').AsString;
ew.Cells.Item[i,3]:=rs.fieldbyname('dz').AsString;
ew.Cells.Item[i,4]:=rs.fieldbyname('dfyf').AsString;
ew.Cells.Item[i,5]:=rs.fieldbyname('hdf').AsString;
i:=i+1;
rs.Next;
end;
这是我的一些代码,请帮忙看看问题
ezlz 2003-01-22
  • 打赏
  • 举报
回复
//参数赋值
title:='序号,'+
'姓名,'+
'性别,'+
'身份证号码,'+
'出生日期,'+
'学历,'+
'毕业院校,'+
'所学专业,'+
'报考专业,'+
'职称,'+
'从事工程造价年限,'+
'是否取得造价工程师注册证书,'+
'造价工程师注册证书编号,'+
'注册日期,'+
'从业人员资格证书编号,'+
'取得资格方式,'+
'考试通过年度,'+
'考试档案号,'+
'人事档案关系,'+
'造价工程师资格证书编号,'+
'执业专用章编号,'+
'有效期,'+
'人事档案存档单位,'+
'存档单位联系电话,'+
'存档单位联系人,'+
'档案存档号';
sqlstr:='select px,xm,xb,sfzhm,csrq,xl,byyx,sxzy,xcszy,zc,csgczjnx,' +
'sfqdzjgcszczs,zjgcszczsbh,zcrq,cyryzgzsbh,qdzgfs,kstgnd,ksdah,rsdagx,' +
'zyzgzczbh,zyzyzbh,yxq,rsdacddw,cddwlxdh,cddwlxr,dacdh' +
' from ' + pre_database + 'zzzyjsryylb where scbz<>2 and jmsbh="' + G_zsbh +'" order by px';

//调用过程
toxls(title,sqlstr,'专职专业人员基本情况');

//说明:
//title是excel的每字段的标题,sqlstr是一sql语名。

//过程
procedure toxls(title:string;sqlstr:string;fname:string);
var
i,j:integer;
mem:tmemo;
str:string;
eclApp,WorkBook:Variant;
//声明为OLE Automation 对象
xlsFileName:string;
begin
with DM1.ADOQ_xls do
begin
close;
sql.clear;
sql.add(sqlstr);
open;
end;
try
//创建OLE对象Excel Application与 WorkBook
eclApp:=CreateOleObject('Excel.Application');
WorkBook:=CreateOleobject('Excel.Sheet');
except
application.MessageBox(pchar('您的机器里未安装 Microsoft Excel 。'),'提示信息',MB_ICONinformation+mb_ok);
Exit;
end;
try
workBook:=eclApp.workBooks.Add;
//显示表头
for j:=1 to DM1.ADOQ_xls.FieldCount do
eclApp.Cells(1,j):=FindStr(title,j);
//确定内容
DM1.ADOQ_xls.first;
while not DM1.ADOQ_xls.eof do
begin
for j:=0 to DM1.ADOQ_xls.FieldCount-1 do
begin
//if DM1.ADOQ_xls.Fields[j].DataType<>ftString then
// eclApp.Cells(DM1.ADOQ_xls.RecNo+1,j+1).type:='数值型'
//else
// eclApp.Cells(DM1.ADOQ_xls.RecNo+1,j+1).type:='文本格式';
eclApp.Cells(DM1.ADOQ_xls.RecNo+1,j+1):=dm1.ADOQ_xls.Fields[j].asstring;
end;
DM1.ADOQ_xls.next;
end;

fmain.s1.filename:=fname;
if not fmain.s1.execute then
exit;
WorkBook.saveas(fmain.s1.filename);
WorkBook.close;
eclApp.Quit;
//退出Excel Application
//释放VARIANT变量
eclApp:=Unassigned;
application.MessageBox(pchar('文件保存成功!'),'提示信息',MB_ICONinformation+mb_ok);
except
ShowMessage('不能正确操作Excel文件。可能是该文件已被其他程序打开, 或系统错误。');
WorkBook.close;
eclApp.Quit;
//释放VARIANT变量
eclApp:=Unassigned;
end;
end;

请试试。
murphy 2003-01-22
  • 打赏
  • 举报
回复
导数据到excel?
说清楚点儿!

不是有许多相应的控件嘛!?
zhy2003 2003-01-22
  • 打赏
  • 举报
回复
DM1.ADOQ_xls是什么意思?

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧