將PB的交叉報表完整轉換成EXCEL

如何將PB的交叉報表轉換成EXCEL形式(包括標題和合計的完整形式)

函數部分:(還用到了PFC的字串處理的一個函數和在本對象中的一個取值函數)
//====================================================================
// [PUBLIC] Function uf_data2excel 在 u_data2word inherited from nonvisualobject
//--------------------------------------------------------------------
// 說明:將數據倒入excel中,支持計算列及顯示格式,要求在題頭的計算列要寫tag值
//--------------------------------------------------------------------
// 參數1:[value] datawindow adw
//  說明:數據窗口
//--------------------------------------------------------------------
// 返回: (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

------------------------

PFC的字串處理函數(可直接改成全局函數就可用了)
//====================================================================
// [PUBLIC] Function of_lastpos 在 n_cst_string inherited from n_ccu_base
//--------------------------------------------------------------------
// 說明:找出一字串在另一串中最後出現的位置
//--------------------------------------------------------------------
// 參數1:[value] string as_source
//  說明:源串
// 參數2:[value] string as_target
//  說明:目標串
// 參數3:[value] long al_start
//  說明:開始位置
//--------------------------------------------------------------------
// 返回: (LONG) 從右數起出現的位置,找不到則返回0
//--------------------------------------------------------------------

Long ll_Cnt, ll_Pos

//Check for Null Parameters.
IF IsNull(as_source) or IsNull(as_target) or IsNull(al_start) Then
 SetNull(ll_Cnt)
 Return ll_Cnt
End If

//Check for an empty string
If Len(as_Source) = 0 Then
 Return 0
End If

// Check for the starting position, 0 means start at the end.
If al_start=0 Then 
 al_start=Len(as_Source)
End If

//Perform find
For ll_Cnt = al_start to 1 Step -1
 ll_Pos = Pos(as_Source, as_Target, ll_Cnt)
 If ll_Pos = ll_Cnt Then
  //String was found
  Return ll_Cnt
 End If
Next

//String was not found
Return 0
--------------------------------

得到一個數據窗口列及計算列的準確顯示值函數:
//====================================================================
// [PUBLIC] Function uf_getdata 在 u_data2word inherited from nonvisualobject
//--------------------------------------------------------------------
// 說明:得到一個數據窗口列及計算列的準確顯示值
//--------------------------------------------------------------------
// 參數1:[value] datawindow dw_1
//  說明:
// 參數2:[value] string col
//  說明:對象名
// 參數3:[value] integer row
//  說明:行
//--------------------------------------------------------------------
// 返回: (STRING) 值
//--------------------------------------------------------------------
// 作者: cwl  日期: 2002.03.18
//====================================================================
string ls_edittype,ls_value,ls_format
integer id
ls_edittype=lower(dw_1.Describe(col+".Edit.Style"))//得到編緝風格
choose case ls_edittype
 case 'ddlb','dddw'//應該得到顯示值
  ls_value=dw_1.describe(  "Evaluate('LookUpDisplay("+col+") ',"+string(row)+" )")
 case else
  id=long(dw_1.Describe(col+".id"))
  ls_format=dw_1.Describe(col+".Format")
  if mid(ls_format,1,1)='[' or ls_format='?' or ls_format='' then //不作格式處理
   if id=0 then //計算列
    ls_value=dw_1.Describe("Evaluate(~"" + dw_1.Describe(col + '.expression')&
     + "~","+string(row)+")")
   else
    ls_value=string(dw_1.object.data[row,id])
   end if
  else
   if id=0 then //計算列
    ls_value=string(dw_1.Describe("Evaluate('" + dw_1.Describe(col + '.expression')&
     + "',"+string(row)+")"),ls_format)
   else
    ls_value=string(dw_1.object.data[row,id],ls_format)
   end if
  end if
end choose
if isnull(ls_value) then ls_value=''
return ls_value
--------------------------

最簡單的解決辦法,把dw存成html文件,然後改擴展名爲.xls,一切ok了

發佈了7 篇原創文章 · 獲贊 0 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章