您的位置:首页 > 其它

统计目录(包含子目录)下所有word文档页码数

2014-12-22 11:10 330 查看
提醒:使用poi3.7类库,下载相关jar包,java工程build path添加jar包引用

package com.smi.wordpage.tool;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class WordPageGetter {

/**
* @param args
*/
public static int sumpage=0;

public static void main(String[] args) {
// TODO Auto-generated method stub
WordPageGetter wpg =new WordPageGetter();
List<String> lstFileNames=wpg.getListFiles("E://验收文档141219","docx",true);
XWPFDocument docx;
try {
for(int i=0;i<lstFileNames.size();i++){
docx = new XWPFDocument(POIXMLDocument.openPackage(lstFileNames.get(i)));
int pages = docx.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();//总页数
//  int wordCount = docx.getProperties().getExtendedProperties().getUnderlyingProperties().getCharacters();// 忽略空格的总字符数 另外还有getCharactersWithSpaces()方法获取带空格的总字数。
System.out.println (lstFileNames.get(i)+"   pages=" + pages );
sumpage+=pages;
docx=null;
}
System.out.println ("目录下文档总页数 = " + sumpage );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

//获取目录(包括子目录)下所有suffix类型文件的完整路径   目录,类型,是否查询子目录
public  List<String> getListFiles(String path, String suffix, boolean isdepth) {
List<String> lstFileNames = new ArrayList<String>();
File file = new File(path);
return listFile(lstFileNames, file, suffix, isdepth);
}

private  List<String> listFile(List<String> lstFileNames, File f, String suffix, boolean isdepth) {
// 若是目录, 采用递归的方法遍历子目录
if (f.isDirectory()) {
File[] t = f.listFiles();

for (int i = 0; i < t.length; i++) {
if (isdepth || t[i].isFile()) {
listFile(lstFileNames, t[i], suffix, isdepth);
}
}
} else {

String filePath = f.getAbsolutePath();
if (!suffix.equals("")) {
int begIndex = filePath.lastIndexOf(".");
String tempsuffix = "";

if (begIndex != -1) {
tempsuffix = filePath.substring(begIndex + 1, filePath.length());
if (tempsuffix.equals(suffix)) {
lstFileNames.add(filePath);
}
}
} else {
lstFileNames.add(filePath);
}
//System.out.println(filePath);
}
return lstFileNames;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