如何把现有的txt文件导入PB开发环境的表中?
如题。 问题点数:20、回复次数:6Top
1 楼killerdanny(堕落的瓦拉斯塔兹)回复于 2003-06-02 13:57:06 得分 0
主要看你的TXT文本的格式了!
现成的例子:
global type f_import_file from function_object
end type
forward prototypes
global function long f_import_file (datawindow adw_name, string as_filename, character ac_separator)
end prototypes
global function long f_import_file (datawindow adw_name, string as_filename, character ac_separator);//函数功能:将非TAB分割符文本文件倒入数据窗口
//参数:adw_name :数据窗口
// as_filename :倒入文件名
// ac_separator:分割符,如“|”
long ll_start_pos = 1
string ls_mystring
integer li_FileNum
char lc_tab
string ls_tempfile
int li_rtn
li_FileNum = FileOpen(as_filename,StreamMode!, Read!)
If isnull(li_FileNum) or li_FileNum < 0 then return -2
//将文件内容读入字符串
if FileRead(li_FileNum, ls_mystring) < 0 then return -2
//关闭文件
FileClose(li_FileNum)
lc_tab = char(9) //TAB分割符
//找第一个分割符
ll_start_pos = Pos(ls_mystring, ac_separator, ll_start_pos)
If ll_start_pos <= 0 then
Messagebox('提示信息','文件内没有“' + ac_separator+ '”分割符 ')
return -10
End if
//循环替换分割符
DO WHILE ll_start_pos > 0
//替换分割符
ls_mystring = Replace(ls_mystring, ll_start_pos, 1, lc_tab)
//找下一个分割符
ll_start_pos = Pos(ls_mystring, ac_separator, ll_start_pos + 1)
LOOP
//在当前目录下生成临时文件
ls_tempfile = GetCurrentDirectory() + "\importfile_tmp.txt"
li_FileNum = FileOpen(ls_tempfile, &
StreamMode!, Write!, LockWrite!, Replace! )
FileWrite(li_FileNum, ls_mystring)
FileClose(li_FileNum)
//将临时文件内容倒入数据窗口
li_rtn = adw_name.ImportFile(ls_tempfile)
//删除临时文件
FileDelete ( ls_tempfile )
return li_rtn
//注:
//如果要使函数f_import_file具有更强的通用性,还需进行一定的改造。
//如要判断文件长度,如大于32KB需要用FileRead函数循环读取,
//还可以将倒入起始行、截止行、起始列、截止列作为参数写入函数。
end function
Top
2 楼devil_heart(心魔)回复于 2003-06-02 14:20:54 得分 0
学习先Top
3 楼ccy()回复于 2003-06-02 16:07:47 得分 0
有图形界面工具实现这个功能吗?Top
4 楼csdnpaul(天天想你)回复于 2003-06-02 16:42:15 得分 0
dw_1.reset()
dw_1.settransobject(sqlca)
string filename
string str
filename=trim(st_4.text)
int fp,fr,ll_i
fp = fileopen(filename,linemode!,read!)
fr=fileread(fp,str)
do while fr<> -100
if fr=-1 then
return
end if
ll_i=dw_1.insertrow(0)
dw_1.setitem(ll_i,"kxpx_xh",str)
fr=fileread(fp,str)
loop
fileclose(fp)Top
5 楼JiangSf(菜鸟,多多关照)回复于 2003-06-02 16:53:24 得分 20
从菜单中选择 rows --> import
ok
Top
6 楼bastenlee(Lee)回复于 2003-06-02 17:01:29 得分 0
global type f_import_file from function_object
end type
forward prototypes
global function long f_import_file (datawindow adw_name, string as_filename, character ac_separator)
end prototypes
global function long f_import_file (datawindow adw_name, string as_filename, character ac_separator);//函数功能:将非TAB分割符文本文件倒入数据窗口
//参数:adw_name :数据窗口
// as_filename :倒入文件名
// ac_separator:分割符,如“|”
long ll_start_pos = 1
string ls_mystring
integer li_FileNum
char lc_tab
string ls_tempfile
int li_rtn
li_FileNum = FileOpen(as_filename,StreamMode!, Read!)
If isnull(li_FileNum) or li_FileNum < 0 then return -2
//将文件内容读入字符串
if FileRead(li_FileNum, ls_mystring) < 0 then return -2
//关闭文件
FileClose(li_FileNum)
lc_tab = char(9) //TAB分割符
//找第一个分割符
ll_start_pos = Pos(ls_mystring, ac_separator, ll_start_pos)
If ll_start_pos <= 0 then
Messagebox('提示信息','文件内没有“' + ac_separator+ '”分割符 ')
return -10
End if
//循环替换分割符
DO WHILE ll_start_pos > 0
//替换分割符
ls_mystring = Replace(ls_mystring, ll_start_pos, 1, lc_tab)
//找下一个分割符
ll_start_pos = Pos(ls_mystring, ac_separator, ll_start_pos + 1)
LOOP
//在当前目录下生成临时文件
ls_tempfile = GetCurrentDirectory() + "\importfile_tmp.txt"
li_FileNum = FileOpen(ls_tempfile, &
StreamMode!, Write!, LockWrite!, Replace! )
FileWrite(li_FileNum, ls_mystring)
FileClose(li_FileNum)
//将临时文件内容倒入数据窗口
li_rtn = adw_name.ImportFile(ls_tempfile)
//删除临时文件
FileDelete ( ls_tempfile )
return li_rtn
//注:
//如果要使函数f_import_file具有更强的通用性,还需进行一定的改造。
//如要判断文件长度,如大于32KB需要用FileRead函数循环读取,
//还可以将倒入起始行、截止行、起始列、截止列作为参数写入函数。
end function
Top




