1.java如何根据word模板生成word文档
先下载jacob_1.10.1.zip。
解压后将jacob.dll放到windows/system32下面或\j2sdk\bin下面。 将jacob.jar加入项目。
/* * Java2word.java * * Created on 2007年8月13日, 上午10:32 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ /* * 传入数据为HashMap对象,对象中的Key代表word模板中要替换的字段,Value代表用来替换的值。 * word模板中所有要替换的字段(即HashMap中的Key)以特殊字符开头和结尾,如:$code$、$date$……, 以免执行错误的替换。
* 所有要替换为图片的字段,Key中需包含image或者Value为图片的全路径(目前只判断文件后缀名为:.bmp、.jpg、.gif)。 * 要替换表格中的数据时,HashMap中的Key格式为“table$R@N”,其中:R代表从表格的第R行开始替换,N代表 word模板中的第N张表格;Value为ArrayList对象,ArrayList中包含的对象统一为String[],一条String[]代 表一行数据,ArrayList中第一条记录为特殊记录,记录的是表格中要替换的列号,如:要替换第一列、第三列、第五列的数据,则第一条记录为String[3] {“1”,”3”,”5”}。
*/ package com.word.util; /** * * @author kdl */ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class Java2word { private boolean saveOnExit; /** * word文档 */ Dispatch doc = null; /** * word运行程序对象s */ private ActiveXComponent word; /** * 所有word文档 */ private Dispatch documents; /** * 构造函数 */ public Java2word() { if(word==null){ word = new ActiveXComponent("Word.Application"); word.setProperty("Visible",new Variant(false)); } if(documents==null) documents = word.getProperty("Documents").toDispatch(); saveOnExit = false; } /** * 设置参数:退出时是否保存 * @param saveOnExit boolean true-退出时保存文件,false-退出时不保存文件 */ public void setSaveOnExit(boolean saveOnExit) { this.saveOnExit = saveOnExit; } /** * 得到参数:退出时是否保存 * @return boolean true-退出时保存文件,false-退出时不保存文件 */ public boolean getSaveOnExit() { return saveOnExit; } /** * 打开文件 * @param inputDoc String 要打开的文件,全路径 * @return Dispatch 打开的文件 */ public Dispatch open(String inputDoc) { return Dispatch.call(documents,"Open",inputDoc).toDispatch(); //return Dispatch.invoke(documents,"Open",Dispatch.Method,new Object[]{inputDoc},new int[1]).toDispatch(); } /** * 选定内容 * @return Dispatch 选定的范围或插入点 */ public Dispatch select() { return word.getProperty("Selection").toDispatch(); } /** * 把选定内容或插入点向上移动 * @param selection Dispatch 要移动的内容 * @param count int 移动的距离 */ public void moveUp(Dispatch selection,int count) { for(int i = 0;i < count;i="" ++)="" dispatch.call(selection,"moveup");="" }="" *="" *="" 把选定内容或插入点向下移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */="" public="" void="" movedown(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)="" dispatch.call(selection,"movedown");="" }="" *="" *="" 把选定内容或插入点向左移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */="" public="" void="" moveleft(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)="" {="" dispatch.call(selection,"moveleft");="" }="" }="" *="" *="" 把选定内容或插入点向右移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */="" public="" void="" moveright(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)="" dispatch.call(selection,"moveright");="" }="" *="" *="" 把插入点移动到文件首位置="" *="" @param="" selection="" dispatch="" 插入点="" */="" public="" void="" movestart(dispatch="" selection)="" {="" dispatch.call(selection,"homekey",new="" variant(6));="" }="" *="" *="" 从选定内容或插入点开始查找文本="" *="" @param="" selection="" dispatch="" 选定内容="" *="" @param="" tofindtext="" string="" 要查找的文本="" *="" @return="" boolean="" true-查找到并选中该文本,false-未查找到文本="" */="" public="" boolean="" find(dispatch="" selection,string="" tofindtext)="" {="" 从selection所在位置开始查询="" dispatch="" find="word.call(selection,"Find").toDispatch();" 设置要查找的内容="" dispatch.put(find,"text",tofindtext);="" 向前查找="" dispatch.put(find,"forward","true");="" 设置格式="" dispatch.put(find,"format","true");="" 大小写匹配="" dispatch.put(find,"matchcase","true");="" 全字匹配="" dispatch.put(find,"matchwholeword","true");="" 查找并选中="" return="" dispatch.call(find,"execute").getboolean();="" }="" *="" *="" 把选定内容替换为设定文本="" *="" @param="" selection="" dispatch="" 选定内容="" *="" @param="" newtext="" string="" 替换为文本="" */="" public="" void="">
2.java导出word表格
首先我用的技术是 poi 这是代码,一个工具类得调用public class WordUtil { /** * 基于模板文件导出 word 文档,此方法主要是用来处理文档中需要替换的文本内容,对图片和表格无效 * * @param templatePath * 模板文件的路径,要求路径中要包含全名,并且模板文件只能是 07 及以上格式,即 docx 的文件 * @param destFilePath * 导出文件的存放路径,包含文件名,例如,E:/test/小区公告.docx * @param data * 用来替换文档中预定义的字符串,要求预定义的字符串与 data 中的 key 值要相同 */ public static void exportWordByTemplate(String templatePath, String destFilePath, Map E:/test/test.doc 新生成的地址。 java导出word大致有6种解决方案:1:Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。 使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。DLL动态链接库的生成需要windows平台的支持。 该方案只能在windows平台实现,是其局限性。2:Apache POI包括一系列的API,它们可以操作基于MicroSoft OLE 2 Compound Document Format的各种格式文件,可以通过这些API在Java中读写Excel、Word等文件。 他的excel处理很强大,对于word还局限于读取,目前只能实现一些简单文件的操作,不能设置样式。3:Java2word是一个在java程序中调用 MS Office Word 文档的组件(类库)。 该组件提供了一组简单的接口,以便java程序调用他的服务操作Word 文档。 这些服务包括: 打开文档、新建文档、查找文字、替换文字,插入文字、插入图片、插入表格,在书签处插入文字、插入图片、插入表格等。 填充数据到表格中读取表格数据 ,1.1版增强的功能: 指定文本样式,指定表格样式。如此,则可动态排版word文档。 是一种不错的解决方案。4:iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。 通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。功能强大。 5:JSP输出样式,该方案实现简单,但是处理样式有点缺陷,简单的导出可以使用。6:用XML做就很简单了。 Word从2003开始支持XML格式,大致的思路是先用office2003或者2007编辑好word的样式,然后另存为xml,将xml翻译为FreeMarker模板,最后用java来解析FreeMarker模板并输出Doc。经测试这样方式生成的word文档完全符合office标准,样式、内容控制非常便利,打印也不会变形,生成的文档和office中编辑文档完全一样。 阅读目录 1. 制作 Word 模版,将你需要动态生成的字段用${}替换。 2. 将 Word文档保存为 xml 。 3. 引入项目。 项目中需要用 java 程序生成doc 文件,百度一番,发现FreeMarker 的评价比较高。 FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出,至于想详细了解 FreeMarker 的请自行百度。 1. 制作 Word 模版,将你需要动态生成的字段用${}替换。如:${name}、${age} 2. 将 Word文档保存为 xml。 3. 引入项目。将 xml 文件更改后缀名 为 .ftl, 然后引用到你的项目中。 4. 需要注意的问题: a. word 版本不能低于 2003 ,因为 2003 才开始支持 xml。 5. 备注: a. template.process(),接受一个 Map 和 输入流做为入参,Map 既是你需要动态生成到 doc 里面的数据,字段名必须与你在 .ftl 里面定义的一致; b. 如果你想利用这段代码,需要有一个 .ftl 文件,并且在你的项目 src 目录下面新建 template 目录; c.辅助文件"FreeMarker.jar 、实例项目的.ftl "。 Jacob解决Word文档的读写问题收藏Jacob 是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。 使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。Jacob下载的地址为:.jacob.activeX.ActiveXComponent;import com.jacob.com.Dispatch;import com.jacob.com.Variant;public class WordOperate { public static void main(String args[]) { ActiveXComponent wordApp = new ActiveXComponent("Word.Application"); // 启动word // Set the visible property as required. Dispatch.put(wordApp, "Visible", new Variant(true));// //设置word可见 Dispatch docs = wordApp.getProperty("Documents").toDispatch(); // String inFile = "d:\\test.doc"; // Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, // new Object[] { inFile, new Variant(false), new Variant(false)},//参数3,false:可写,true:只读 // new int[1]).toDispatch();//打开文档 Dispatch document = Dispatch.call(docs, "Add").toDispatch();// create new document String userName = wordApp.getPropertyAsString("Username");// 显示用户信息 System.out.println("用户名:" + userName); // 文档对齐,字体设置//////////////////////// Dispatch selection = Dispatch.get(wordApp, "Selection").toDispatch(); Dispatch align = Dispatch.get(selection, "ParagraphFormat") .toDispatch(); // 行列格式化需要的对象 Dispatch font = Dispatch.get(selection, "Font").toDispatch(); // 字型格式化需要的对象 // 标题处理//////////////////////// Dispatch.put(align, "Alignment", "1"); // 1:置中 2:靠右 3:靠左 Dispatch.put(font, "Bold", "1"); // 字型租体 Dispatch.put(font, "Color", "1,0,0,0"); // 字型颜色红色 Dispatch.call(selection, "TypeText", "Word文档处理"); // 写入标题内容 Dispatch.call(selection, "TypeParagraph"); // 空一行段落 Dispatch.put(align, "Alignment", "3"); // 1:置中 2:靠右 3:靠左 Dispatch.put(selection, "Text", " "); Dispatch.call(selection, "MoveDown"); // 光标标往下一行 //表格处理//////////////////////// Dispatch tables = Dispatch.get(document, "Tables").toDispatch(); Dispatch range = Dispatch.get(selection, "Range").toDispatch(); Dispatch table1 = Dispatch.call(tables, "Add", range, new Variant(3), new Variant(2), new Variant(1)).toDispatch(); // 设置行数,列数,表格外框宽度 // 所有表格 Variant tableAmount = Dispatch.get(tables, "count"); System.out.println(tableAmount); // 要填充的表格 Dispatch t1 = Dispatch.call(tables, "Item", new Variant(1)) .toDispatch(); Dispatch t1_row = Dispatch.get(t1, "rows").toDispatch();// 所有行 int t1_rowNum = Dispatch.get(t1_row, "count").getInt(); Dispatch.call(Dispatch.get(t1, "columns").toDispatch(), "AutoFit");// 自动调整 int t1_colNum = Dispatch.get(Dispatch.get(t1, "columns").toDispatch(), "count").getInt(); System.out.println(t1_rowNum + " " + t1_colNum); for (int i = 1; i <= t1_rownum;="" i++)="" {="" for="" (int="" j="1;" j="">=><= t1_colnum;="" j++)="" {="" dispatch="" cell="Dispatch.call(t1," "cell",="" new="" variant(i),="" new="" variant(j)).todispatch();//="" 行,列="" dispatch.call(cell,="" "select");="" dispatch.put(selection,="" "text",="" "cell"="" +="" i="" +="" j);="" 写入word的内容="" dispatch.put(font,="" "bold",="" "0");="" 字型租体(1:租体="" 0:取消租体)="" dispatch.put(font,="" "color",="" "1,1,1,0");="" 字型颜色="" dispatch.put(font,="" "italic",="" "1");="" 斜体="" 1:斜体="" 0:取消斜体="" dispatch.put(font,="" "underline",="" "1");="" 下划线="" dispatch="" range="Dispatch.get(cell," "range").todispatch();="" string="" cellcontent="Dispatch.get(Range," "text").tostring();="" system.out.println((cellcontent.substring(0,="" cellcontent="" .length()="" -="" 1)).trim());="" }="" dispatch.call(selection,="" "movedown");="" 光标往下一行(才不会输入盖过上一输入位置)="" }="" 合并单元格////////////////////////="" dispatch.put(selection,="" "text",="" "="">=> 首先我用的技术是 poi 这是代码,一个工具类得调用 public class WordUtil { /** * 基于模板文件导出 word 文档,此方法主要是用来处理文档中需要替换的文本内容,对图片和表格无效 * * @param templatePath * 模板文件的路径,要求路径中要包含全名,并且模板文件只能是 07 及以上格式,即 docx 的文件 * @param destFilePath * 导出文件的存放路径,包含文件名,例如,E:/test/小区公告.docx * @param data * 用来替换文档中预定义的字符串,要求预定义的字符串与 data 中的 key 值要相同 */ public static void exportWordByTemplate(String templatePath, String destFilePath, Map E:/test/test.doc 新生成的地址。 首先我用的技术是 poi 这是代码,一个工具类得调用 public class WordUtil { /** * 基于模板文件导出 word 文档,此方法主要是用来处理文档中需要替换的文本内容,对图片和表格无效 * * @param templatePath * 模板文件的路径,要求路径中要包含全名,并且模板文件只能是 07 及以上格式,即 docx 的文件 * @param destFilePath * 导出文件的存放路径,包含文件名,例如,E:/test/小区公告.docx * @param data * 用来替换文档中预定义的字符串,要求预定义的字符串与 data 中的 key 值要相同 */ public static void exportWordByTemplate(String templatePath, String destFilePath, Mapdata) { FileOutputStream out = null; XWPFDocument doc = null; try { doc = new XWPFDocument(POIXMLDocument.openPackage(templatePath)); ListlistRun; ListlistParagraphs = doc.getParagraphs(); for (int i = 0; i data) { try { WordExtractor doc = new WordExtractor(new FileInputStream( templatePath)); String content = doc.getText(); for (String key : data.keySet()) { content = content.replaceAll(key, data.get(key)); } byte b[] = content.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(b); POIFSFileSystem fs = new POIFSFileSystem(); DirectoryEntry directory = fs.getRoot(); directory.createDocument("WordDocument", bais); FileOutputStream ostream = new FileOutputStream(destFilePath); fs.writeFilesystem(ostream); bais.close(); ostream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { Mapmaps = new HashMap(); maps.put("appellation", "万达公寓业主:"); maps.put( "main_body", "输出的内容"); maps.put("date", "2013年1月23日"); exportWordByTemplate("E:/sss 2.docx", "E:/test/test.doc", maps); } } "E:/sss 2.docx 模板存放的地址。 E:/test/test.doc 新生成的地址。 转载请注明出处51数据库 » java导出word模板替换3.怎么用java导出word文档
4.java动态生成word,该怎么解决
5.java生成word文档的问题
6.java导出word表格
7.java word中表格如何排版输出
袅男