使用POI讀寫word docx文件【docx總結的不錯】

目錄app

1     讀docx文件this

1.1     經過XWPFWordExtractor讀spa

1.2     經過XWPFDocument讀.net

2     寫docx文件對象

2.1     直接經過XWPFDocument生成blog

2.2     以docx文件做爲模板rem

 

       POI在讀寫word docx文件時是經過xwpf模塊來進行的,其核心是XWPFDocument。一個XWPFDocument表明一個docx文檔,其能夠用來讀docx文檔,也能夠用來寫docx文檔。XWPFDocument中主要包含下面這幾種對象:文檔

l  XWPFParagraph:表明一個段落。字符串

l  XWPFRun:表明具備相同屬性的一段文本。get

l  XWPFTable:表明一個表格。

l  XWPFTableRow:表格的一行。

l  XWPFTableCell:表格對應的一個單元格。

 

1       讀docx文件

       跟讀doc文件同樣,POI在讀docx文件的時候也有兩種方式,經過XWPFWordExtractor和經過XWPFDocument。在XWPFWordExtractor讀取信息時其內部仍是經過XWPFDocument來獲取的。

1.1     經過XWPFWordExtractor讀

       在使用XWPFWordExtractor讀取docx文檔的內容時,咱們只能獲取到其文本,而不能獲取到其文本對應的屬性值。下面是一段使用XWPFWordExtractor來讀取docx文檔內容的示例代碼:

Java代碼  收藏代碼

  1. public class XwpfTest {  
  2.    
  3.    /** 
  4.     * 經過XWPFWordExtractor訪問XWPFDocument的內容 
  5.     * @throws Exception 
  6.     */  
  7.    @Test  
  8.    public void testReadByExtractor() throws Exception {  
  9.       InputStream is = new FileInputStream("D:\\test.docx");  
  10.       XWPFDocument doc = new XWPFDocument(is);  
  11.       XWPFWordExtractor extractor = new XWPFWordExtractor(doc);  
  12.       String text = extractor.getText();  
  13.       System.out.println(text);  
  14.       CoreProperties coreProps = extractor.getCoreProperties();  
  15.       this.printCoreProperties(coreProps);  
  16.       this.close(is);  
  17.    }  
  18.     
  19.    /** 
  20.     * 輸出CoreProperties信息 
  21.     * @param coreProps 
  22.     */  
  23.    private void printCoreProperties(CoreProperties coreProps) {  
  24.       System.out.println(coreProps.getCategory());   //分類  
  25.       System.out.println(coreProps.getCreator()); //建立者  
  26.       System.out.println(coreProps.getCreated()); //建立時間  
  27.       System.out.println(coreProps.getTitle());   //標題  
  28.    }  
  29.     
  30.    /** 
  31.     * 關閉輸入流 
  32.     * @param is 
  33.     */  
  34.    private void close(InputStream is) {  
  35.       if (is != null) {  
  36.          try {  
  37.             is.close();  
  38.          } catch (IOException e) {  
  39.             e.printStackTrace();  
  40.          }  
  41.       }  
  42.    }  
  43.     
  44. }  

 

 

1.2     經過XWPFDocument讀

       在經過XWPFDocument讀取docx文檔時,咱們就能夠獲取到文本比較精確的屬性信息了。好比咱們能夠獲取到某一個XWPFParagraph、XWPFRun或者是某一個XWPFTable,包括它們對應的屬性信息。下面是一個使用XWPFDocument讀取docx文檔的示例:

