与word导入相关问题
我用的是pb8,我想把一个word文档的内容,导入sqlserver2000数据库中的一个表(字段:id ,....,document)中的document字段中,该字段的类型时text.
请指教,我应如何把word文档的内容取出并存入数据库中??
问题点数:100、回复次数:7Top
1 楼balloonman2002()回复于 2002-11-08 12:52:07 得分 60
1、读出WORD内容:使用FILEREAD()函数,操作如:
integer li_FileNum, loops, i
long flen, bytes_read, new_pos
blob b, tot_b
// Set a wait cursor
SetPointer(HourGlass!)
// Get the file length, and open the file
flen = FileLength(sle_filename.Text)
li_FileNum = FileOpen(sle_filename.Text, &
StreamMode!, Read!, LockRead!)
// Determine how many times to call FileRead
IF flen > 32765 THEN
IF Mod(flen, 32765) = 0 THEN
loops = flen/32765
ELSE
loops = (flen/32765) + 1
END IF
ELSE
loops = 1
END IF
// Read the file
new_pos = 1
FOR i = 1 to loops
bytes_read = FileRead(li_FileNum, b)
tot_b = tot_b + b
NEXT
FileClose(li_FileNum)
2、保存到数据库:
insert...
注意此处要INSERT进去一个“ ”值,然后再用:
updateblob...
更新,而不能直接INSERT进去;
同时注意要先将AUTOCOMMIT设为TRUE,之后改为FALSE;Top
2 楼archerhe(AC米兰)回复于 2002-11-08 13:03:59 得分 20
如果你了解word中宏的概念的话,这个问题就比较明朗了。
这是一个从固定格式的表格中读取数据的一部分,关键的是获取对应cell[]中的数据,目前还没有做出动态读取数据的函数,不过利用这个例子,你可能有点启发。
//(2)打开文件
oo=create oleobject
oo连接word
oo.Documents.Open(*.doc)
oo.ActiveDocument.tables[1].cell[row,col].select//选中row行、col列的数据
oo.selection.copy//copy
mle_copy.text=''
mle_copy.paste()//paste
//去掉最后的回车符(这样的回车符往往是由WORD自动生成的)
do while right(mle_copy.text,2)='~r~n'
mle_copy.text=mid(mle_copy.text,1,len(mle_copy.text) -2)
这样mle_copy.text上的值就是word上的数据了。
存入数据库。Top
3 楼dongquestion(书山有路勤为径)回复于 2002-11-08 13:09:54 得分 20
我用的是image类型的字段,希望对你有帮助:
integer li_FileNum, ret, loops, i
double ls_len, new_pos
blob b,tot_b
string txtname, named
if ddlb_2.text="" or isnull(ddlb_2.text) then
messagebox("警告","请选择文档类型!")
return
end if
if ddlb_1.text="" or isnull(ddlb_1.text) then
messagebox("警告","请选择合同编号!")
return
end if
string defext,ftype
string Filter
double bytes_read,flen
if ddlb_2.text="Word格式" then
defext = "Doc"
Filter = "Word Files (*.Doc), *.Doc"
ftype="Word格式"
elseif ddlb_2.text="Excel格式" then
defext = "xls"
Filter = "Excel Files (*.xls), *.xls"
ftype="Excel格式"
end if
ret = GetFileOpenName("Open Word", txtname, named, defext, filter)
INSERT INTO wendang_table
( contact_id,
file_names,
file_type,
image_blob)
VALUES ( :ddlb_1.text,
:named,
:ftype,
null);
IF ret = 1 THEN
flen = FileLength(txtname)
li_FileNum = FileOpen(txtname,StreamMode!)
IF flen > 32765 THEN
IF Mod(flen, 32765) = 0 THEN
loops = flen/32765
ELSE
loops = (flen/32765) + 1
END IF
ELSE
loops = 1
END IF
new_pos = 1
// FOR i = 1 to loops
// bytes_read = FileRead(li_FileNum, b)
// tot_b = tot_b + b
// NEXT
int pos
pos=FileRead(li_FileNum, b)
do while pos>0
tot_b=tot_b+b
pos=FileRead(li_FileNum, b)
loop
ls_len =len(tot_b)
// messagebox("",string(ls_len)+"dddd")
FileClose(li_FileNum)
Sqlca.autocommit=true
// INSERT INTO wendang_table
// ( contact_id,
// file_names,
// file_type,
// image_blob)
// VALUES ( :ddlb_1.text,
// :named,
// :ftype,
// null);
updateblob wendang_table
set image_blob = :tot_b
where contact_id=:ddlb_1.text and file_names=:named
USING sqlca ;
IF sqlca.SQLNRows > 0 THEN
COMMIT USING sqlca ;
messagebox("提示","连接成功!")
Sqlca.autocommit=false
close(parent)
open(w_wendang)
END IF
end if
注意,大于32K的字段要分几次重复读取才行。
Top
4 楼heliu121(牛牛)回复于 2002-11-12 16:56:54 得分 0
谢谢大家的帮助,我还想问一个问题,就是我数据库中是text类型,在sqlserver2000中没有blob类型,我对text 与 blob类型的转化不熟,需要转换成字符型做中间过渡吗?问题已解决我就结帖。
在此谢谢以上各位!!!Top
5 楼xiaozg118(肖)回复于 2002-11-12 17:11:21 得分 0
在SQL SERVER中用TEXT即可
不需要别的转换Top
6 楼balloonman2002()回复于 2002-11-12 17:41:05 得分 0
用TEXT和BLOB,均无需改代码,上述均可;Top
7 楼LynnWang(老王)回复于 2002-11-12 18:22:45 得分 0
参考
http://expert.csdn.net/Expert/topic/1139/1139374.xml?temp=.2313501Top




