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