PB从EXCEL中导入数据到数据窗口的程序(2)

cfc_exceltodw(datawindow adw, string excelname)
参数是导出的dw和excel文件的完整路径

OLEObject ole_object
ole_object = CREATE OLEObject
integer li_ret1,value
string named

li_ret1 = ole_object.ConnectToObject("","Excel.Application")
IF li_ret1 = 0 THEN
value = MessageBox("提示", "你已打开Excel(注意保存),关闭吗?", Question!, YesNo!)
if value = 1 then
ole_object.quit()
ole_object.disconnectobject()
else
return 0
end if
end if

value = GetFileOpenName("Select File", excelname, named, "XLS", "Excel Files (*.XLS),*.XLS")
if value<>1 then
return 0
end if

li_ret1 = ole_object.ConnectToNewObject("Excel.Application")
if li_ret1 <>0 then
MessageBox('提示','OLE无法连接Execl!')
return 0
end if

openwithparm(w_daopop,1)
w_daopop.st_2.text='正在准备......'

ole_object.workbooks.open(excelname)
int rtn,i,j

long ll_colnum,ll_rownum
string ls_value

string ls_objects,ls_obj,ls_objs[],ls_objtag[]
long ll_pos,ll_len,ll_num = 0,ll_obj[]

ls_objects = trim(adw.Describe('datawindow.Objects'))+"~t"

do while (pos(ls_objects,"~t") > 0)
ll_pos = pos(ls_objects,"~t")
ll_len = ll_pos - 1
ls_obj = left(ls_objects,ll_len)
if (adw.Describe(ls_obj + '.type') = 'column' or adw.Describe(ls_obj + '.type') = 'compute') and (adw.Describe(ls_obj + '.band') = 'detail') and (ls_obj <> "asd") then
ll_num += 1
ls_objs[ll_num] = ls_obj
ls_objtag[ll_num] = adw.Describe(ls_obj + '_t.text')
end if
ls_objects = right(ls_objects,len(ls_objects) - ll_pos)
loop

string str,strtype
j=1
getremote("r1c1",str,"excel",excelname)
str= lower(left(trim(str),len(str)-2))
if len(str)=0 then
messagebox("错误","没有表头数据!")
ole_object.quit()
ole_object.disconnectobject()
destroy ole_object
close(w_daopop)
return 0
end if

do while len(str)>0
i=1
do while lower(trim(ls_objtag[i])) <> str
   i= i+1
if i > ll_num then
messagebox("错误","没有找到"+str+"列!")
ole_object.quit()
ole_object.disconnectobject()
destroy ole_object
close(w_daopop)
return 0
end if
loop
ll_obj[j]=i
j=j + 1
getremote("r1c"+string(j),str,"excel",excelname)
str= lower(left(trim(str),len(str)-2))
loop
ll_num = j - 1
i=2
getremote("r"+string(i)+"c1",str,"excel",excelname)
str= left(str,len(str)-2)
do while len(str)>0
ll_rownum=adw.insertrow(0)
w_daopop.st_2.text='正在导入第'+string(i - 1)+'条记录......'
for j=1 to ll_num
getremote("r"+string(i)+"c"+string(j),str,"excel",excelname)
str= left(str,len(str)-2)
strtype = adw.Describe( ls_objs[ll_obj[j]] + '.coltype')
ll_pos = pos(strtype,"(")
if ll_pos<>0 then strtype=left(strtype,ll_pos - 1)
CHOOSE CASE strtype
CASE 'number','decimal','long'
adw.setitem(ll_rownum,ll_obj[j],dec(str))
case 'date'
adw.setitem(ll_rownum,ll_obj[j],date(str))
case 'datetime'
adw.setitem(ll_rownum,ll_obj[j],datetime(date(str)))
CASE ELSE
adw.setitem(ll_rownum,ll_obj[j],str)
END CHOOSE
next
i=i+1
getremote("r"+string(i)+"c1",str,"excel",excelname)
str= left(str,len(str)-2)
loop
ole_object.quit()
ole_object.disconnectobject()
destroy ole_object
close(w_daopop)
return 1

你可能感兴趣的:(报表,PowerBuilder)