‘壹’ 如何实现在线编辑word
有很多在线编辑Word的方法。
1)利用控件,比如Page
Office,确切说是网页里面编辑Word,控件负责下载文件、调用Office,然后上传文件并删除缓存。
2)Office
365,微软产品当然可以。无非是网页比起应用程序功能弱一些。
3)UZER.ME,原生态Word在线编辑。
‘贰’ 在java中怎么实现在线编辑Word
需求是在Java中处理Word文档还是要在网页中在线编辑文档?
如果是用Java处理Word文档,我记得有个库是Apache POI,功能还是蛮强大的。
如果要网页端在线编辑Word文档,推荐使用桐享EaaS,有SDK的。客户端无需安装Word,也无需安装插件。对用户来说非常方便,只要一个浏览器就可以了。
‘叁’ 如何在java中操作word
想用java操作word文件?jacob是个不错的选择,也就是java-com桥,你可以在http://sourceforge.net/projects/jacob-project/下载,我下载的版本是1.12,注意版本太低的话可能会报错。
如果没有特殊需求,可以直接使用jacob_*.zip中提供的jacob.jar和jacob.dll。把jacob.dll文件放在系统可以找得到的
路径上,一般放c:/windows/system32下就行了,注意你用的jacob.dll文件和你的jacob.jar包要匹配,否则会报错哦!
如果想自己编译也很简单,把jacob_*_src.zip解开,建个工程,在build.xml中稍作配置即可:
<property name="JDK" value="D:\Java\j2sdk1.4.2_13"/>
<property name="MSDEVDIR" value="D:\Microsoft Visual Studio\VC98"/>
<property name="version" value="1.12"/>
看出来了吗,你的机器上需要有JDK和VC环境,VC是用来生成jacob.dll文件的,如果编译时说找不到MSPDB60.DLL,那就在你的
Microsoft Visual Studio目录下搜索一下,拷贝到D:\Microsoft Visual
Studio\VC98\Bin下就行了。
如果需要对jacob里的jar包改名,(虽然通常不会发生这种情况,但如果你需要两个版本的jacob同时使用,改名可能是一种选择),这时你的工作就多一些:
(1)package改名是必须的了,比如我们把src下的com.jacob.activeX改为com.test.jacob.activeX,把
com.jacob.com改为com.test.jacob.com,打包时只有这两个包是有用的,所以只改它们就够了。
(2)然后修改build.xml中src.java.jacob.mainpackage的value为com.test.jacob,修改java.class.main的value为com.test.jacob.com.Jacob。
(3)别忘了javaJarBin中打包的源码路径也要改,<include name="com/**/*.class" />改为<include name="com/test/**/*.class" />。
(4)build.xml中对生成的dll和jar包也要改个名,比如我们把这两个文件改为jacob_test.dll和
jacob_test.jar。修改build.xml中的enerated.filename.dll和generated.filename.jar
的value为你新改的名字。
(5)com.test.jacob.com.LibraryLoader中,System.loadLibrary("jacob");改成
System.loadLibrary("jacob_test");
(6)另外,很重要的,在jni中*.cpp和*.h中com_jacob_com统一改为com_test_jacob_com,com/jacob
/com统一改为com/test/jacob/com。
(7)ant编译,编译好的文件在release目录下。
(8)最后把编译好的jacob_test.dll文件放在windows/system32下就大功告成了。
现在该用到jacob.jar了,如果你自己修改过jar包的名字,用新改的jar包,如jacob_test.jar,这里统一称为jacob.jar。
首先在classpath中引入jacob.jar包,如果是web应用,WEB-INF的lib中也要加入jacob.jar包。
下面给一个例子:
类ReplaceWord.java
import com.jacob.com.*;
import com.jacob.activeX.*;
public class ReplaceWord {
public static void main(String[] args) {
ActiveXComponent app = new ActiveXComponent("Word.Application"); //启动word
String inFile = "C:\\test.doc"; //要替换的word文件
try {
app.setProperty("Visible", new Variant(false)); //设置word不可见
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new
Object[] { inFile, new Variant(false),new Variant(false) }, new
int[1]).toDispatch();
//打开word文件,注意这里第三个参数要设为false,这个参数表示是否以只读方式打开,因为我们要保存原文件,所以以可写方式打开。
Dispatch
selection=app.getProperty("Selection").toDispatch();//获得对Selection组件
Dispatch.call(selection, "HomeKey", new Variant(6));//移到开头
Dispatch find = Dispatch.call(selection, "Find").toDispatch();//获得Find组件
Dispatch.put(find, "Text", "name"); //查找字符串"name"
Dispatch.call(find, "Execute"); //执行查询
Dispatch.put(selection, "Text", "张三"); //替换为"张三"
Dispatch.call(doc, "Save"); //保存
Dispatch.call(doc, "Close", new Variant(false));
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
app.safeRelease();
}
}
}
也许你会问,我怎么知道要调用哪个方法传哪些参数来进行操作?别忘了,word还有宏呢!自己录制一个宏,编辑这个宏就可以看到代码了!用哪个对象的哪个方法就看你的了。
我总结了一下:
document下的组件都用Dispatch selection=app.getProperty("Selection").toDispatch()这种方法获得;
再往下的组件就需要调用selection的方法来获取,如 Dispatch find = Dispatch.call(selection, "Find").toDispatch();
如果某个方法需要参数,Dispatch doc =
Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { inFile, new
Variant(false),new Variant(false) }, new
int[1]).toDispatch()是一个例子,这是调用docs的Open方法,Object[]数组里就是它需要的参数了;
如果要修改某个组件的属性呢,用Dispatch.put(find, "Text", "name")这种形式,"Text"是属性名,"name"是值。
‘肆’ 关于java中在线编辑word和excel
这个肯定要用ActiveX插件技术了,要么自己开发,要么用第三方的产品,个人感觉pageoffice相当不错。
虽然我回复的文字比较少,但的确是本人分析各个产品之后的结果。
‘伍’ 用java在线编辑word文档
你网上搜PageOffice,有很多它的示例代码的,它在在线编辑word方面做得很好,并且还是支持多个浏览器的啊,你试试。
‘陆’ Java在线预览word文件问题
spire.cloud在线编辑器可以在线预览或编辑word文档
‘柒’ java怎么实现在线打开word时给word加水印
packagecom.ymo.word;
importcom.jacob.activeX.ActiveXComponent;
importcom.jacob.com.ComThread;
importcom.jacob.com.Dispatch;
importcom.jacob.com.Variant;
publicclassTestJacobWord{
privateActiveXComponentwrdCom=null;
privateDispatchdoc=null;
privateDispatchactiveDoc=null;
privateDispatchdocSelect=null;
privateDispatchdocs=null;
=null;
privateStringdocName="";
(){
if(instance==null){
instance=newTestJacobWord();
}
returninstance;
}
privatebooleaninitWord(){
booleanflag=false;
ComThread.InitSTA();
wrdCom=newActiveXComponent("word.Application");
try{
docs=wrdCom.getProperty("Documents").toDispatch();
wrdCom.setProperty("Visible",newVariant(false));
flag=true;
}catch(Exceptione){
flag=false;
e.printStackTrace();
}
returnflag;
}
privatevoidcreateNewDocument(){
doc=Dispatch.call(docs,"Add").toDispatch();
docSelect=Dispatch.get(wrdCom,"Selection").toDispatch();
}
privatevoidgetActiveDoc(){
activeDoc=wrdCom.getProperty("ActiveWindow").toDispatch();
System.out.println(activeDoc.getProgramId());
}
privatevoidopenDocument(StringdocPath){
if(this.doc!=null){
closeDocument();
}
this.doc=Dispatch.call(docs,"Open",docPath,newVariant(false),
newVariant(false)).toDispatch();
docSelect=Dispatch.get(wrdCom,"Selection").toDispatch();
}
privatevoidcloseDocument(){
if(doc!=null){
Dispatch.call(doc,"Save");
Dispatch.call(doc,"Close",newVariant(true));
doc=null;
}
}
privatevoidsetImgWaterMark(StringwaterMarkPath){
DispatchactivePan=Dispatch.get(activeDoc,"ActivePane").toDispatch();
Dispatchview=Dispatch.get(activePan,"View").toDispatch();
Dispatch.put(view,"SeekView",newVariant(9));
Dispatchheadfooter=Dispatch.get(docSelect,"HeaderFooter")
.toDispatch();
//取得图形对象
Dispatchshapes=Dispatch.get(headfooter,"Shapes").toDispatch();
Dispatchpic=Dispatch.call(shapes,"AddPicture",waterMarkPath)
.toDispatch();
Dispatch.call(pic,"Select");
Dispatch.put(pic,"Left",newVariant(10));
Dispatch.put(pic,"Top",newVariant(200));
Dispatch.put(pic,"Width",newVariant(150));
Dispatch.put(pic,"Height",newVariant(80));
Dispatch.put(view,"SeekView",newVariant(0));
}
publicvoidsetTextWaterMark(StringwaterMarkStr){
DispatchactivePan=Dispatch.get(activeDoc,"ActivePane").toDispatch();
Dispatchview=Dispatch.get(activePan,"View").toDispatch();
Dispatch.put(view,"SeekView",newVariant(9));
Dispatchheadfooter=Dispatch.get(docSelect,"HeaderFooter")
.toDispatch();
Dispatchshapes=Dispatch.get(headfooter,"Shapes").toDispatch();
Dispatchselection=Dispatch.call(shapes,"AddTextEffect",
newVariant(9),waterMarkStr,"宋体",newVariant(1),
newVariant(false),newVariant(false),newVariant(0),
newVariant(0)).toDispatch();
Dispatch.call(selection,"Select");
DispatchshapeRange=Dispatch.get(docSelect,"ShapeRange")
.toDispatch();
Dispatch.put(shapeRange,"Name","PowerPlusWaterMarkObject
1");
DispatchtextEffect=Dispatch.get(shapeRange,"TextEffect")
.toDispatch();
Dispatch.put(textEffect,"NormalizedHeight",newBoolean(false));
Dispatchline=Dispatch.get(shapeRange,"Line").toDispatch();
Dispatch.put(line,"Visible",newBoolean(false));
Dispatchfill=Dispatch.get(shapeRange,"Fill").toDispatch();
Dispatch.put(fill,"Visible",newBoolean(true));
//设置水印透明度
Dispatch.put(fill,"Transparency",newVariant(0.5));
DispatchforeColor=Dispatch.get(fill,"ForeColor").toDispatch();
Dispatch.put(foreColor,"RGB",newVariant(16711620));
Dispatch.call(fill,"Solid");
//设置水印旋转
Dispatch.put(shapeRange,"Rotation",newVariant(315));
Dispatch.put(shapeRange,"LockAspectRatio",newBoolean(true));
Dispatch.put(shapeRange,"Height",newVariant(117.0709));
Dispatch.put(shapeRange,"Width",newVariant(468.2835));
Dispatch.put(shapeRange,"Left",newVariant(-999995));
Dispatch.put(shapeRange,"Top",newVariant(-999995));
DispatchwrapFormat=Dispatch.get(shapeRange,"WrapFormat")
.toDispatch();
//是否允许交叠
Dispatch.put(wrapFormat,"AllowOverlap",newVariant(true));
Dispatch.put(wrapFormat,"Side",newVariant(3));
Dispatch.put(wrapFormat,"Type",newVariant(3));
Dispatch.put(shapeRange,"RelativeHorizontalPositi
on",newVariant(0));
Dispatch.put(shapeRange,"RelativeVerticalPosition
",newVariant(0));
Dispatch.put(view,"SeekView",newVariant(0));
}
privatevoidcloseWord(){
//关闭word文件
wrdCom.invoke("Quit",newVariant[]{});
//释放com线程
ComThread.Release();
}
publicStringgetDocName(){
returndocName;
}
publicvoidsetDocName(StringdocName){
this.docName=docName;
}
privatebooleanaddWaterMark(StringwordPath,StringwaterMarkPath){
booleanflag=false;
try{
if(initWord()){
openDocument(wordPath);
getActiveDoc();
setImgWaterMark(waterMarkPath);
closeDocument();
closeWord();
flag=true;
}else{
flag=false;
}
}catch(Exceptione){
flag=false;
e.printStackTrace();
closeDocument();
closeWord();
}
returnflag;
}
publicstaticvoidmain(String[]args){
TestJacobWordjacob=TestJacobWord.getInstance();
//jacob.addWaterMark("F://test//test.doc","F://test//ymo.jpg");
try{
if(jacob.initWord()){
jacob.openDocument("F://test/test.doc");
jacob.getActiveDoc();
jacob.setTextWaterMark("重庆宇能科技有限公司");
jacob.closeDocument();
jacob.closeWord();
}
}catch(Exceptione){
e.printStackTrace();
jacob.closeDocument();
jacob.closeWord();
}
}
}
‘捌’ JAVA编辑WORD文件插入图片
试试这个代码,需要添加spire.doc jar依赖
importcom.spire.doc.Document;
importcom.spire.doc.FileFormat;
importcom.spire.doc.Section;
importcom.spire.doc.documents.*;
importcom.spire.doc.fields.DocPicture;
publicclassInsertImage{
publicstaticvoidmain(String[]args){
//实例化Document对象
Documentdoc=newDocument();
//加载文档
doc.loadFromFile("C:\Users\Administrator\Desktop\test.docx");
//获取第一个section
Sectionsection=doc.getSections().get(0);
//添加一个段落
Paragraphpara=section.addParagraph();
//添加图片到段落
DocPicturepicture=para.appendPicture("C:\Users\Administrator\Desktop\Cartoon.png");
//设置文字环绕方式(居于文字上方)
picture.setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);
//指定图片的相对位置
picture.setHorizontalOrigin(HorizontalOrigin.Page);
picture.setHorizontalPosition(250f);
picture.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
picture.setVerticalPosition(150f);
//设置图片大小
picture.setWidth(80f);
picture.setHeight(80f);
//保存到文档
doc.saveToFile("output/InsertImage.docx",FileFormat.Docx);
}
}
生成的Word:
‘玖’ Java中怎么实现浏览器在线编辑Office文档
可以用第三方服务接口实现,但安全性要好,文档不能泄露出去
poi里有个ExcelToHtmlConverter 和 WordToHtmlConverter
这是在网上找的一段代码我没试
HWPFDocumentCore wordDocument = WordToHtmlUtils.loadDoc(new FileInputStream("D:\\temp\\seo\\1.doc"));
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder()
.newDocument());
wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
out.close();
String result = new String(out.toByteArray());
System.out.println(result);