可以建立一个函数来完成数据导出的功能:
函数如下:
return type:long
argument type:datawindow
argument name:dw
pass by:value
function name :excel_out
代码如下:
string ls_pathname,ls_name
int rte,li_rtn
boolean lb_exist
if dw.RowCount()<1 then
MessageBox("提示信息","请先检索数据再导出至Excel!")
return -1
end if
rte=GetFileSaveName("请选择文件",ls_pathname,ls_name,"xls","Excel文件(*.xls),*.xls")
if rte=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
dw.SaveAsAscii(ls_pathname)
if li_rtn=1 then
MessageBox("提示信息","导出数据成功!")
dw.reset()
else
MessageBox("错误信息","导出数据失败!")
return -1
end if
else
return -1
end if
else
return -1
end if
return 1
数据从文本文件写入数据库表的捷径
许多情况下,文本文件中的数据排列顺序与数据库表中列的顺序
一致,并且其数据类型与数据库表中对应列也一致,要做的就是将该文
件中的内容存入对应数据库表中。例如在点对点通信的情况下,一方
把数据库表中的数据以文本文件格式存储并传送给另一方,另一方要
做的工作就是将收到的数据存入相应的数据库表中。PowerBuilder有
多种方式与文本文件打交道,比如使用ODBC的文本文件驱动器、采用D
DE(动态数据交换)方式、采用OLE方式或使用Cu rsor将文本文件逐行
逐列读取并写入数据库表等。但还有一种更快捷的方法:使用ImportF
ile()函数将文本文件内容直接倒入数据库表中。该函数用法为:
datawindowname.ImportFile(filename{,startrow{,endrow &
{,startcolumn{,endcolumn{,dwstartcolumn}}}}})
ImportFile()函数要求数据窗列的数据类型和列的排列顺序必须
与文本文件中的数据相匹配。
在存储文本文件时要注意接收数据库对表的格式要求,比如在Syb
ase中,表格的列与列间是采用Tab键区分的,所以如果要用ImportFile
()函数将文件内容写入Sybase表中,就要将相应文本文件的列与列间
加入Tab键——这在文件形成时即可加入。
下面的代码将文件infor.txt写入数据窗dw-infor,并将与dw-inf
or对应的数据库表更新:
int impt
if (FileLength("c:\data\infor.txt") > 0) then
impt = dw-infor.ImportFile("c:\data\infor.txt")
if impt > 0 then
dw-infor.settrans(sqlca);
update(dw-infor);
commit;
if sqlca.sqlcode = -1 then
messagebox("SQL ERROR",sqlca.sqlerrtext)
end if
else
messagebox("注意","文件infor.txt写入失败!")
end if
end if
此外,ImportFile()函数还支持从文本文件的指定开始行列到结
束行列读取并写入数据;并且支持.dbf(dBase)文件的读写,这给原有
台式数据库数据文件向新的分布式数据库表的转换带来了极大的方便
。