如何将PB的交叉报表转换成EXCEL形式
如何将PB的交叉报表转换成EXCEL形式,用的办法是写一个通用的转换函数
主函数部分:(还用到了PFC的字串处理的一个函数和在本对象中的一个取值函数)
-
//====================================================================
-
// [PUBLIC] Function uf_data2excel 在 u_data2word inherited from nonvisualobject
-
//--------------------------------------------------------------------
-
// 说明:将数据倒入excel中,支持计算列及显示格式,要求在题头的计算列要写tag值
-
//--------------------------------------------------------------------
-
// 参数1:[value] datawindow adw
-
// 说明:数据窗口
-
//--------------------------------------------------------------------
扫描二维码关注公众号,回复: 5542807 查看本文章 -
// 返回: (INTEGER) 成功返回1,不成功返回0
-
//--------------------------------------------------------------------
-
// 作者: cwl 日期: 2002.03.18
-
//====================================================================
-
//变更日志:020515加入对交叉表倒出的支持(主要是修改了保存题头部分)
-
constant integer ppLayoutBlank = 12
-
OLEObject ole_object
-
ole_object = CREATE OLEObject
-
integer li_ret,li_crosstab=0
-
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
-
//题头区
-
long ll_headnum
-
string ls_head[],ls_headtag[]
-
//合计区
-
long ll_sumnum,i=1,startpos=1,endpos,li_pos
-
string ls_sum[],ls_sumtag[],ls_bind,token[],list,ls_temp,ls_crosstabcol
-
n_cst_string lu_string //PFC string处理对象
-
li_ret = ole_object.ConnectToObject("","Excel.Application")
-
IF li_ret <> 0 THEN
-
//如果Excel还没有打开,则新建。
-
li_ret = ole_object.ConnectToNewObject("Excel.Application")
-
if li_ret <> 0 then
-
MessageBox('OLE错误','OLE无法连接!错误号:' + string(li_ret))
-
return 0
-
end if
-
ole_object.Visible = false//不可见
-
END IF
-
if adw.Object.DataWindow.Processing='4' then //交叉表处理
-
adw.Object.DataWindow.Crosstab.StaticMode='true'//将数据静态化
-
li_crosstab=1
-
end if
-
pointer oldpointer
-
oldpointer = SetPointer(HourGlass!)
-
//新增一个工作区
-
ole_object.Workbooks.Add
-
ls_objects = trim(adw.Describe('datawindow.Objects'))
-
list=ls_objects
-
EndPos = pos(list, '~t', StartPos)
-
//得到对象列表
-
Do while ( EndPos > 0 )
-
token[i] = Mid(list, StartPos, EndPos - StartPos)
-
i ++
-
StartPos = EndPos + 1
-
EndPos = pos(list, '~t', StartPos)
-
LOOP
-
token[i] = Mid(list, StartPos)
-
ll_rownum=UpperBound(token)
-
for i=1 to ll_rownum
-
ls_obj = token[i]
-
if ls_obj='title' then messagebox('',adw.Describe(ls_obj + '.type'))
-
if lower(adw.Describe(ls_obj + '.type')) = 'column' or &
-
lower(adw.Describe(ls_obj + '.type')) = 'compute' then
-
ls_bind=lower(adw.Describe(ls_obj + '.band'))
-
if ls_bind = 'detail' then
-
ll_num += 1
-
ls_objs[ll_num] = ls_obj
-
if li_crosstab=0 then //一般处理
-
ls_objtag[ll_num] = adw.Describe(ls_obj + '_t.text')
-
elseif li_crosstab=1 then //交叉表处理
-
li_pos=lu_string.of_lastpos(ls_obj,'_',len(ls_obj))//找出最后一次出现'_'的位置
-
if li_pos=0 or (not isnumber(mid(ls_obj,li_pos+1))) then //不是交叉列
-
ls_objtag[ll_num] = adw.Describe(ls_obj + '_t.text')
-
else
-
ls_temp=mid(ls_obj,li_pos)
-
ls_crosstabcol=mid(ls_obj,1,li_pos - 1)//取出交叉列名
-
// messagebox('',ls_crosstabcol+',,,,'+ls_temp)
-
ls_objtag[ll_num]=adw.Describe( ls_crosstabcol + "_t"+ls_temp+".Text" )//取出交叉表的题头
-
end if
-
end if
-
elseif (ls_bind = 'summary') then
-
ll_sumnum += 1
-
ls_sum[ll_sumnum] = ls_obj
-
ls_sumtag[ll_sumnum] = adw.Describe(ls_obj + '.tag')
-
else
-
ll_headnum += 1
-
ls_head[ll_headnum] = ls_obj
-
ls_headtag[ll_headnum] = adw.Describe(ls_obj + '.tag')
-
end if
-
end if
-
next
-
//得到数据窗口数据的列数与行数(行数应该是数据行数 + 2)
-
ll_colnum = ll_num
-
ll_rownum = adw.rowcount() + 2
-
string column_name
-
string ls_colname
-
integer j,k
-
//写题头
-
for i=1 to ll_headnum
-
ls_value = ls_headtag[i]
-
if ls_value<>'?' then
-
ole_object.cells(1,(i - 1)*2+1).value = ls_value
-
end if
-
column_name = ls_head[i]
-
ls_value=this.uf_getdata(adw,column_name,1)
-
ole_object.cells(1,(i)*2).value = ls_value
-
next
-
//写结尾
-
for i=1 to ll_sumnum
-
ls_value = ls_sumtag[i]
-
if ls_value<>'?' then
-
ole_object.cells(ll_rownum+1,(i - 1)*2+1).value = ls_value
-
end if
-
column_name = ls_sum[i]
-
ls_value=this.uf_getdata(adw,column_name,1)
-
ole_object.cells(ll_rownum+1,(i)*2).value = ls_value
-
next
-
//写标题
-
for i = 1 to ll_colnum
-
//得到标题头的名字
-
ls_value = ls_objtag[i]
-
ole_object.cells(2,i).value = ls_value
-
next
-
//写数据
-
for i = 3 to ll_rownum
-
for j = 1 to ll_colnum
-
column_name = ls_objs[j]
-
ls_value=this.uf_getdata(adw,column_name,i - 2)
-
ole_object.cells(i,j).value = ls_value
-
next
-
next
-
SetPointer(oldpointer)
-
ole_object.Visible = True
-
ole_object.disconnectobject()
-
DESTROY ole_object
-
return 1