jacob如何操作word

首先,大家先要了解一下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 文件。

Java代码
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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值