java用poi导出word文档,我要导出一个表格,表格的单元格中还要有...
有个最简单,不用使用任何 poi 或其他第三方类库的方法生成 excel ,或 word ,你把一个生成好的 word 例子用文件另存为 xml , docx 之类,然后你再直接用记事本去打开这个 docx ,你就会发现是 xml 格式,这个时候,你在程序里面就直接用普通代码生成这个 xml 就可以了。
java poi怎么向word文档中插入图片
Jacob解决Word文档的读写问题收藏Jacob 是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。
使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。
Jacob下载的地址为:http://sourceforge.net/project/showfiles.php?group_id=109543&package_id=118368配置:(1)将解压包中的jacob.dll(x86常用,x64)拷到jdk安装目录下的jre\bin文件夹或windows安装路径下的WINDOWS\system32文件夹下(2)将jacob.jar文件拷到classpath下即可常见问题解决:对于”java.lang.UnsatisfiedLinkError: C:\WINDOWS\system32\jacob-1.14.3-x86.dll: 由于应用程序配置不正确,应用程序未能启动。
重新安装应用程序可能会纠正”这个问题,可以通过重新下载Jacob的jar及dll文件(最好版本比现在的低,如1.11)解决实例制作(主要功能:标题制作,表格制作,合并表格,替换文本,页眉页脚,书签处理):import com.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", " "); Dispatch.call(selection, "MoveDown"); // 光标标往下一行 Dispatch range2 = Dispatch.get(selection, "Range").toDispatch(); Dispatch table2 = Dispatch.call(tables, "Add", range2, new Variant(8), new Variant(4), new Variant(1))....
java程序,使用poi,用word模板,如何在分页后重复展示表头
// 表格类型ListtableList = doc.getTables();for (int i = 0; i System.out.println(i);XWPFTable table = tableList.get(i);System.out.println(table.getText());}获取表格中内容可以用这个,但是你说的格式是什么意思,每个字的字体之类的吗?
java POI 如何读取word的表格中的表格
自己在做项目过程中用到了解析分割word2003和2007,2010等文件内容,以下是代码:下面是部分代码,如果想要全部代码可以发我邮箱,yongqian.liu@peraglobal.com,接口类 :PoiExtractContent.javapackage com.peraglobal.extract.poi;import java.util.Map;/*** 使用 POI 解析 WORD 文件的内容信息* @author yongqian.liu* 2015-2-9*/public interface PoiExtractContent {/*** 根据文件路径获得 Document 对象* @param docPath 路径* @return Document*/public T getDocument(String docPath);/*** 解析 word 文档的标题* @param doc Document 对象* @return word 文档中标题*/public String getTilte(T doc);/*** 获取 word 文档里所有文字内容(不包括图片、表格等格式的内容)* @param doc Document 对象* @return word 文档中文字部分全部内容*/public String getContent(T doc);/*** 获取 word 文档里所有文字内容(不包括图片、表格等格式的内容)* @param docPath doc 对象路径* @return word 文档中文字部分全部内容*/public String getContent(String docPath);/*** 获取 word 文档里面所有图片并另存到指定目录下* @param doc Document 对象* @param picPath 保存图片路径* @param suffix 后缀名*/public void getPictures(T doc, String picPath, String suffix);/*** 获取word 文档里面所有表格* @param doc Document 对象*/public void getTables(T doc);/*** 获取word 文档中最大的字体* @param doc Document 对象* @return 最大字体*/public int getMaxFontSize(T doc);/*** 获取 word 文档的创建信息* @param docPath doc路径* @return 创建文档的信息*/public Map getInfo(String docPath);}
java POI html转word,无法显示图片
我也刚解决了这个问题 作为参考你可以试一下(我是选择了第一种方法)1)在安装时解决:解决方法:安装的时候一定自定义安装(这里是我的习惯,不然垃圾会装了很多),以2003的版本为例的话,自定义的下一步有个,高级。
。
前面的勾打上,以便下面自己设置。
再下一步是 选择要安装的组件,里面 有个Office共享工具,这个,关键所在。
。
不信展开看一下,里面有个,新建。
。
什么什么的。
。
默认是不安装的,这里我们选择从本地运行。
再安装就OK了2)安装后的解决:有些简版的OFFICE2003,右键的新建菜单中没有“新建WORD文档”。
在网上搜了好多的资料都是无效的,下面是一项编辑注册表的信息,把横线中的信息复制的记事本中,然后另存为 *.reg格式的文件。
----------------------------------- Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.doc] @="Word.Document.8""Content Type"="application/msword" [HKEY_CLASSES_ROOT\.doc\OpenWithList] [HKEY_CLASSES_ROOT\.doc\OpenWithList\WordPad.exe] @="" [HKEY_CLASSES_ROOT\.doc\PersistentHandler] @="{98de59a0-d175-11cd-a7bd-00006b827d94}" [HKEY_CLASSES_ROOT\.doc\ShellNew] [HKEY_CLASSES_ROOT\.doc\Word.Document.6] [HKEY_CLASSES_ROOT\.doc\Word.Document.6\ShellNew]"FileName"="winword.doc" [HKEY_CLASSES_ROOT\.doc\Word.Document.8] [HKEY_CLASSES_ROOT\.doc\Word.Document.8\ShellNew]"FileName"="winword8.doc" [HKEY_CLASSES_ROOT\.doc\WordDocument] [HKEY_CLASSES_ROOT\.doc\WordDocument\ShellNew]"FileName"="winword2.doc" [HKEY_CLASSES_ROOT\.doc\WordPad.Document.1] [HKEY_CLASSES_ROOT\.doc\WordPad.Document.1\ShellNew]"NullFile"="" ----------------------------------- 以下是新建EXCEL文档的注册信息(.xls) ----------------------------------- Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.xls]"Content Type"="application/vnd.ms-excel" @="Excel.Sheet.8" [HKEY_CLASSES_ROOT\.xls\Excel.Sheet.5] [HKEY_CLASSES_ROOT\.xls\Excel.Sheet.5\ShellNew]"FileName"="excel.xls" [HKEY_CLASSES_ROOT\.xls\Excel.Sheet.8] @="" [HKEY_CLASSES_ROOT\.xls\Excel.Sheet.8\ShellNew]"FileName"="excel9.xls" [HKEY_CLASSES_ROOT\.xls\ExcelWorksheet] [HKEY_CLASSES_ROOT\.xls\ExcelWorksheet\ShellNew]"FileName"="excel4.xls" [HKEY_CLASSES_ROOT\.xls\PersistentHandler] @="{98de59a0-d175-11cd-a7bd-00006b827d94}" ----------------------------------- 以下是powerpiont幻灯片文档的注册信息(.ppt) ----------------------------------- Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.ppt]"Content Type"="application/vnd.ms-powerpoint" @="PowerPoint.Show.8" [HKEY_CLASSES_ROOT\.ppt\PersistentHandler] @="{98de59a0-d175-11cd-a7bd-00006b827d94}" [HKEY_CLASSES_ROOT\.ppt\PowerPoint.Show.4] [HKEY_CLASSES_ROOT\.ppt\PowerPoint.Show.4\ShellNew]"FileName"="powerpnt.ppt" [HKEY_CLASSES_ROOT\.ppt\PowerPoint.Show.8] [HKEY_CLASSES_ROOT\.ppt\PowerPoint.Show.8\ShellNew]"FileName"="pwrpnt11.pot" 双击该注册项。
会弹出“是否确认要将***.reg中的信息添加到注册表” 点击“是”。
刷新一下就可以看到右键菜单中的新建项的word文档了。
转载请注明出处51数据库 » java poi创建word文档
文氓兄弟