『壹』 如何實現在線編輯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);