一、java用POI第三方API操作word的时候,读取最终状态
java读取word文档时,虽然网上介绍了很多插件poi、java2Word、jacob、itext等等,poi无法读取格式(新的API估计行好像还在处于研发阶段,不太稳定,做项目不太敢用);java2Word、jacob容易报错找不到注册,比较诡异,我曾经在不同的机器上试过,操作方法完全一致,有的机器不报错,有的报错,去他们论坛找高人解决也说不出原因,项目部署用它有点玄;itxt好像写很方便但是我查了好久资料没有见到过关于读的好办法。
经过一番选择还是折中点采用rtf最好,毕竟rtf是开源格式,不需要借助任何插件,只需基本IO操作外加编码转换即可。rtf格式文件表面看来和doc没啥区别,都可以用word打开,各种格式都可以设定。
二、java进行word文档的开发一般使用什么技术
需要借助一些Word操作库才行,如免费版Spire.Doc for Java, 下面是该组件生成简单的Word文档 import com.spire.doc.*;import com.spire.doc.documents.Paragraph;public class helloWorld { public static void main(String[] args) { String output = "output/helloWorld.docx"; //create Word document Document document = new Document(); //create a new section Section section = document.addSection(); //create a new paragraph Paragraph paragraph = section.addParagraph(); //append text paragraph.appendText("Hello World!"); //save the file document.saveToFile(output, FileFormat.Docx); }}。
三、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的方法有几个,列出来:IText,Apache的POI包,tm-extractors包,jacob包,java2word包。
IText包比较大,但是功能比较全。而且比较成熟,用的人也多。
POI包是Apache的一个项目,不过现在已经停止更新了,而且我看了里面的Example,感觉调用起来很麻烦。用过之后再也不想用了。
tm-extractors包的API比较简单,调用起来很方便,代码量也少。
jacob包我觉得是最强大的,可以操作word,excel。因为调用的是微软底层的dll文件,需要在system32目录下放置jacob.dll,然后在classpath里面添加jacob.jar文件。推荐使用1.9版本,之前1.8版本调用起来就算你的代码没有写错也会出现一些莫名其妙的异常。
java2word也是调用微软的底层dll文件,有安装包,也有解压包,安装包会将jacob.dll文件装在system32目录下。之后将java2word.jar加到classpath就可以使用了。感觉如果只是操作word文档的话,比jacob用起来要简单和舒服。代码量也少,很清晰。
另外,使用jacob和java2word的话,会在调用WINWORD进程,如果多人并发访问服务器的word文件的话,会造成内存不足。
五、java进行word文档的开发一般使用什么技术
需要借助一些Word操作库才行,如免费版Spire.Doc for Java, 下面是该组件生成简单的Word文档
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
public class helloWorld {
public static void main(String[] args) {
String output = "output/helloWorld.docx";
//create Word document
Document document = new Document();
//create a new section
Section section = document.addSection();
//create a new paragraph
Paragraph paragraph = section.addParagraph();
//append text
paragraph.appendText("Hello World!");
//save the file
document.saveToFile(output, FileFormat.Docx);
}
}
转载请注明出处51数据库 » java2wordapi