Java代碼  收藏代碼

  1. public class XwpfTest {  
  2.    
  3.    /** 
  4.     * 經過XWPFDocument對內容進行訪問。對於XWPF文檔而言,用這種方式進行讀操做更佳。 
  5.     * @throws Exception 
  6.     */  
  7.    @Test  
  8.    public void testReadByDoc() throws Exception {  
  9.       InputStream is = new FileInputStream("D:\\table.docx");  
  10.       XWPFDocument doc = new XWPFDocument(is);  
  11.       List<XWPFParagraph> paras = doc.getParagraphs();  
  12.       for (XWPFParagraph para : paras) {  
  13.          //當前段落的屬性  
  14. //       CTPPr pr = para.getCTP().getPPr();  
  15.          System.out.println(para.getText());  
  16.       }  
  17.       //獲取文檔中全部的表格  
  18.       List<XWPFTable> tables = doc.getTables();  
  19.       List<XWPFTableRow> rows;  
  20.       List<XWPFTableCell> cells;  
  21.       for (XWPFTable table : tables) {  
  22.          //表格屬性  
  23. //       CTTblPr pr = table.getCTTbl().getTblPr();  
  24.          //獲取表格對應的行  
  25.          rows = table.getRows();  
  26.          for (XWPFTableRow row : rows) {  
  27.             //獲取行對應的單元格  
  28.             cells = row.getTableCells();  
  29.             for (XWPFTableCell cell : cells) {  
  30.                 System.out.println(cell.getText());;  
  31.             }  
  32.          }  
  33.       }  
  34.       this.close(is);  
  35.    }  
  36.     
  37.    /** 
  38.     * 關閉輸入流 
  39.     * @param is 
  40.     */  
  41.    private void close(InputStream is) {  
  42.       if (is != null) {  
  43.          try {  
  44.             is.close();  
  45.          } catch (IOException e) {  
  46.             e.printStackTrace();  
  47.          }  
  48.       }  
  49.    }  
  50.     
  51. }  

 

 

2       寫docx文件

2.1     直接經過XWPFDocument生成

       在使用XWPFDocument寫docx文件時不須要像使用HWPFDocument寫doc文件那樣必須從一個doc文件開始,咱們能夠直接new一個空的XWPFDocument,以後再往這個XWPFDocument裏面填充內容,而後再把它寫入到對應的輸出流中。下面是使用XWPFDocument生成docx文件的示例代碼:

Java代碼  收藏代碼

  1. public class XwpfTest {  
  2.     
  3.    /** 
  4.     * 基本的寫操做 
  5.     * @throws Exception 
  6.     */  
  7.    @Test  
  8.    public void testSimpleWrite() throws Exception {  
  9.       //新建一個文檔  
  10.       XWPFDocument doc = new XWPFDocument();  
  11.       //建立一個段落  
  12.       XWPFParagraph para = doc.createParagraph();  
  13.        
  14.       //一個XWPFRun表明具備相同屬性的一個區域。  
  15.       XWPFRun run = para.createRun();  
  16.       run.setBold(true); //加粗  
  17.       run.setText("加粗的內容");  
  18.       run = para.createRun();  
  19.       run.setColor("FF0000");  
  20.       run.setText("紅色的字。");  
  21.       OutputStream os = new FileOutputStream("D:\\simpleWrite.docx");  
  22.       //把doc輸出到輸出流  
  23.       doc.write(os);  
  24.       this.close(os);  
  25.    }  
  26.     
  27.    /*** 
  28.     * 寫一個表格 
  29.     * @throws Exception 
  30.     */  
  31.    @Test  
  32.    public void testWriteTable() throws Exception {  
  33.       XWPFDocument doc = new XWPFDocument();  
  34.       //建立一個5行5列的表格  
  35.       XWPFTable table = doc.createTable(5, 5);  
  36.       //這裏增長的列本來初始化建立的那5行在經過getTableCells()方法獲取時獲取不到,但經過row新增的就能夠。  
  37. //    table.addNewCol(); //給表格增長一列,變成6列  
  38.       table.createRow(); //給表格新增一行,變成6行  
  39.       List<XWPFTableRow> rows = table.getRows();  
  40.       //表格屬性  
  41.       CTTblPr tablePr = table.getCTTbl().addNewTblPr();  
  42.       //表格寬度  
  43.       CTTblWidth width = tablePr.addNewTblW();  
  44.       width.setW(BigInteger.valueOf(8000));  
  45.       XWPFTableRow row;  
  46.       List<XWPFTableCell> cells;  
  47.       XWPFTableCell cell;  
  48.       int rowSize = rows.size();  
  49.       int cellSize;  
  50.       for (int i=0; i<rowSize; i++) {  
  51.          row = rows.get(i);  
  52.          //新增單元格  
  53.          row.addNewTableCell();  
  54.          //設置行的高度  
  55.          row.setHeight(500);  
  56.          //行屬性  
  57. //       CTTrPr rowPr = row.getCtRow().addNewTrPr();  
  58.          //這種方式是能夠獲取到新增的cell的。  
  59. //       List<CTTc> list = row.getCtRow().getTcList();  
  60.          cells = row.getTableCells();  
  61.          cellSize = cells.size();  
  62.          for (int j=0; j<cellSize; j++) {  
  63.             cell = cells.get(j);  
  64.             if ((i+j)%2==0) {  
  65.                 //設置單元格的顏色  
  66.                 cell.setColor("ff0000"); //紅色  
  67.             } else {  
  68.                 cell.setColor("0000ff"); //藍色  
  69.             }  
  70.             //單元格屬性  
  71.             CTTcPr cellPr = cell.getCTTc().addNewTcPr();  
  72.             cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);  
  73.             if (j == 3) {  
  74.                 //設置寬度  
  75.                 cellPr.addNewTcW().setW(BigInteger.valueOf(3000));  
  76.             }  
  77.             cell.setText(i + ", " + j);  
  78.          }  
  79.       }  
  80.       //文件不存在時會自動建立  
  81.       OutputStream os = new FileOutputStream("D:\\table.docx");  
  82.       //寫入文件  
  83.       doc.write(os);  
  84.       this.close(os);  
  85.    }  
  86.     
  87.    /** 
  88.     * 關閉輸出流 
  89.     * @param os 
  90.     */  
  91.    private void close(OutputStream os) {  
  92.       if (os != null) {  
  93.          try {  
  94.             os.close();  
  95.          } catch (IOException e) {  
  96.             e.printStackTrace();  
  97.          }  
  98.       }  
  99.    }  
  100.     
  101. }  

 

 

