POI 讀取文檔標註

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]);
//         }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章