一、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文档时,虽然网上介绍了很多插件poi、java2Word、jacob、itext等等,poi无法读取格式(新的API估
计行好像还在处于研发阶段,不太稳定,做项目不太敢用);java2Word、jacob容易报错找不到注册,比较诡异,我曾经在不同的机器上试过,操作
方法完全一致,有的机器不报错,有的报错,去他们论坛找高人解决也说不出原因,项目部署用它有点玄;itxt好像写很方便但是我查了好久资料没有见到过关
于读的好办法。经过一番选择还是折中点采用rtf最好,毕竟rtf是开源格式,不需要借助任何插件,只需基本IO操作外加编码转换即可。rtf格式文件表
面看来和doc没啥区别,都可以用word打开,各种格式都可以设定。
三、怎么用java poi生成word表格
rt java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class XwpfTUtil {
/*String filePath = "/sta.docx";
InputStream is;
XWPFDocument doc;
Mapparams = new HashMap();
{
params.put("${name}", "xxx");
params.put("${sex}", "男");
params.put("${political}", "共青团员");
params.put("${place}", "sssss");
params.put("${classes}", "3102");
params.put("${id}", "213123123");
params.put("${qq}", "213123");
params.put("${tel}", "312313213");
params.put("${oldJob}", "sadasd");
params.put("${swap}", "是");
params.put("${first}", "asdasd");
params.put("${second}", "综合事务部");
params.put("${award}", "asda");
params.put("${achievement}", "完成科协网站的开发");
params.put("${advice}", "没有建议");
四、Java解析word文档
原因被我找到了,因为你的文件里面有换行,点星不支持,所以改用下面的匹配,你一定能成功的!!!!!String reg1 = "[\\s\\S]*性别\\s*(:|:)\\s*([^\\|]*)\\|?[\\s\\S]*";String reg2 = "[\\s\\S]*民族\\s*(:|:)\\s*([^\\|]*)\\|?[\\s\\S]*";String reg3 = "[\\s\\S]*生日\\s*(:|:)\\s*([\\d\\-]*)\\|?[\\s\\S]*";String str = "性别:女士|民族:汉族|生日:1990-9-22\n" + "婚姻状况:已婚|身高:165厘米|体重:50公斤\n" + "户口所在地:广东省顺德容桂镇|目前所在地:广东省顺德容桂镇";String sex = str.replaceAll(reg1, "$2");String mz = str.replaceAll(reg2, "$2");String brith = str.replaceAll(reg3, "$2");System.out.println (sex);System.out.println (mz);System.out.println (brith);你改动了啊,我给你的原版不是这样的啊。
转载请注明出处51数据库 » java解析word表格