PB數據窗口導出到EXCEL函數

function f_oletoexcel(adw datawindow,title string,as_file string)

Oleobject ole_object
String    s_english="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
String    ls_obj,ls_objs[],ls_objtag[],ls_width[],ls_value,column_name,ls_coltype,ls_range
Integer   li_ret,i,li_grouprow,li_newgrouprow,j,row,groupflag,detailflag,sumflag
Long      ll_row,ll_num,ll_column,ll_groupnum,ll_sumrow
Int li_rtn=-9
dec       ld_width
Pointer   oldpointer
groupflag=0
detailflag=1
sumflag=1
/*-------打開EXCELL文檔-------*/


ole_object = CREATE OLEObject
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))
      li_rtn = -1
  goto totheend
   end if
  
   ole_object.Visible = False  //一般自動保存時設爲不可視
END IF
/*-------打開文檔完畢-------*/


oldpointer = SetPointer(HourGlass!)
ole_object.Workbooks.Add
ll_row = 1
ll_column =long(adw.Object.DataWindow.Column.Count)
ll_num = 1
//"#1"可以指向第一列,#2則指向datawindow的第二列
FOR i = 1 TO ll_column
        IF adw.Describe("#"+String(i)+".Visible")="1" and adw.Describe("#"+String(i)+".Band")="detail" THEN 
  yield()
  ls_obj = adw.Describe("#"+String(i)+".name")   
  ls_objs[ll_num] = ls_obj
  ls_objtag[ll_num]=adw.Describe(ls_obj+"_t.text")      //字段名稱的顯示值
//  messagebox(ls_obj,adw.Describe(ls_obj+"_t.text"))
   ls_width[ll_num]= adw.Describe(ls_obj  + '.width')         //每列的寬度
  ll_num++
 END IF
NEXT
//messagebox("",ll_column)
ll_column = upperbound(ls_objs)

if ll_column <= 26 then
     ls_range = Char(64+ll_column)
else
     ls_range = char(64+integer((ll_column - 1)/26))+char(64+integer(mod((ll_column - 1),26)+1))
end if

IF trim(title)<>"" THEN
 ll_row = 2
 /*-------生成標題-------*/

 ole_object.Cells(1,1).Value =title
 ole_object.Range('A1').Select
 ole_object.Selection.Font.Size =24
 ole_object.selection.HorizontalAlignment =3
 ole_object.Range('A1:'+ls_range+'1').Select
 ole_object.Range('A1:'+ls_range+'1').Merge

END IF
/*-------設置標題欄-------*/
for i = 1 to ll_column
 yield()

   ls_value = ls_objtag[i]
   ole_object.cells(ll_row,i).value = ls_value  //1
   if isnull(ls_width[i]) then
  ld_width=12
 else
  ld_width=dec(ls_width[i])/35
 end if
 ole_object.Columns(i).ColumnWidth=ld_width
 ole_object.Columns(i).HorizontalAlignment =3
 ole_object.Columns(i).Borders.LineStyle= 1
 ole_object.Columns(i).Font.Bold =True
next
IF groupflag = 1 and detailflag = 0 THEN
 ll_row ++
END IF

li_grouprow = 0
ll_groupnum = 0
IF groupflag = 1 THEN 

 FOR i = 1 to adw.rowcount()
  li_newgrouprow = adw.findgroupchange(i,1)
  IF li_newgrouprow = i THEN
   IF li_grouprow > 0 THEN
    row = i*detailflag + ll_row + ll_groupnum
    for j = 1 to ll_column
     yield()

         column_name = ls_objs[j]+"_c"
         ls_value = adw.Describe("Evaluate('" + adw.Describe(column_name + '.expression') + "',"+string(li_grouprow)+")")
         ls_coltype=adw.Describe(column_name+'.coltype')
     IF isnull(ls_value) or ls_value="!" or ls_value = "?" THEN
          ls_value = ""
         END IF
         if pos(upper(ls_coltype),"CHAR")>0 then     //對字符型數據處理
            ole_object.cells(row,j).NumberFormat ="@"
         end if
         ole_object.cells(row,j).Font.Bold =true
           ole_object.cells(row,j).value = ls_value
      next
    ll_groupnum++
    li_grouprow = li_newgrouprow
   ELSE
    li_grouprow = li_newgrouprow
   END IF
   IF detailflag = 1 THEN
    row = i + ll_row + ll_groupnum
    for j = 1 to ll_column
     yield()

           column_name = ls_objs[j]
          if adw.Describe(column_name + '.type') = 'column' then
             ls_value = adw.Describe("Evaluate('LookupDisplay("+column_name+")',"+string(i)+")")
          end if
          if adw.Describe(column_name + '.type') = 'compute' then
             ls_value = adw.Describe("Evaluate('" + adw.Describe(column_name + '.expression') + "',"+string(i)+")")
          end if
            IF isnull(ls_value) or ls_value="!" or ls_value = "?" THEN
           ls_value = ""
          END IF
          ls_coltype=adw.Describe(column_name+'.coltype')
          if pos(upper(ls_coltype),"CHAR")>0 then     //對字符型數據處理
             ole_object.cells(row,j).NumberFormat ="@"
          end if
          ole_object.cells(row,j).Font.Bold =false
          ole_object.cells(row,j).value = ls_value
     next
   END IF
  ELSE
   IF detailflag = 1 THEN  
    row = i + ll_row + ll_groupnum
    for j = 1 to ll_column
        yield()

         column_name = ls_objs[j]
         if adw.Describe(column_name + '.type') = 'column' then
            ls_value = adw.Describe("Evaluate('LookupDisplay("+column_name+")',"+string(i)+")")
         end if
         if adw.Describe(column_name + '.type') = 'compute' then
            ls_value = adw.Describe("Evaluate('" + adw.Describe(column_name + '.expression') + "',"+string(i)+")")
         end if
           IF isnull(ls_value) or ls_value="!" or ls_value = "?" THEN
          ls_value = ""
         END IF
         ls_coltype=adw.Describe(column_name+'.coltype')
         if pos(upper(ls_coltype),"CHAR")>0 then     //對字符型數據處理
            ole_object.cells(row,j).NumberFormat ="@"
         end if
         ole_object.cells(row,j).Font.Bold =false
         ole_object.cells(row,j).value = ls_value
    next
    END IF
  END IF
 NEXT
 ll_sumrow = i*detailflag + ll_row + ll_groupnum
 IF groupflag = 1 THEN
    row = i*detailflag + ll_row + ll_groupnum
  ll_sumrow++
  for j = 1 to ll_column
    yield()

      column_name = ls_objs[j]+"_c"
    ls_value = adw.Describe("Evaluate('" + adw.Describe(column_name + '.expression') + "',"+string(li_grouprow)+")")
      ls_coltype=adw.Describe(column_name+'.coltype')
    IF isnull(ls_value) or ls_value="!" or ls_value = "?" THEN
     ls_value = ""
    END IF
        if pos(upper(ls_coltype),"CHAR")>0 then     //對字符型數據處理
       ole_object.cells(row,j).NumberFormat ="@"
      end if
    ole_object.cells(row,j).Font.Bold =true
    ole_object.cells(row,j).value = ls_value
   next
 END IF
