powerbulider數據窗口轉microsoft execl、word程序源代碼
一、f_cncharnum函數 f_cncharnum.srf
$pbexportheader$f_cncharnum.srf
$pbexportcomments$得到字符串中漢字或者雙字節的個數
global type f_cncharnum from function_object
end type
forward prototypes
global function integer f_cncharnum (string astring)
end prototypes
global function integer f_cncharnum (string astring);
//函數名: f_cncharnum
//用途: 返回一個字符串中漢字的個數
//輸入: astring - string, 給定的字符串
//返回值: li_num - integer, 給定的字符串中漢字的個數
//注意: 1. 此方法基于漢字的國標漢字庫區位編碼的有效性,不符合此編碼的系統此函數無效!
// 2. 若漢字串含有非漢字字符,如圖形符號或ascii碼,則這些非漢字字符將保持不變.
//例如: li_ret = f_cncharnum("擺渡人ferryman") li_ret = 3
string ls_ch //臨時單元
string ls_secondsectable //存放所有國標二級漢字讀音
integer li_num = 0 //返回值
integer i,j
for i = 1 to len(astring)
ls_ch = mid(astring,i,1)
if asc(ls_ch) >= 128 then //是漢字
li_num++
i = i+1
end if
next
return li_num
end function
二、pbtoexcel函數f_outputtoexcel_new.srf
$pbexportheader$f_outputtoexcel_new.srf
global type f_outputtoexcel_new from function_object
end type
forward prototypes
global function integer f_outputtoexcel_new (datawindow adw)
end prototypes
global function integer f_outputtoexcel_new (datawindow adw);
//函數名:f_outputtoexcel_new
//輸入: adw - datawindow,指定的數據窗口
//返回值: integer
constant integer pplayoutblank = 12
oleobject ole_object
ole_object = create oleobject
integer li_ret
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 = true
end if
pointer oldpointer
oldpointer = setpointer(hourglass!)
ole_object.workbooks.add
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
ls_objects = trim(adw.describe('datawindow.objects'))
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 + '.tag')
end if
ls_objects = right(ls_objects,len(ls_objects) - ll_pos)
loop
//得到數據窗口數據的列數與行數(行數應該是數據行數 + 1)
ll_colnum = ll_num
ll_rownum = adw.rowcount() + 1
string ls_colname
integer i,j,k
for i = 1 to ll_colnum
//得到標題頭的名字
ls_value = ls_objtag[i]
ole_object.cells(1,i).value = ls_value
next
string column_name
for i = 2 to ll_rownum
for j = 1 to ll_colnum
column_name = ls_objs[j]
if adw.describe(column_name + '.type') = 'column' then
ls_value = adw.describe("evaluate('lookupdisplay("+column_name+")',"+string(i - 1)+")")
end if
if adw.describe(column_name + '.type') = 'compute' then
ls_value = adw.describe("evaluate('" + adw.describe(column_name + '.expression') + "',"+string(i - 1)+")")
end if
ole_object.cells(i,j).value = ls_value
next
next
setpointer(oldpointer)
ole_object.disconnectobject()
destroy ole_object
return 1
end function
三、pbtoword函數f_outputtoword_new.srf
$pbexportheader$f_outputtoword_new.srf
global type f_outputtoword_new from function_object
end type
forward prototypes
global function integer f_outputtoword_new (datawindow adw)
end prototypes
global function integer f_outputtoword_new (datawindow adw);
//函數名:f_outputtoword_new
//輸入: adw - datawindow,指定的數據窗口
//返回值: integer
constant integer pplayoutblank = 12
oleobject ole_object
ole_object = create oleobject
integer li_ret
li_ret = ole_object.connecttoobject("","word.application")
if li_ret <> 0 then
//如果word還沒有打開,則新建。
li_ret = ole_object.connecttonewobject("word.application")
if li_ret <> 0 then
messagebox('ole錯誤','ole無法連接!錯誤號:' + string(li_ret))
return 0
end if
ole_object.visible = true
end if
long ll_colnum,ll_rownum
constant long wdword9tablebehavior = 1
constant long wdautofitfixed = 0
constant long wdcell = 12
string ls_value
pointer oldpointer
oldpointer = setpointer(hourglass!)
string ls_objects,ls_obj,ls_objs[],ls_objtag[]
long ll_pos,ll_len,ll_num = 0
ls_objects = trim(adw.describe('datawindow.objects'))
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 + '.tag')
end if
ls_objects = right(ls_objects,len(ls_objects) - ll_pos)
loop
//得到數據窗口數據的列數與行數(行數應該是數據行數 + 1)
ll_colnum = ll_num
ll_rownum = adw.rowcount() + 1
ole_object.documents.add()
ole_object.activedocument.tables.add(ole_object.selection.range, ll_rownum, ll_colnum)
string ls_colname
integer i,j,k
for i = 1 to ll_colnum
//得到標題頭的名字
ls_value = ls_objtag[i]
ole_object.selection.typetext(ls_value)
for k = 1 to f_cncharnum(ls_value)
ole_object.selection.typebackspace()
next
ole_object.selection.moveright(wdcell)
next
adw.setredraw(false)
ole_object.selection.moveleft(wdcell)
string column_name
for i = 2 to ll_rownum
for j = 1 to ll_colnum
column_name = ls_objs[j]
if adw.describe(column_name + '.type') = 'column' then
ls_value = adw.describe("evaluate('lookupdisplay("+column_name+")',"+string(i - 1)+")")
end if
if adw.describe(column_name + '.type') = 'compute' then
ls_value = adw.describe("evaluate('" + adw.describe(column_name + '.expression') + "',"+string(i - 1)+")")
end if
ole_object.selection.moveright(wdcell)
ole_object.selection.typetext(ls_value)
for k = 1 to f_cncharnum(ls_value)
ole_object.selection.typebackspace()
next
next
next
adw.setredraw(true)
constant long wdformatdocument = 0
setpointer(oldpointer)
//保存新建的文檔
if messagebox("保存","文檔已經成功完成,是否保存?",question!,yesno!) = 1 then
string docname, named
integer value
value = getfilesavename("選擇文件",docname, named, "doc","doc files (*.doc), *.doc")
if value = 1 then
ole_object.activedocument.saveas(docname, 0,false,"",true,"",false,false,false, false,false)
end if
end if
//斷開ole連接
ole_object.disconnectobject()
destroy ole_object
return 1
end function