2.2     以docx文件做爲模板

       固然,咱們也能夠像寫doc文件那樣,先以一個docx文件做爲模板,而後創建基於該docx文件的XWPFDocument對象,再把裏面一些變化的信息在運行時進行替換,以後將XWPFDocument進行輸出就能夠了。所不一樣的是XWPFDocument中沒有像HWPFDocument中那樣的Range能夠用來直接替換內容。並且底層的XWPFParagraph和XWPFRun也不支持直接將文本進行替換。卻是XWPFRun提供了一個設置文本的方法,不過新的文本不會替換舊的文本,而是會追加到原來的文本以後。如今的一個作法是先找出含有須要替換的變量的XWPFRun,而後將其移除,以後在原來的位置新增一個XWPFRun,其對應的文本是替換變量以後的文本。不過你設置的那個的變量的位置不必定就在一個XWPFRun裏面,它有可能會被拆分到兩個甚至更多的XWPFRun中,因此不是頗有必要的話仍是不推薦使用這種方式。

       假設咱們有一個docx文件,其內容是這樣的:



 

 

       以後咱們以該文件做爲模板,利用相關數據把裏面的變量進行替換,而後把替換後的文檔輸出到另外一個docx文件中。具體作法以下:

Java代碼  收藏代碼

  1.    
  2. public class XwpfTest {  
  3.    
  4.    /** 
  5.     * 用一個docx文檔做爲模板,而後替換其中的內容,再寫入目標文檔中。 
  6.     * @throws Exception 
  7.     */  
  8.    @Test  
  9.    public void testTemplateWrite() throws Exception {  
  10.       Map<String, Object> params = new HashMap<String, Object>();  
  11.       params.put("reportDate", "2014-02-28");  
  12.       params.put("appleAmt", "100.00");  
  13.       params.put("bananaAmt", "200.00");  
  14.       params.put("totalAmt", "300.00");  
  15.       String filePath = "D:\\word\\template.docx";  
  16.       InputStream is = new FileInputStream(filePath);  
  17.       XWPFDocument doc = new XWPFDocument(is);  
  18.       //替換段落裏面的變量  
  19.       this.replaceInPara(doc, params);  
  20.       //替換表格裏面的變量  
  21.       this.replaceInTable(doc, params);  
  22.       OutputStream os = new FileOutputStream("D:\\word\\write.docx");  
  23.       doc.write(os);  
  24.       this.close(os);  
  25.       this.close(is);  
  26.    }  
  27.     
  28.    /** 
  29.     * 替換段落裏面的變量 
  30.     * @param doc 要替換的文檔 
  31.     * @param params 參數 
  32.     */  
  33.    private void replaceInPara(XWPFDocument doc, Map<String, Object> params) {  
  34.       Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();  
  35.       XWPFParagraph para;  
  36.       while (iterator.hasNext()) {  
  37.          para = iterator.next();  
  38.          this.replaceInPara(para, params);  
  39.       }  
  40.    }  
  41.     
  42.    /** 
  43.     * 替換段落裏面的變量 
  44.     * @param para 要替換的段落 
  45.     * @param params 參數 
  46.     */  
  47.    private void replaceInPara(XWPFParagraph para, Map<String, Object> params) {  
  48.       List<XWPFRun> runs;  
  49.       Matcher matcher;  
  50.       if (this.matcher(para.getParagraphText()).find()) {  
  51.          runs = para.getRuns();  
  52.          for (int i=0; i<runs.size(); i++) {  
  53.             XWPFRun run = runs.get(i);  
  54.             String runText = run.toString();  
  55.             matcher = this.matcher(runText);  
  56.             if (matcher.find()) {  
  57.                 while ((matcher = this.matcher(runText)).find()) {  
  58.                    runText = matcher.replaceFirst(String.valueOf(params.get(matcher.group(1))));  
  59.                 }  
  60.                 //直接調用XWPFRun的setText()方法設置文本時,在底層會從新建立一個XWPFRun,把文本附加在當前文本後面,  
  61.                 //因此咱們不能直接設值,須要先刪除當前run,而後再本身手動插入一個新的run。  
  62.                 para.removeRun(i);  
  63.                 para.insertNewRun(i).setText(runText);  
  64.             }  
  65.          }  
  66.       }  
  67.    }  
  68.     
  69.    /** 
  70.     * 替換表格裏面的變量 
  71.     * @param doc 要替換的文檔 
  72.     * @param params 參數 
  73.     */  
  74.    private void replaceInTable(XWPFDocument doc, Map<String, Object> params) {  
  75.       Iterator<XWPFTable> iterator = doc.getTablesIterator();  
  76.       XWPFTable table;  
  77.       List<XWPFTableRow> rows;  
  78.       List<XWPFTableCell> cells;  
  79.       List<XWPFParagraph> paras;  
  80.       while (iterator.hasNext()) {  
  81.          table = iterator.next();  
  82.          rows = table.getRows();  
  83.          for (XWPFTableRow row : rows) {  
  84.             cells = row.getTableCells();  
  85.             for (XWPFTableCell cell : cells) {  
  86.                 paras = cell.getParagraphs();  
  87.                 for (XWPFParagraph para : paras) {  
  88.                    this.replaceInPara(para, params);  
  89.                 }  
  90.             }  
  91.          }  
  92.       }  
  93.    }  
  94.     
  95.    /** 
  96.     * 正則匹配字符串 
  97.     * @param str 
  98.     * @return 
  99.     */  
  100.    private Matcher matcher(String str) {  
  101.       Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);  
  102.       Matcher matcher = pattern.matcher(str);  
  103.       return matcher;  
  104.    }  
  105.     
  106.    /** 
  107.     * 關閉輸入流 
  108.     * @param is 
  109.     */  
  110.    private void close(InputStream is) {  
  111.       if (is != null) {  
  112.          try {  
  113.             is.close();  
  114.          } catch (IOException e) {  
  115.             e.printStackTrace();  
  116.          }  
  117.       }  
  118.    }  
  119.     
  120.    /** 
  121.     * 關閉輸出流 
  122.     * @param os 
  123.     */  
  124.    private void close(OutputStream os) {  
  125.       if (os != null) {  
  126.          try {  
  127.             os.close();  
  128.          } catch (IOException e) {  
  129.             e.printStackTrace();  
  130.          }  
  131.       }  
  132.    }  
  133.     
  134. }  

 

 

       通過上面的代碼所示的過程處理後,咱們替換變量後新輸出來的docx文件的內容是這樣的:


相關文章
相關標籤/搜索