如何将PB的交叉报表转换成EXCEL形式

如何将PB的交叉报表转换成EXCEL形式

如何将PB的交叉报表转换成EXCEL形式,用的办法是写一个通用的转换函数


主函数部分:(还用到了PFC的字串处理的一个函数和在本对象中的一个取值函数)

 
  1. //====================================================================

  2. // [PUBLIC] Function uf_data2excel 在 u_data2word inherited from nonvisualobject

  3. //--------------------------------------------------------------------

  4. // 说明:将数据倒入excel中,支持计算列及显示格式,要求在题头的计算列要写tag值

  5. //--------------------------------------------------------------------

  6. // 参数1:[value] datawindow adw

  7. // 说明:数据窗口

  8. //--------------------------------------------------------------------

    扫描二维码关注公众号,回复: 5542807 查看本文章
  9. // 返回: (INTEGER) 成功返回1,不成功返回0

  10. //--------------------------------------------------------------------

  11. // 作者: cwl 日期: 2002.03.18

  12. //====================================================================

  13. //变更日志:020515加入对交叉表倒出的支持(主要是修改了保存题头部分)

  14.  
  15. constant integer ppLayoutBlank = 12

  16. OLEObject ole_object

  17. ole_object = CREATE OLEObject

  18.  
  19. integer li_ret,li_crosstab=0

  20. long ll_colnum,ll_rownum

  21. string ls_value

  22. string ls_objects,ls_obj,ls_objs[],ls_objtag[]

  23. long ll_pos,ll_len,ll_num = 0

  24. //题头区

  25. long ll_headnum

  26. string ls_head[],ls_headtag[]

  27. //合计区

  28. long ll_sumnum,i=1,startpos=1,endpos,li_pos

  29. string ls_sum[],ls_sumtag[],ls_bind,token[],list,ls_temp,ls_crosstabcol

  30. n_cst_string lu_string //PFC string处理对象

  31.  
  32. li_ret = ole_object.ConnectToObject("","Excel.Application")

  33. IF li_ret <> 0 THEN

  34. //如果Excel还没有打开,则新建。

  35. li_ret = ole_object.ConnectToNewObject("Excel.Application")

  36. if li_ret <> 0 then

  37. MessageBox('OLE错误','OLE无法连接!错误号:' + string(li_ret))

  38. return 0

  39. end if

  40. ole_object.Visible = false//不可见

  41. END IF

  42.  
  43. if adw.Object.DataWindow.Processing='4' then //交叉表处理

  44. adw.Object.DataWindow.Crosstab.StaticMode='true'//将数据静态化

  45. li_crosstab=1

  46. end if

  47.  
  48. pointer oldpointer

  49. oldpointer = SetPointer(HourGlass!)

  50.  
  51. //新增一个工作区

  52. ole_object.Workbooks.Add

  53.  
  54.  
  55.  
  56. ls_objects = trim(adw.Describe('datawindow.Objects'))

  57. list=ls_objects

  58. EndPos = pos(list, '~t', StartPos)

  59. //得到对象列表

  60. Do while ( EndPos > 0 )

  61. token[i] = Mid(list, StartPos, EndPos - StartPos)

  62. i ++

  63. StartPos = EndPos + 1

  64. EndPos = pos(list, '~t', StartPos)

  65. LOOP

  66. token[i] = Mid(list, StartPos)

  67. ll_rownum=UpperBound(token)

  68.  
  69. for i=1 to ll_rownum

  70. ls_obj = token[i]

  71. if ls_obj='title' then messagebox('',adw.Describe(ls_obj + '.type'))

  72. if lower(adw.Describe(ls_obj + '.type')) = 'column' or &

  73. lower(adw.Describe(ls_obj + '.type')) = 'compute' then

  74. ls_bind=lower(adw.Describe(ls_obj + '.band'))

  75. if ls_bind = 'detail' then

  76. ll_num += 1

  77. ls_objs[ll_num] = ls_obj

  78. if li_crosstab=0 then //一般处理

  79. ls_objtag[ll_num] = adw.Describe(ls_obj + '_t.text')

  80. elseif li_crosstab=1 then //交叉表处理

  81. li_pos=lu_string.of_lastpos(ls_obj,'_',len(ls_obj))//找出最后一次出现'_'的位置

  82. if li_pos=0 or (not isnumber(mid(ls_obj,li_pos+1))) then //不是交叉列

  83. ls_objtag[ll_num] = adw.Describe(ls_obj + '_t.text')

  84. else

  85. ls_temp=mid(ls_obj,li_pos)

  86. ls_crosstabcol=mid(ls_obj,1,li_pos - 1)//取出交叉列名

  87. // messagebox('',ls_crosstabcol+',,,,'+ls_temp)

  88. ls_objtag[ll_num]=adw.Describe( ls_crosstabcol + "_t"+ls_temp+".Text" )//取出交叉表的题头

  89. end if

  90. end if

  91. elseif (ls_bind = 'summary') then

  92. ll_sumnum += 1

  93. ls_sum[ll_sumnum] = ls_obj

  94. ls_sumtag[ll_sumnum] = adw.Describe(ls_obj + '.tag')

  95. else

  96. ll_headnum += 1

  97. ls_head[ll_headnum] = ls_obj

  98. ls_headtag[ll_headnum] = adw.Describe(ls_obj + '.tag')

  99. end if

  100.  
  101. end if

  102.  
  103. next

  104.  
  105. //得到数据窗口数据的列数与行数(行数应该是数据行数 + 2)

  106. ll_colnum = ll_num

  107. ll_rownum = adw.rowcount() + 2

  108.  
  109. string column_name

  110. string ls_colname

  111. integer j,k

  112. //写题头

  113. for i=1 to ll_headnum

  114. ls_value = ls_headtag[i]

  115. if ls_value<>'?' then

  116. ole_object.cells(1,(i - 1)*2+1).value = ls_value

  117. end if

  118. column_name = ls_head[i]

  119. ls_value=this.uf_getdata(adw,column_name,1)

  120. ole_object.cells(1,(i)*2).value = ls_value

  121. next

  122. //写结尾

  123. for i=1 to ll_sumnum

  124. ls_value = ls_sumtag[i]

  125. if ls_value<>'?' then

  126. ole_object.cells(ll_rownum+1,(i - 1)*2+1).value = ls_value

  127. end if

  128. column_name = ls_sum[i]

  129. ls_value=this.uf_getdata(adw,column_name,1)

  130. ole_object.cells(ll_rownum+1,(i)*2).value = ls_value

  131. next

  132.  
  133. //写标题

  134. for i = 1 to ll_colnum

  135. //得到标题头的名字

  136. ls_value = ls_objtag[i]

  137. ole_object.cells(2,i).value = ls_value

  138. next

  139. //写数据

  140. for i = 3 to ll_rownum

  141. for j = 1 to ll_colnum

  142. column_name = ls_objs[j]

  143. ls_value=this.uf_getdata(adw,column_name,i - 2)

  144. ole_object.cells(i,j).value = ls_value

  145. next

  146. next

  147.  
  148. SetPointer(oldpointer)

  149. ole_object.Visible = True

  150. ole_object.disconnectobject()

  151. DESTROY ole_object

  152.  
  153. return 1

  154.  

猜你喜欢

转载自blog.csdn.net/u010561878/article/details/88570254