把数据一条一条写入excel解决方法
把数据一条一条写入excel
想把数据一条一条写入excel
constant integer ppLayoutBlank = 12
OLEObject oleobject
oleobject = CREATE OLEObject
integer liret
liret = oleobject.ConnectToObject("d:\test.xls ", "Excel.Application ")
IF liret <> 0 THEN
//如果Excel还没有打开,则新建。
liret = oleobject.ConnectToNewObject( "Excel.Application ")
if liret <> 0 then
MessageBox( 'OLE错误 ', 'OLE无法连接!错误号: ' + string(liret))
return
end if
这里就出现了问题,li_ret = -2
我用的是pb10.5 ,求大虾帮忙解决.
PowerBuilder语言基础 PowerBuilder 程序开发
Answers
在这个程序吧,可以正常执行的。
String lsFileName,lsorifile
oleobject ole1
int lii,lirowcount,licurrow,liretvalue,lizt
dec ldcje
if dw1.rowcount() > 0 then
if not directoryexists('d:\资产报表') then
if createdirectory('d:\资产报表') <> 1 then
messagebox('提示信息','生成d:\资产报表目录失败,请手工创建!')
return
end if
end if
lsfilename = 'd:\资产报表\固定资产库存清单' + string(today(),'yymmdd') + '.xls'
lsorifile = esheererp.ofgetapppath() + '\excel templet\固定资产库存清单.xls'
if fileexists(lsFileName) Then
if messagebox('提示信息',lsFileName+'文件已经存在,要覆盖吗?',Question!,YesNo!,1) = 2 then Return
End If
if fileexists(lsorifile) then
if FileCopy(lsorifile,lsFileName,true) = 1 Then
Else
messagebox('提示信息','生成'+lsFileName+'文件失败!')
End If
else
messagebox('提示信息','找不到'+lsorifile +'模板文件,不能另存为excel!')
return
end if
lirowcount = dw1.rowcount()
ole1 = create oleobject
liRetvalue = ole1.Connecttonewobject("Excel.Application")
if liRetValue <> 0 then
MessageBox('提示信息','不能联接到EXCEL,请确认你的机器上已经安装Excel!')
Return
end if
ole1.Visible = False
ole1.workbooks.open(lsFileName)
ole1.Application.Cells(1,1).value = dw1.object.ttitle.text//设置标题
ole1.Application.Cells(2,1).value = dw1.object.t1.text//设置统计条件
ole1.Application.Cells(2,7).value = dw1.getitemstring(1,'computedate')//设置日期
ole1.Application.Cells(7,7).value = dw1.object.tuser.text//设置制表人
ole1.Application.Cells(6,4).value = dw1.getitemdecimal(1,"compute4")
ole1.Application.Cells(6,6).value = dw1.getitemdecimal(1,"compute5")
ole1.Application.Cells(6,8).value = dw1.getitemdecimal(1,"compute6")
if lirowcount > 2 then
for lii = 1 to lirowcount - 2
ole1.Application.Rows( "5:5").Select
ole1.Selection.Insert()
next
end if
licurrow = 3
werpmainc.ofSetBarMax(liRowCount)
for lii = 1 to lirowcount
werpmainc.ofStateBarStep()
licurrow ++
ole1.Application.Cells(licurrow,1).value = dw1.object.daima[lii]
ole1.Application.Cells(licurrow,2).value = dw1.object.wupinmingcheng[lii]
ole1.Application.Cells(licurrow,3).value = dw1.object.jiliangdangwei[lii]
ole1.Application.Cells(licurrow,4).value = dw1.object.shuliang[lii]
ole1.Application.Cells(licurrow,5).value = dw1.object.kucunjine[lii]
ole1.Application.Cells(licurrow,6).value = dw1.object.lingyongshulian[lii]
ole1.Application.Cells(licurrow,7).value = dw1.object.lingyongjine[lii]
ole1.Application.Cells(licurrow,8).value = dwpute1[li_i]