我有一个文本格式文件格式如下:估计有十万行数据,想用TAB替换^字符,然后用PB的Importfile函数导入到数据窗口提交,但是在替换用太久的时间了,有什么好方法解决及如何提交数据吗?
谢洪明^男^19931229^0000327599^000032759901^6013822600000728956
林进军^男^19920518^0000327622^000032762201^6013822600000728956
廖海彬^女^19940115^0000327654^000032765401^6013822600000728956
覃秋游^女^19940124^0000327658^000032765801^6013822600000728956
李航^男^19940514^0000327667^000032766701^6013822600000728956
------解决方案--------------------
使用ultraedit的替换
Ctrl+R
查找 ^
替换为 ^t
------解决方案--------------------
------解决方案--------------------
ls_tab=chara(9)
if rte_1.insertdocument(txtpath, true) =1 then rtn= rte_1.Find(",", TRUE, TRUE, FALSE, TRUE)
do while rtn>0
rte_1.ReplaceText(ls_tab)
rtn= rte_1.Find(",", TRUE, TRUE, FALSE, TRUE)
loop
if rte_1.SaveDocument('c:\ecourt\temp\banktemp.txt', FileTypeText!) =1 then dw_1.ImportFile('c:\ecourt\temp\banktemp.txt')
if fileexists('c:\ecourt\temp\banktemp.txt') then filedelete('c:\ecourt\temp\banktemp.txt')
------解决方案--------------------
#10楼,看了你的代码,知道你为什么慢了。
//将文件内容读入字符串
for i = 1 to ll_loops
if FileRead(li_FileNum, ls_TempFile) < 0 then
as_ErrorMsg = "读取"+as_FileName+"文件出错。"
return -1
end if
ls_MyString = ls_MyString + ls_TempFile
next
这里不要这样写,这样子写会很慢的,你应该读取一次就替换字符,然后把ls_tempfile转换成blob,然后用BlobEdit()连接起来。最后直接用string()转换,用importstring()导入。
大家有情趣可以测试一下,用blobedit()连接 + importstring()导入,要比string 用 + 号连接再导入 快十倍左右,如果文件越大就越明显
------解决方案--------------------
- C/C++ code
//====================================================================// 函数: w_import::wf_importfile()//--------------------------------// 描述: TXT文件导入//--------------------------------// 参数: // datawindow adw_name 目标窗口 // character ac_separator 文件分隔符 //--------------------------------// 返回值: integer//--------------------------------// 作者: Ken.Wong 日期: 2010-01-20//--------------------------------// 修改历史: // //--------------------------------// 版权所有 (c) 2004-2010 Sky.Qi(TM) 保留所有版权。//====================================================================Int li_rtnString ls_file_directory,ls_file_name //选择文件的目录及文件名称long ll_filelengthinteger li_FileNum,li_loops,li_istring ls_filestring ls_file_allchar lc_tab=char(9)long ll_start_pos = 1string ls_tempfileli_rtn = GetFileOpenName("选择数据文件", ls_file_directory ,ls_file_name,& "DOC", "Text Files (*.TXT),*.TXT,"& + "PDF文件 (*.PDF),*.PDF,"& + "Doc Files (*.DOC),*.DOC," & + "All Files (*.*), *.*")If li_rtn <> 1 Then Return -1ll_filelength = filelength(ls_file_directory)if ll_filelength <=0 then return -1li_FileNum = FileOpen(ls_file_directory,StreamMode!, Read!)// 统计循环次数IF ll_filelength > 32765 THEN IF Mod(ll_filelength, 32765) = 0 THEN li_loops = ll_filelength/32765 ELSE li_loops = (ll_filelength/32765) + 1 END IFELSE li_loops = 1END IF// 读文件FOR li_i = 1 to li_loops FileRead(li_FileNum, ls_file) ls_file_all = ls_file_all + ls_fileNEXT//关闭文件FileClose(li_FileNum)ll_start_pos = Pos(ls_file_all, ac_separator, ll_start_pos)if ll_start_pos <= 0 then li_rtn=adw_name.ImportFile(ls_file_directory)else //循环替换分割符 DO WHILE ll_start_pos > 0 //替换分割符 ls_file_all = Replace(ls_file_all, ll_start_pos, 1, lc_tab) //找下一个分割符 ll_start_pos = Pos(ls_file_all, ac_separator, ll_start_pos + 1) LOOP ll_filelength = lenA(ls_file_all) IF ll_filelength > 32765 THEN IF Mod(ll_filelength, 32765) = 0 THEN li_loops = ll_filelength/32765 ELSE li_loops = (ll_filelength/32765) + 1 END IF ELSE li_loops = 1 END IF ls_tempfile = GetCurrentDirectory() + "\importfile_tmp.txt" If FileExists ( ls_tempfile ) Then filedelete(ls_tempfile) End if li_FileNum = FileOpen(ls_tempfile, & StreamMode!, Write!, LockWrite!, Append! ) // 写到临时文件中 FOR li_i = 0 to li_loops - 1 ls_file = Mid(ls_file_all, li_i * 32765 + 1, 32765) FileWrite(li_FileNum, ls_file) NEXT //关闭临时文件 FileClose(li_FileNum) //将临时文件内容倒入数据窗口 li_rtn = adw_name.ImportFile(ls_tempfile) //也可以直接这样// li_rtn = adw_name.ImportString(ls_file_all) //删除临时文件 FileDelete ( ls_tempfile )end ifreturn li_rtn