- 浏览: 114538 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
HuoYun:
兄弟,这个代码对于其他excel转换会出问题,如数据库导出的e ...
excel文件转换为pdf文件的问题总结 -
listen-raining:
这两天正在弄这个东东
excel文件转换为pdf文件的问题总结 -
李001:
你是英雄!
excel文件转换为pdf文件的问题总结 -
237304457:
少文,功 能有全事例不,
excel文件转换为pdf文件的问题总结 -
237304457:
提供大资源是哪种方式呀?
excel文件转换为pdf文件的问题总结
首先,大家先要了解一下jacob ,官方的解释是Java COM Bridge,即java和 com组件间的桥梁,这里说说为什么我们用jacob操纵word。而不直接使用java去做?
这要原因:在Java开源世界没有很好工具来操作Word文档,POI对word操作还是很不完善,所以我们无法使用它很方便操作word文档来满足我们需求。相比之下使用jacob操作word文档非常方便。也比较容易。
jacob 下载地址:http://danadler.com/jacob/这个网址还可以下载到源码和例子程序
jacob 使用方法:将jacob1.7里面jacob.jar添加到我们应用程序环境中,并将 jacob.dl(l就是我前面说的com组件)把放到c:/windows/system32下。如果是web环境中,需要将jacod.jar放到Tomcat的lib目录下.(如果用Tomcat服务器)
值得注意的是,不同的版本的系统使用不同的dll文件
所以如果你编译成功,但运行失败一般是dll文件问题
遇到这种情况,可以到
http://downloads.sourceforge.net/jacob-project/jacob_1.9.zip?modtime=1109437002&big_mirror=0
下载其他的版本的 dll 文件。
下面这段程序是我在别人代码基础上进行一些改进(增加了一些新方法,渴望各位同行批评指正)
package com.bperp.word.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; public class WordWriter { private WordOperator word; public WordWriter(String filePath){ word=new WordOperator(); word.openDocument(filePath); } public WordWriter(InputStream input,String filePath,String fileName) throws Exception{ String path=saveAsDocFile(input,filePath,fileName); word=new WordOperator(); word.openDocument(path); } /** * 将word文档输入流保存为本地得到word文件 * @param input * @param filePath * @param fileName * @throws Exception */ @SuppressWarnings("unused") private String saveAsDocFile(InputStream input,String filePath,String fileName)throws Exception{ if(!StringUtils.isValidateString(filePath)||!StringUtils.isValidateString(fileName)){ throw new Exception("The filePath or fileName is error"); } if(input==null){ throw new Exception("InputStream is null"); } File file=new File(filePath); if(!file.exists()){ throw new Exception(" The FilePath is null"); } filePath = validateFilePath(filePath); fileName = getRandomFileName(fileName); InputStream in=null; OutputStream out=null; try{ in=new BufferedInputStream(input); out=new BufferedOutputStream(new FileOutputStream(filePath+fileName)); byte[] b=new byte[1024]; for(int p=0; (p=in.read(b))!=-1;){ out.write(b); out.flush(); } }finally{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } return filePath+fileName; } /** * 验证Word文件路径 * @param filePath * @return */ private String validateFilePath(String filePath) { if((filePath.lastIndexOf("\\\\")==-1)&&(filePath.lastIndexOf("/")==-1)){ filePath=filePath+"/"; } return filePath; } /** * 生成一个新的文件名(保证文件名不相同) * @param fileName * @return */ private String getRandomFileName(String fileName) { fileName= fileName + "_"+ new SimpleDateFormat("yyyyMMddHHmmssZ").format(new Date())+".doc"; return fileName; } /** * replaceText * @param map */ public void replaceAllText(Map<String,String> map){ if(map==null){ return; } Set<String> keys=map.keySet(); Iterator<String> it=keys.iterator(); while(it.hasNext()){ String key=it.next(); word.replaceAllText(key, map.get(key)); } } /** * add details * @param values */ public void insertContextInRow(List<Map<String,String>> values,int tableIndex){ if(tableIndex<=1){ tableIndex=1; } if(values==null||values.size()<=0){ return; } int[] p=null; Map<String,String> m=values.get(0); Set<String> keys=m.keySet(); Iterator<String> it=keys.iterator(); while(it.hasNext()){ String str=it.next(); int[] a=word.getTableCellPostion(str, tableIndex); if(a!=null&&a[0]!=0){ p=a; } } if(p!=null&&p[0]!=0){ for(int i=1;i<values.size();i++){ word.addTableRow(tableIndex,p[0]);//在表格插入行数 } } Iterator<String> it2=keys.iterator(); while(it2.hasNext()){ int row=p[0]; int col=0; String str=it2.next(); int[]a=word.getTableCellPostion(str, tableIndex); if(a!=null){ col=a[1]; } for(Map<String,String> map:values){ word.putTxtToCell(tableIndex, row, col, map.get(str)); row++; } } } /** * close document */ public void close(){ word.closeDocument(); word.close(); } /** * 依据Word文件完整路径删除文件 * @param path * @throws Exception */ public void deleteWordFile(String path) throws Exception{ File f=new File(path); if(!f.exists()){ throw new Exception("The file is not exists"); } f.delete(); } /** * * @param args * @throws Exception */ public static void main(String args[]) throws Exception{ InputStream in=new FileInputStream("d:\\aaa.doc"); String path="d:\\qq"; String fileName="aaa"; WordWriter writer=new WordWriter(in,path,fileName); Map<String,String> map1=new HashMap<String,String>(); map1.put("p21", "上海商哲"); map1.put("p12", "1550"); writer.replaceAllText(map1); List<Map<String,String>> values =new ArrayList<Map<String,String>>(); for(int i=0;i<10;i++){ Map<String,String> map=new HashMap<String,String>(); map.put("$1", "111111111111"); map.put("$2", "222222222222"); map.put("$3", "333333333333"); map.put("$4", "444444444444"); map.put("$5", "555555555555"); map.put("$6", "666666666666"); values.add(map); } writer.insertContextInRow(values, 1); writer.close(); } }
- jacobBin_17.zip (174.4 KB)
- 下载次数: 452
- word.rar (6.8 KB)
- 下载次数: 288
评论
5 楼
zihua
2012-09-17
WordOperator 是你自己写的类吗?
4 楼
aiwwwu
2012-08-23
博主,你这StringUtils也没有,程序里面也没有说明一下你到底做了哪些功能的示范啊,有点点马虎了~
3 楼
venusf
2012-03-16
import com.bperp.word.util.StringUtils
楼主这个包在哪里导入啊?
在程序里这个地方会报错,楼主能否把这个文件发给我参考一下? 这是我的邮箱 venusf@163.com 万分感激啊^^
楼主这个包在哪里导入啊?
在程序里这个地方会报错,楼主能否把这个文件发给我参考一下? 这是我的邮箱 venusf@163.com 万分感激啊^^
2 楼
xushunwang
2011-01-11
为什么你的代码中出现的 WordOperator 这个类找不到
1 楼
wangyj0898
2010-05-30
楼主写的东西都是很实用的,学习了~!
发表评论
-
java 解析数学表达式
2011-11-20 10:33 5532数学表达式解析工具类: 支持负数运算, 多层括号嵌套运算 ... -
java代码模拟计算机实现原理(一)加法器模拟
2011-09-29 23:30 3258加法器是有两种基本逻辑门电路:异或门、与门电路组成的 ... -
jar 命令简介
2011-02-16 09:55 1178jar 命令用途:采用zip和ZLIB的压缩格式,将一系列程序 ... -
javac 命令
2011-02-16 09:17 1308最简单的方式(javac -d)首先,需要编译JAVA文件,需 ... -
阿拉伯数组转换成对应中文
2011-02-12 11:52 1117package com.heda.code; publi ... -
java二叉树实现
2011-02-12 11:50 1034package com.heda.code; publi ... -
最大公约数和最小公倍数
2011-02-12 11:48 997package com.heda.code; publi ... -
比较完整数据源实现
2010-06-12 21:35 1437package com.bpsoft.bl ... -
jxl 操作excel Demo
2010-05-25 16:02 1313程序是用:jxl技术来操作excel文档的,实现了excel插 ... -
Java 正则表达式小结
2010-05-11 20:25 1467Java 中正则表达式是在Java 1.4版本以后才出现的。在 ... -
DOM写的xml工具类
2010-04-05 16:15 1367package xml_util; import j ... -
日志记录器
2010-03-12 23:12 959package log_util; import jav ... -
java1.5新特性枚举实现原理
2010-03-09 00:24 1116package com.bp_soft.day01; /** ... -
Struts 1实现原理之我理解
2010-03-06 23:23 1465Struts是在servlet基础上设计的一种框架。下面看一段 ... -
JDBC模板类实现(version1.1)
2010-01-30 14:34 1344package daoUtil.daoTemplate; ... -
BeanUtils.copyProperties(son, father)使用感想
2010-01-28 12:20 3154BeanUtils.copyProperties(son, f ... -
jdbc数据源
2010-01-20 19:06 1052数据源设计思想和数据库连接池设计思想基本相似。大家知道在操作数 ... -
经典JDBC DAOFactory类实现
2010-01-09 23:02 2407package ajax.user.language.fact ... -
WEB应用中常用的数据库连接池
2009-12-26 16:07 1348数据库连接池是WEB应用中必不可少的东西,这是我个人最近参照别 ... -
非常好获得数据库连接工具类
2009-12-26 15:58 1775这是一个写的非常好的获得数据库连接工具类,里面提供常用数据库驱 ...
相关推荐
jacob操作WORD详细 教程和JAR包文件
1、把jacob.dll文件,复制到 windows\system32 目录下。(注:我用的时候这个方法不能运行) 2、 把jacob.dll放入 Java\jdk1.5.0_06\jre\bin目录下.把jacob.jar放入 Java\jdk1.5.0_0\jre\lib\ext 目录下.可以正常...
使用jacob操作word文档的部分方法希望对你们能有所帮助
jacob操作word实例,包括文本、表格的读写操作
jacob操作word资料
jacob操作word文档
java操作word 超简单实用:第一步: 引入包jacob.jar 第二步:参考工具类MsWordTool.java 含概基本WORD 所有操作方法 【特别难点】解决实例: 1)word书签内容替换后,书签会丢失问题 2)word定点批量插入图片...
该word中是一些关于jacob如何操作word的范例代码!互相交流学习
如果你要对 MS Word、Excel 进行处理,JACOB 是一个好的选择。JACOB目前已经成为sourceforge(http://sourceforge.net/projects/jacob- project/)的一个开源项目,本文使用的版本是1.10.1。 这篇文章可能不能完全...
jacob操作word
运用jacob将word文件转成pdf文件,word文件支持xml格式,首先将文档中的dll放到jdk的bin文件夹下,运行java文件即可
任务目的 1自动生成word文档目录。 用例测试操作步骤 在一个word文档的第二页填写占位符: {目录}保存。调用程序读取目标文档,自动根据标题生成目录到{目录}位置。
JACOB操作work和excel示例。附包
有关jacob的word操作研究,好东西,值得参考学习!希望对你有帮助!
Jacob操作word 文档的方法集!(含创建目录),使用的Jacob版本是1.5
一次使用jacob在服务器端操作word文档的总结
使用jacob操作word,包括根据摸版生成word,将word转换成html文件,以合并word文件
jacob 操作word完整代码实例,绝对实用
非常实用哦,对于入门者更是只剩的法宝哦。还在等什么,快下载吧。