一、纯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);。
二、用java打开一个word文档(a.docx) 怎么搞
package cn.rain.main;
import java.io.File;
import java.io.IOException;
public class TT {
/**
* @param args
*/
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("C:\\Program Files\\Microsoft Office\\OFFICE11\\WINWORD.EXE aa.doc");
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
你的WORD安装路径C:\\Program Files\\Microsoft Office\\OFFICE11\\WINWORD.EXE空格后所加的aa.doc为你的文档文件名(有文件名重复的,可以加路径,写法和安装路径的写法一样)
或者用Runtime.getRuntime().exec("cmd /c \"C:\\temp\\the list.doc\"");
也可以运行
三、Java程序把Word文档直接换成Html文件方法是什么呢
acob是Java和Windows下的Com桥,通过它我们可以在Java程序中调用COM组件。
如果你的JDK是1。4,那你需要下载Jacob 1。
9的jni库才能正常运行,早期版本在JDK 1。4下有些问题。
以下是引用片段:package com;/*** 〈p〉Title:Word文档转html类〈/p〉* 〈p〉Description: 〈/p〉* 〈p〉Copyright:() 2002〈/p〉* @author 舵手* @version 1。 0*/import com。
jacob。com。
*;import com。jacob。
activeX。*;public class WordtoHtml {/***文档转换函数*@param docfile word文档的绝对路径加文件名(包含扩展名)*@param htmlfile 转换后的html文件绝对路径和文件名(不含扩展名)*/public static void change(String docfile, String htmlfile) {ActiveXComponent app = new ActiveXComponent("Word。
Application"); // 启动wordtry {app。setProperty("Visible", new Variant(false));//设置word不可见Object docs = app。
getProperty("Documents")。 toDispatch();Object doc = Dispatch。
invoke(docs,"Open",Dispatch。Method,new Object[]{ docfile, new Variant(false),new Variant(true) }, new int[1])。
toDispatch();// 打开word文件Dispatch。invoke(doc, "SaveAs", Dispatch。
Method, new Object[] {htmlfile,new Variant (8) }, new int[1]);// 作为html格式保存到临时文件Variant f = new Variant(false);Dispatch。 call(doc, "Close", f);} catch (Exception e) {e。
printStackTrace();} finally {app。invoke("Quit", new Variant[]{});}}public static void main(String[] strs){WordtoHtml。
change("c:\\a\\运输管理调度系统总体方案。doc", "c:\\a\\t");}}。
四、Java如何操作Word?Java如何操作Word,Excel,
Java Excel API 文档 http://www。
andykhan。com/jexcelapi/ 1、一个jacob操作Word的例子,其他操作excel,pdf的sample里都有 import java。
io。File; import com。
jacob。com。
*; import com。jacob。
activeX。*; public class WordTest { public static void main(String[] args) { WordBean word=new WordBean(); word。
openWord(true); word。createNewDocument(); word。
insertText("Hello word。"); } } import com。
jacob。activeX。
*; import com。 jacob。
com。*; public class WordBean extends java。
awt。Panel { private ActiveXComponent MsWordApp = null; private Dispatch document = null; public WordBean() { super(); } public void openWord(boolean makeVisible) { //Open Word if we've not done it already if (MsWordApp == null) { MsWordApp = new ActiveXComponent("Word。
Application"); } //Set the visible property as required。 Dispatch。
put(MsWordApp, "Visible", new Variant(makeVisible)); } public void createNewDocument() { //Find the Documents collection object maintained by Word Dispatch documents = Dispatch。 get(MsWordApp,"Documents")。
toDispatch(); //Call the Add method of the Documents collection to create //a new document to edit document = Dispatch。 call(documents,"Add")。
toDispatch(); } public void insertText(String textToInsert) { // Get the current selection within Word at the moment。 If // a new document has just been created then this will be at // the top of the new doc Dispatch selection = Dispatch。
get(MsWordApp,"Selection")。toDispatch(); //Put the specified text at the insertion point Dispatch。
put(selection,"Text",textToInsert); } public void saveFileAs(String filename) { Dispatch。 call(document,"SaveAs",filename); } public void printFile() { //Just print the current document to the default printer Dispatch。
call(document,"PrintOut"); } public void closeDocument() { // Close the document without saving changes // 0 = wdDoNotSaveChanges // -1 = wdSaveChanges // -2 = wdPromptToSaveChanges Dispatch。 call(document, "Close", new Variant(0)); document = null; } public void closeWord() { Dispatch。
call(MsWordApp,"Quit"); MsWordApp = null; document = null; } }。
五、Java读取数据库打印到word文档问题求教各位大侠,java写
为什么非要写道word文档中? 你可以写进txt文件中,然后使用word进行编辑 如果确实需要,请看下面的例子: import java。
io。File; import java。
io。FileInputStream; import java。
io。FileOutputStream; import org。
apache。poi。
hwpf。extractor。
WordExtractor; import org。apache。
poi。hwpf。
model。io。
HWPFOutputStream; public class Word { /** * 读取纯文本的word文件 */ public String readWord(String doc) throws Exception { String context = null; WordExtractor extractor = null; //纯文本的遍历器 try { FileInputStream in = new FileInputStream(new File(doc)); extractor = new WordExtractor(in); context = extractor。 getText(); } catch (Exception e) { e。
printStackTrace(); } return context; } /** * 对word文档做写操作 * */ public boolean writeWord(String path, String content) throws Exception { boolean w = false; try { byte b[] = content。 getBytes(); FileOutputStream fs = new FileOutputStream(path); HWPFOutputStream hos = new HWPFOutputStream(); hos。
write(b, 0, b。length); hos。
writeTo(fs); hos。close(); w=true; } catch (Exception e) { e。
printStackTrace(); } return w; } public static void main(String[] args) { ReadWord rw = new ReadWord(); try{ String text = rw。 readDoc("D:\\workspace\\MyUntil\\t。
doc"); rw。writeDoc("D:\\workspace\\MyUntil\\d。
doc", text); }catch(Exception e){ e。 printStackTrace(); } } } 所需的包请各位自己上apache的官网上poi工程目录下下吧 poi-3。
5-final poi-contrib-3。5-final poi-ooxml-3。
5-final poi-scratchpad-3。5-final 。
六、如何使用java操作word 文档
可以考虑Apache POI ,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.poi.hwpf.extractor.WordExtractor;
/**读取word文本信息*/
public class WordDemo {
public static void main(String[] args) {
try {
InputStream is = new FileInputStream(new File("src/123.doc"));
@SuppressWarnings("resource")
String text123 = new WordExtractor(is).getText();
System.out.println(text123);
} catch (Exception e) {
e.printStackTrace();
}
}
}
转载请注明出处51数据库 » java在线看word文档