JACOB 就是 JAVA-COM Bridge的缩写,提供自动化的访问com的功能,也是通过JNI功能访问windows平台下的com组件或者win32系统库的。
/**
* 将jacob.dll放入JDK的bin目录下
* 把jacob.jar放入项目的WEB-INF\lib目录下
*/
public class JacobHelper {
private ActiveXComponent word;
private Dispatch documents;
private Dispatch doc;
private Dispatch selection;
private ActiveXComponent excel;
private Dispatch workbooks;
private Dispatch workbook;
private void wordInit(){
if(word==null){
//创建一个word对象
word = new ActiveXComponent("Word.Application");
word.setProperty("Visible", new Variant(false)); //word不可见
word.setProperty("AutomationSecurity", new Variant(3)); //禁用宏
}
if(documents==null){
//获取文挡属性
documents = word.getProperty("Documents").toDispatch();
}
}
private void wordDestory(){
try{
if(doc!=null){
//Dispatch.call(doc, "Close", new Variant(true));
Dispatch.invoke(doc, "Close", Dispatch.Method, new Object[]{new Variant(true)}, new int[1]);
doc = null;
}
if(word!=null){
word.invoke("Quit", new Variant[]{});
word.safeRelease();
word = null;
documents = null;
}
}catch(Exception ex){
ex.printStackTrace();
}
}
/**
* 合并Word文档
*/
public boolean wordUnite(String[] filePath, String outputPath){
if(filePath==null || filePath.length==0) return false;
boolean successful = false;
try{
wordInit();
//添加一个新文挡
doc = Dispatch.call(documents, "Add").toDispatch();
selection = word.getProperty("Selection").toDispatch();
for(int i=0;i<filePath.length;i++){
Dispatch.call(selection, "insertFile", filePath[i]);
if(i<filePath.length-1){
Dispatch.call(selection, "TypeParagraph"); //换行
}
}
//文件另存为
Dispatch.call(doc, "SaveAs", outputPath);
successful = true;
}catch(Exception ex){
successful = false;
ex.printStackTrace();
}finally{
wordDestory();
}
return successful;
}
/**
* Word转Html
*/
public boolean word2html(String wordFilePath, String htmlFilePath){
boolean successful = false;
try{
wordInit();
//对象数组第三个表示是否以只读方式打开文件
doc = Dispatch.invoke(documents, "Open", Dispatch.Method,
new Object[]{wordFilePath, new Variant(false), new Variant(true)}, new int[1]).toDispatch();
//另存为
Dispatch.invoke(doc, "SaveAs", Dispatch.Method,
new Object[]{htmlFilePath, new Variant(8)}, new int[1]); //html-new Variant(8) txt-new Variant(2)
successful = true;
}catch(Exception ex){
successful = false;
ex.printStackTrace();
}finally{
wordDestory();
}
return successful;
}
private void excelInit(){
if(excel==null){
ComThread.InitSTA();
excel = new ActiveXComponent("Excel.Application");
excel.setProperty("Visible", new Variant(false));
}
}
private void excelDestory(){
try{
if(workbook!=null){
Dispatch.call(workbook, "Close", new Variant(true));
workbook = null;
}
if(excel!=null){
excel.invoke("Quit", new Variant[]{});
ComThread.Release();
excel = null;
}
}catch(Exception ex){
ex.printStackTrace();
}
}
public boolean excel2html(String excelFilePath, String htmlFilePath){
boolean successful = false;
try{
excelInit();
//先删除旧文件
File file = new File(htmlFilePath);
if(file.exists() && file.canRead()){
file.delete();
}
workbooks = excel.getProperty("Workbooks").toDispatch();
workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method,
new Object[]{excelFilePath, new Variant(false), new Variant(true)}, new int[1]).toDispatch(); //以只读方式打开
Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[]{htmlFilePath, new Variant(44)}, new int[1]);
successful = true;
}catch(Exception ex){
successful = false;
ex.printStackTrace();
}finally{
excelDestory();
}
return successful;
}
}
//设置字体
Dispatch font = Dispatch.get(selection, "Font").toDispatch();
Dispatch.put(font, "Size", "10");
Dispatch.put(font, "Name", new Variant("宋体"));
//设置段落格式
Dispatch align = Dispatch.get(selection, "ParagraphFormat").toDispatch();
Dispatch.put(align, "Alignment", "1"); //1:置中 2:靠右 3:靠左
//插入文本字符串
Dispatch.put(selection, "Text", "经理例会重点问题(" + DatetimeUtil.getToday("MMdd") + ")");
//回车换行
Dispatch.call(selection, "MoveDown");
Dispatch.call(selection, "TypeParagraph");
//重新设置文档内容的字体
selection = Dispatch.get(doc, "Content").toDispatch();
font = Dispatch.get(selection, "Font").toDispatch();
Dispatch.put(font, "Size", "10");
Dispatch.put(font, "Name", new Variant("宋体"));
Dispatch.put(font, "Bold", new Variant(false));