当前位置:数据库 > Excel >>

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

我想在主菜单里设置一个按钮,主要实现把当前数据窗口(是指当前处于激活状态的数据窗口,并不是特定的某个窗口)的数据另存为excel表格的目的,要求要有保存路径和输入保存文件名的对话框(如果可能的话,这种效果尽量和保存office文档一样的对话框)。 我是新手,希望能有相关代码。。在此先谢谢了。。
补充:楼下的,请把相关信息说明一下吧。我怎么有点看不懂呀???(我见你的答案在我的第二个问题里也拷贝了一份,那明显是错误的!!)好多语句我都没见过。。。。说清楚一点,我好给你加分呀。。
答案:我是这样写的,不知道对你有没有帮助,首先写一个外部通用函数(带两个参数,第一个参数为导出excel的标题,第二个为要导出的数据窗口,传递方式都是value):f_export_excel(string rptheadtxt,datawindow ad_dw)

Int   li_value
String  ls_path,ls_fname
String      ls_tempfilename 

li_value = GetFileSaveName("请选择导出文件", &
 + ls_path, ls_fname, "XLS", &
 + "Excel文件 (*.xls), *.xls," &
 + "Word 文件 (*.doc), *.doc,")
IF li_value <> 1 THEN RETURN 0

SetPointer(hourglass!)

// 删除原文件
IF FileExists(ls_path) THEN
 IF MessageBox('提示信息', '原文件已经存在, 是否覆盖 ?', Question!, YesNo!) = 2 THEN RETURN 0
 IF NOT FileDelete(ls_path) THEN
  MessageBox('提示信息', '删除原文件失败, 该文件可能正在被使用 !')
  RETURN 0
 END IF
END IF

ls_tempfilename = "temp_excel" + String( Today( ), 'yyyymmddhhmmssfff' )

IF ad_dw.SaveAsAscii ( ls_tempfilename,  "~t",  "")  =  -1  THEN
 MessageBox("提示信息", "导出数据出错. 不能写入文件 !", Exclamation!)
 RETURN 0
END IF

// 去掉行间隔
Integer li_FileNum, li_FileNum_temp, li_len = Len( "<tr>~r~n<tr>" )
String  ls_FileRead
Long    ll_pos = 1

li_FileNum      = FileOpen( ls_path, StreamMode!, Write!, LockWrite!, Append!)
li_FileNum_temp = FileOpen( ls_tempfilename, StreamMode! )

DO WHILE FileRead( li_FileNum_temp, ls_FileRead ) > li_len
 ll_pos = Pos( ls_FileRead, "<tr>~r~n<tr>", 1 )
 DO WHILE ll_pos > 0
  ls_FileRead = Replace( ls_FileRead, ll_pos, li_len, "<tr>" )
  ll_pos = Pos( ls_FileRead, "<tr>~r~n<tr>", ll_pos )
 LOOP
 FileWrite( li_FileNum, ls_FileRead )
LOOP
FileClose( li_FileNum )
FileClose( li_FileNum_temp )
FileDelete( ls_tempfilename )

MessageBox('提示信息','数据导出成功 !', Exclamation!)
RETURN 1



调用规则:f_export_excel(parent.title,dw_1)
其他:global type uf_dwsaveas_excel from function_object
end type

forward prototypes
global function integer uf_dwsaveas_excel (datawindow datawin)
end prototypes

global function integer uf_dwsaveas_excel (datawindow datawin);integer li_rtn,ii,li_asc
string ls_name,ls_pathname
boolean lb_exist
if datawin.RowCount()<1 then
MessageBox("系统提示","请先检索数据再导出至Excel!")
return -1
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+"已经存在,是否覆盖?",question!,YesNo!)
end if
if li_rtn=1 then
li_rtn=datawin.saveas(ls_pathname,excel8!,true)
if li_rtn=1 then
else
MessageBox("系统提示","导出数据失败!")
return -1
end if
else
return -1
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()
// 产生oleobject的实例
xlApp = Create OLEObject
//连接ole对象
ret = xlApp.ConnectToNewObject("Excel.Sheet" )
if ret < 0 then
MessageBox("系统提示","连接到EXCEL失败,请确认您的系统是否已经安装EXCEL!~r~n"&
+"错误代码:"+string(ret))
    return -1
end if
// 打开EXCEL文件
xlApp.Application.Workbooks.Open(ls_pathname) 
////使文件可见
//xlApp.Application.Visible = true

// 得到活动工作表的引用,改善程序性能
xlsub = xlapp.Application.ActiveWorkbook.Worksheets[1]
string ls_colname,ls_text,ls_modistr,ls_col
//取字段名更改为对应的文本text值
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.Application.ActiveWorkBook.Save()
//xlapp.Application.Displayalerts=true
xlApp.Application.Quit()  
xlApp.DisConnectObject()
Destroy xlapp


MessageBox("提示信息","导出数据成功!")
return 1 

end function 

上一个:在EXCEL表中如何统计与当前月相同月份的数
下一个:如何将excel中的数据导入到sql server中

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,