统计一个文件夹下所有word的总页数

能够统计在一个文件夹在的所有word的总页数。

代码如下:

 

package com.chedb.controller;

import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class Test1 {
    public static void main(String[] args) throws Exception {
        /*String dateStr = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
        System.out.println(dateStr);*/
        
        String filepath = "E:\\sy\\51";//D盘下的file文件夹的目录
        File file = new File(filepath);//File类型可以是文件也可以是文件夹
        File[] fileList = file.listFiles();//将该目录下的所有文件放置在一个File类型的数组中
        String[] str={};
        int total = 0;
        for(int i = 0;i<fileList.length;i++){
            //str[i] = fileList.
            //System.out.println(fileList[i]);
            total = total + xsys(fileList[i]);
            System.out.println(fileList[i]);
            System.out.println("总页数为"+total+"页!!!!");
        }
    }
    
    static int xsys(File fileList){
        //String filePath = "E:\\sy\\51\\word名称.doc";// word文档所在路径
        String filePath = fileList+"";
        // 建立ActiveX部件
        ActiveXComponent wordCom = new ActiveXComponent("Word.Application");
        //word应用程序不可见
        wordCom.setProperty("Visible", false);
        // 返回wrdCom.Documents的Dispatch
        Dispatch wrdDocs = wordCom.getProperty("Documents").toDispatch();//Documents表示word的所有文档窗口(word是多文档应用程序)
        // 调用wrdCom.Documents.Open方法打开指定的word文档,返回wordDoc
        Dispatch wordDoc = Dispatch.call(wrdDocs, "Open", filePath, false, true, false).toDispatch();
        Dispatch selection = Dispatch.get(wordCom, "Selection").toDispatch();
        int pages = Integer.parseInt(Dispatch.call(selection,"information",4).toString());//总页数 //显示修订内容的最终状态
        System.out.println(pages);
      //关闭文档且不保存
        Dispatch.call(wordDoc, "Close", new Variant(false));
        //退出进程对象
        wordCom.invoke("Quit", new Variant[] {});
        return pages;
    }
}

其中需要   (1)、jacob-1.14.3.jar这个jar包,下载地址为:链接:https://pan.baidu.com/s/1VsZ2iAuQPZzy9x6RXbRy0g    提取码:xjfx 

                 (2)、jacob-1.14.3.dll这个dll文件,下载地址为:链接:https://pan.baidu.com/s/1VsZ2iAuQPZzy9x6RXbRy0g    提取码:xjfx   放置在c盘下的windows下的system32下

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值