使用POI读写word docx文件

目录

1     docx文件

1.1     通过XWPFWordExtractor

1.2     通过XWPFDocument

2     docx文件

2.1     直接通过XWPFDocument生成

2.2     docx文件作为模板

 

       POI在读写word docx文件时是通过xwpf模块来进行的,其核心是XWPFDocument。一个XWPFDocument代表一个docx文档,其可以用来读docx文档,也可以用来写docx文档。XWPFDocument中主要包含下面这几种对象:

l  XWPFParagraph:代表一个段落。

l  XWPFRun:代表具有相同属性的一段文本。

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(55);  
  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文件的内容是这样的:


猜你喜欢

转载自blog.csdn.net/wangxintong_1992/article/details/80920843