jacob测试

本文介绍了一种使用Java和Jacob组件将多个DOC文件批量转换为HTML的方法。该方法通过递归遍历指定目录下的所有DOC文件,并利用Microsoft Word应用程序将其转换为HTML格式,同时保持原有的文件名和存储位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class JacobTest {
//  ------------------------------------------------------------------------------ 
//  方法原型:  change(String  paths) 
//  功能描述:  将指定目录下面所有的doc文件转化为HTML并存储在相同目录下 
//  输入参数:  String 
//  输出参数:  无 
//  返  回  值:  无 
//  其它说明:  递归 
//  ------------------------------------------------------------------------------ 
     public  static  void  change(String  paths,  String  savepaths)  { 
  
         File  d  =  new  File(paths); 
//  取得当前文件夹下所有文件和目录的列表 
         File  lists[]  =  d.listFiles(); 
         String  pathss  =  new  String(""); 
  
//  对当前目录下面所有文件进行检索 
         for  (int  i  =  0;  i  <  lists.length;  i++)  { 
             if  (lists[i].isFile())  { 
                 String  filename  =  lists[i].getName(); 
                 String  filetype  =  new  String(""); 
//  取得文件类型 
                 filetype  =  filename.substring(  (filename.length()  -  3),  filename.length()); 
  
//  判断是否为doc文件 
                 if  (filetype.equals("doc"))  { 
                     System.out.println("当前正在转换......"); 
//  打印当前目录路径 
                     System.out.println(paths); 
//  打印doc文件名 
                     System.out.println(filename.substring(0,  (filename.length()  -  4))); 
  
                     ActiveXComponent  app  =  new  ActiveXComponent("Word.Application");  //启动word 
  
                     String  docpath  =  paths  +  filename; 
                     String  htmlpath  =  savepaths  + 
                             filename.substring(0,  (filename.length()  -  4)); 
  
                     String  inFile  =  docpath; 
//  要转换的word文件 
                     String  tpFile  =  htmlpath; 
//  HTML文件 
  
                     boolean  flag  =  false; 
  
                     try  { 
                         app.setProperty("Visible",  new  Variant(false)); 
//  设置word不可见 
                         Object  docs  =  app.getProperty("Documents").toDispatch(); 
                         Object  doc  =  Dispatch.invoke((Dispatch) docs,  "Open",  Dispatch.Method, 
                                                                                   new  Object[]  {inFile,  new  Variant(false), 
                                                                                   new  Variant(true)} 
                                                                                   ,  new  int[1]).toDispatch(); 
//  打开word文件 
                         Dispatch.invoke((Dispatch) doc,  "SaveAs",  Dispatch.Method, 
                                                         new  Object[]  {tpFile,  new  Variant(8)} 
                                                         ,  new  int[1]); 
//  作为html格式保存到临时文件 
                         Variant  f  =  new  Variant(false); 
                         Dispatch.call((Dispatch) doc,  "Close",  f); 
                         flag  =  true; 
                     } 
                     catch  (Exception  e)  { 
                         e.printStackTrace(); 
                     } 
                     finally  { 
                         app.invoke("Quit",  new  Variant[]  {}); 
                     } 
                     System.out.println("转化完毕!"); 
                 } 
             } 
             else  { 
                 pathss  =  paths; 
//  进入下一级目录 
                 pathss  =  pathss  +  lists[i].getName()  +  "//"; 
//  递归遍历所有目录 
                 change(pathss,  savepaths); 
             } 
         } 
  
     } 
  
//  ------------------------------------------------------------------------------ 
//  方法原型:  main(String[]  args) 
//  功能描述:  main文件 
//  输入参数:  无 
//  输出参数:  无 
//  返  回  值:  无 
//  其它说明:  无 
//  ------------------------------------------------------------------------------ 
     public  static  void  main(String[]  args)  { 
  
         String  paths  =  new  String("E:/a/"); 
         String  savepaths  =  new  String("E:/a/"); 
  
         change(paths,  savepaths); 
  
     } 
  }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值