dsoFrameocx的使用Word格式文档下载.docx
《dsoFrameocx的使用Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《dsoFrameocx的使用Word格式文档下载.docx(11页珍藏版)》请在冰豆网上搜索。
C:
/33.doc"
false,"
Word.Document"
);
-打开远程的word文件
http:
//localhost:
8080/XxShhCtrl-getAttachmentById.pfv?
fjxh=xxfbfj000562"
true,"
"
MyUserAccount"
MyPassword"
-打开本地的excel文件
/11.xls"
Excel.Sheet"
3保存文件到本地
document.all.DSOFramer1.Save("
c:
/22.doc"
注意:
这里的文件指的是把控件中展示的doc文件,以22.doc的名称保存到c盘。
4保存文件的服务器
functionfileUpload(){
//document.all.DSOFramer1.style.display="
none"
;
alert('
------'
//初始化Http引擎
document.all.DSOFramer1.HttpInit();
//增加Post变量
//document.all.DSOFramer1.HttpAddPostString("
RecordID"
20060102200"
document.all.DSOFramer1.HttpAddPostString("
userid"
mrli"
//上传打开的文件
document.all.DSOFramer1.HttpAddPostCurrFile("
file1"
"
11.doc"
//执行上传动作
document.all.DSOFramer1.HttpPost("
//192.168.0.55:
8082/WordCtrl-test1.pfv"
}
说明:
其中userid相当于表单的name,可以通过这个名字,接收到后面的mrli值。
而11.doc表示被上传的文件名字。
5删除书签
functioncleanMark(){//删除书签
//document.all.DSOFramer1.SetFieldValue("
book1"
:
ADDMARK:
document.all.DSOFramer1.SetFieldValue("
DELMARK:
6循环打印(适用于人名章问题)
functionprintStamp(){
insToTif();
//插入传真件
for(i=0;
i<
2;
i++){
cleanMark();
//书签内容清除
if(i==0){
张三"
}else{
李四"
document.all.DSOFramer1.printout();
7插入传真件
functioninsToTif(){//传真件问题
/44_2.doc"
/cebTest.TIF"
JPG:
//书签中插入图片
//document.all.DSOFramer1.InsertFile("
//在当前光标处插入图片
document.all.DSOFramer1.InsertFile("
8);
8书签中插入内容
向名称为book1的书签中插入数据
test"
//向书签book1中插入文字
functioninsToMark(){
//新建书签book1,并向其中插入文字
FILE:
//插入远程图片
//书签中插入本地图片
9书签中内容的换行
书签中加入\r即可
vardd="
testsss\r\r"
dd,"
//书签中插入文字
10;
如上语句的执行效果如图:
10.获取书签中的内容
functiongetBookMark(){
vardocObj=document.all.DSOFramer1.ActiveDocument;
//获取word对象
varvalue_bookmark=docObj.bookmarks("
).Range;
//获取书签“book1”中的内容
alert(value_bookmark);
11.隐藏工具栏
//隐藏菜单
functionhidBtn(){
vartt=document.all.DSOFramer1.ActiveDocument;
tt.CommandBars
(1).Visible=false;
tt.CommandBars
(2).Visible=false;
tt.CommandBars(3).Visible=false;
tt.CommandBars(4).Visible=false;
tt.CommandBars(5).Visible=false;
tt.CommandBars(6).Visible=false;
tt.CommandBars(7).Visible=false;
如果让某一工具栏的某些按钮隐藏,可以用下面的方法
tt.CommandBars
(1).Controls
(1).Visible=false;
//常用工具栏的第一个图标隐藏
tt.CommandBars
(1).Controls(3).Visible=false;
//常用工具栏的第二个图标隐藏
//常用工具栏的第三个图标隐藏
12.隐藏标题栏,菜单栏,工具栏
document.all.DSOFramer1.Toolbars=false;
document.all.DSOFramer1.Menubar=false;
document.all.DSOFramer1.Titlebar=false;
13视图切换
设置文档显示模式HRESULTShowView(longdwViewType,[out,retval]long*pbool);
dwViewType的可取值为:
enumWdViewType
{
wdNormalView=1,
wdOutlineView=2,
wdPrintView=3,
wdPrintPreview=4,
wdMasterView=5,//这个是大纲
wdWebView=6
};
*/
//如大纲模式
document.all.DSOFramer1.ShowView(5);
============================
Java之Word操作-Jacob
2007-07-0311:
01
jacob
Jacob也是基于用java调用dll的一个框架,不过是个开源的,在大名鼎鼎的Sourceforge有一席之地。
下载地址为,可以从上面下载最新的依赖包和dll文件。
我下载的是目前的最新版jacob_1.13-M2,据传,老版本对office2003支持不是很好:
-)。
下载解压后如图:
在AMD64和X86文件加中是对应不同系统的dll,对应windows,就把x86下面的dll拷贝到c:
/Windows/system32下面即可。
--------------------------------------
把doc文件转换为html文件的小例子(参看网友文章而来)
主要代码和注释如下:
ActiveXComponentapp=newActiveXComponent("
Word.Application"
//启动word
StringdocPath="
e:
/temp.doc"
//word文件的位置
StringhtmlPath="
/ssss.htm"
//目标文件的位置,就是把temp.doc转换为ssss.htm文件
app.setProperty("
Visible"
newVariant(false));
//这里的false表示生成过程不显示word的打开过程,而如果是true,则在生成过程中会显示word打开
Objectdocs=app.getProperty("
Documents"
).toDispatch();
Objectdoc=Dispatch.invoke((Dispatch)docs,"
Open"
Dispatch.Method,newObject[]{docPath,newVariant(false),newVariant(true)},newint[1]).toDispatch();
//打开word文件
Dispatch.invoke((Dispatch)doc,"
SaveAs"
Dispatch.Method,newObject[]{htmlPath,newVariant(8)},newint[1]);
//生成html文件
Variantf=newVariant(false);
Dispatch.call((Dispatch)doc,"
close"
f);
app.invoke("
Quit"
newVariant[]{});
//退出,一般放在finally中
这个我已经测试过了,挺好使的。
呵呵。
使用JACO取得书签处的值和向书签处插入变量值
模板文件如图:
下面是我测试的代码。
publicvoidopenWord(){
StringinFile="
StringtoFile="
/ssss.doc"
Dispatchdocs=app.getProperty("
Dispatchdoc=Dispatch.invoke(docs,"
Dispatch.Method,newObject[]{inFile,newVariant(false),newVariant(true)},newint[1]).toDispatch();
//打开word文件
DispatchactiveDocument=app.getProperty("
ActiveDocument"
DispatchbookMarks=app.call(activeDocument,"
Bookmarks"
//Bookmarks是一个关键字
StringbookMarkKey="
miji"
//书签名称
DispatchrangeItem=Dispatch.call(bookMarks,"
Item"
bookMarkKey).toDispatch();
//Item是关键字
Dispatchrange=Dispatch.call(rangeItem,"
Range"
//Range是关键字
StringbookMarkValue=Dispatch.get(range,"
Text"
).toString();
//获取miji书签处的值
log.debug("
>
"
+bookMarkValue);
//输入“miji”书签的值
StringnewMarkValue="
新标签值"
StringbookMarkKey_new="
fabuchu"
DispatchrangeItem_new=Dispatch.call(bookMarks,"
bookMarkKey_new).toDispatch();
Dispatchrange_new=Dispatch.call(rangeItem_new,"
Dispatch.put(range_new,"
newVariant(newMarkValue));
Dispatch.invoke(doc,"
Dispatch.Method,newObject[]{toFile,newVariant(0)},newint[1]);
Dispatch.call(doc,"
Close"
f);
输出的内容如图示,通个图可以看到取得的miji名称书签的值。
再看看生成的ssss.doc文件,看看是否把字符串StringnewMarkValue="
插入到了指定的位置,如图
最让人欣慰搜的是对中文支持很好。
没有中文乱码现象。
jacob文本内容的替换
下面先看看模板文件的截图
下面是详细的代码:
//模板word文件的位置
//输出的文件
Dispatch.Method,newObject[]{inFile,newVariant(false),newVariant(true)},newint[1]).toDispatch();
Dispatchselection=app.getProperty("
Selection"
//得到一个组件
log.info("
Selection"
+selection);
Dispatchfind=Dispatch.call(selection,"
Find"
//查找什么文本
StringoldText="
2007"
//需要替换的文本内容
StringnewText="
2008-欢迎北京奥运"
//新的内容
Dispatch.call(find,"
ClearFormatting"
Dispatch.put(find,"
oldText);
Execute"
//这一处也是一个很值得注意的地方,如果把这一句去掉或移至Dispatch.put(selection,"
newText);
的下面,就不会执行替换功能,而是把oldText的内容插入到文档的开头部分。
所以这句话应该是定位oldText位置的作用
Dispatch.put(selection,"
//文件保存
Dispatch.Method,newObject[]{toFile,newVariant(0)},newint[1]);
Dispatch.call(doc,"
newVariant[]{});
//一般写在finally结构体中
执行后的结果截图:
从截图可以看出文本“2007”被“2008-欢迎北京奥运会”替换掉了。