import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.xwpf.usermodel.XWPFComment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class WorldUtil {
/**
* @see 返回文件批注
* @param file
* @return 返回参数 [类型,作者,内容]
* @throws IOException
*/
public static List<String[]> getAnnotation(File file) throws IOException {
List<String[]> list = new ArrayList<String[]>();
XWPFDocument docx = null;
try {
String filename = file.getName();
if("docx".equals(filename.substring(filename.lastIndexOf(".")+1))) {
docx = new XWPFDocument(POIXMLDocument.openPackage(
file.getCanonicalPath()));
XWPFComment[] comments = docx.getComments();
for(XWPFComment comment :comments) {
String[] data = new String[3];
data[0] = comment.getText().substring(0, 1);
data[1] = comment.getAuthor();
data[2] = comment.getText();
list.add(data);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(docx!=null) {
docx.close();
}
}
return list;
}
/**
* @see 统计指定文件中批注类型的个数
* @param file
* @return
* @throws Exception
*/
public static Map<String,Integer> countByType(File file) throws Exception{
Map<String,Integer> map = new HashMap<String,Integer>();
XWPFDocument docx = null;
try {
String filename = file.getName();
if("docx".equals(filename.substring(filename.lastIndexOf(".")+1))) {
docx = new XWPFDocument(POIXMLDocument.openPackage(
file.getCanonicalPath()));
XWPFComment[] comments = docx.getComments();
for(XWPFComment cm : comments) {
String key = cm.getText().substring(0, 1);
if(map.get(key)==null) {
map.put(key, 1);
}else {
map.put(key, map.get(key)+1);
}
}
}
} catch (IOException e) {
// 文档格式不正确
e.printStackTrace();
}finally {
if(docx!=null) {
docx.close();
}
}
return map;
}
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\iuit\\Desktop\\test\\111.docx");
// List<String[]> list = getAnnotation(file);
// for(String[] an : list) {
// System.out.println(an[0]+an[1]+an[2]);
// }
}
}