ELSE 
 FOR i = 1 TO adw.rowcount()
    for j = 1 to ll_column
    yield()
    column_name = ls_objs[j]
     if adw.Describe(column_name + '.type') = 'column' then
   
           ls_value = adw.Describe("Evaluate('LookupDisplay("+column_name+")',"+string(i)+")")  //得到所見字段數據
    
      end if
        if adw.Describe(column_name + '.type') = 'compute' then
            ls_value = adw.Describe("Evaluate('" + adw.Describe(column_name + '.expression') + "',"+string(i)+")")//得到所見計算字段數據
      end if
          IF isnull(ls_value) or ls_value="!" or ls_value = "?" THEN
         ls_value = ""
      END IF
        ls_coltype=adw.Describe(column_name+'.coltype')
      if pos(upper(ls_coltype),"CHAR")>0 then     //對字符型數據處理
             ole_object.cells(ll_row+i,j).NumberFormat ="@"
      end if
        ole_object.cells(ll_row+i,j).Font.Bold =false
      ole_object.cells(ll_row+i,j).value = ls_value
    next
 NEXT
 ll_sumrow = ll_row + i
END IF
debugbreak()
IF sumflag = 1 THEN
   for j = 1 to ll_column
   yield()

     column_name = ls_objs[j]+"_sum"

  if adw.Describe(column_name+".Name") <>"!" then
   ls_value = adw.Describe("Evaluate('" + adw.Describe(column_name + '.expression') + "',"+string(1)+")")
   ls_coltype=adw.Describe(column_name+'.coltype')
   IF isnull(ls_value) or ls_value="!" or ls_value = "?" THEN
    ls_value = ""
   END IF
     if pos(upper(ls_coltype),"CHAR")>0 then     //對字符型數據處理
    ole_object.cells(ll_sumrow,j).NumberFormat ="@"
   end if
   ole_object.cells(ll_sumrow,j).Font.Bold =true
   ole_object.cells(ll_sumrow,j).value = ls_value
 end if
 next
END IF

if FileExists ( as_file ) then
 if messagebox("Microsoft Excel","在當前位置發現已經存在名爲"+as_file+" 的文件。您希望將該文件替換掉嗎?", &
 Question!,YesNoCancel!) = 1 then 
 if not FileDelete ( as_file ) then
  messagebox("錯誤","文件"+as_file+"刪除失敗;~r~n請確定磁盤未滿或未被寫保護且文件未被使用。!")
   li_rtn = -1
  goto totheend
 end if
    else
     li_rtn = -1
  goto totheend
 end if
end if
ole_object.ActiveWorkbook.saveas(as_file)
ole_object.Displayalerts = FALSE //關閉在退出EXCEL時的保存提示
if IsValid ( w_prompt ) then close(w_prompt);

   If MessageBox('系統提示','報表已成功導出,是否現在查看導出結果?',Question!,YesNo!)=1 Then
    ole_object.Visible = True
   Else
    ole_object.Quit()                //退出EXCEL
   End If


li_rtn = 1
totheend:
if IsValid ( w_prompt ) then close(w_prompt);   //w_prompt是一個信息顯示窗體
SetPointer(oldpointer)
If IsValid(ole_object) Then ole_object.disconnectobject()
If IsValid(ole_object) Then DESTROY ole_object
//of_waitfor('','')
return li_rtn


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章