当前位置: 代码迷 >> PB >> 怎么在PowerBuilder 9.0中把当前数据窗口数据保存为excel文档
  详细解决方案

怎么在PowerBuilder 9.0中把当前数据窗口数据保存为excel文档

热度:83   发布时间:2016-04-29 08:45:44.0
如何在PowerBuilder 9.0中把当前数据窗口数据保存为excel文档
我想在主菜单里设置一个按钮,主要实现把当前数据窗口(是指当前处于激活状态的数据窗口,并不是特定的某个窗口)的数据另存为excel表格的目的,要求要有保存路径和输入保存文件名的对话框(如果可能的话,这种效果尽量和保存office文档一样的对话框)。
我是新手,希望能有相关代码。。在此先谢谢了。。

------解决方案--------------------
/**********************************************************/
/函数名:uf_dwsaveas_excel,函数类型integer
/参数:datawin,参数类型datawindow
/**********************************************************/
integer li_rtn,ii,li_asc
string ls_name,ls_pathname
boolean lb_exist
if datawin.RowCount()<1 then
MessageBox("信息提示","请先查询再导出Excel!")
return -1//error
end if
li_rtn=GetFileSaveName("保存文件",ls_pathname,ls_name,"xls","Excel文件(*.xls),*.xls")

if li_rtn=1 then
lb_exist = FileExists(ls_pathname)
IF lb_exist THEN 
li_rtn = MessageBox("保存件", ls_pathname+"已经存在,是否覆盖",Exclamation!, YesNo!)
end if
if li_rtn=1 then
li_rtn=datawin.SaveAsAscii(ls_pathname)
if li_rtn=1 then
//MessageBox("提示信息","已经保存成功!")
else
MessageBox("错误信息","文件导出失败!")
return -1//error
end if
else
return -1//error
end if
else
return -1
end if

/**********将导出的excel标题转换*********/
long numcols , numrows , c, r
OLEObject xlapp , xlsub
int ret
numcols = long(datawin.Object.DataWindow.Column.Count)
numrows = datawin.RowCount()


xlApp = Create OLEObject


ret = xlApp.ConnectToNewObject( "Excel.Sheet" )
if ret < 0 then
MessageBox("连接失败","请确认是否安装了excel!~r~n"&
+"错误代码:"+string(ret))
return -1
end if

xlApp.Application.Workbooks.Open(ls_pathname) 

xlsub = xlapp.Application.ActiveWorkbook.Worksheets[1]
string ls_colname,ls_text,ls_modistr,ls_col

FOR c=1 to numcols
ls_col="#"+string(c)+".name"
ls_colname=datawin.describe(ls_col)
ls_modistr=ls_colname+"_t.text"
ls_text=datawin.describe(ls_modistr)
xlsub.cells[1,c]=ls_text
NEXT

xlApp.DisConnectObject()
Destroy xlapp
MessageBox("信息提示","导出成功!")
return 1//success
  相关解决方案