目錄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代碼 ![收藏代碼](http://static.javashuo.com/static/loading.gif)
- public class XwpfTest {
-
- /**
- * 經過XWPFWordExtractor訪問XWPFDocument的內容
- * @throws Exception
- */
- @Test
- public void testReadByExtractor() throws Exception {
- InputStream is = new FileInputStream("D:\\test.docx");
- XWPFDocument doc = new XWPFDocument(is);
- XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
- String text = extractor.getText();
- System.out.println(text);
- CoreProperties coreProps = extractor.getCoreProperties();
- this.printCoreProperties(coreProps);
- this.close(is);
- }
-
- /**
- * 輸出CoreProperties信息
- * @param coreProps
- */
- private void printCoreProperties(CoreProperties coreProps) {
- System.out.println(coreProps.getCategory()); //分類
- System.out.println(coreProps.getCreator()); //建立者
- System.out.println(coreProps.getCreated()); //建立時間
- System.out.println(coreProps.getTitle()); //標題
- }
-
- /**
- * 關閉輸入流
- * @param is
- */
- private void close(InputStream is) {
- if (is != null) {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- }
1.2 經過XWPFDocument讀
在經過XWPFDocument讀取docx文檔時,咱們就能夠獲取到文本比較精確的屬性信息了。好比咱們能夠獲取到某一個XWPFParagraph、XWPFRun或者是某一個XWPFTable,包括它們對應的屬性信息。下面是一個使用XWPFDocument讀取docx文檔的示例:
Java代碼 ![收藏代碼](http://static.javashuo.com/static/loading.gif)
- public class XwpfTest {
-
- /**
- * 經過XWPFDocument對內容進行訪問。對於XWPF文檔而言,用這種方式進行讀操做更佳。
- * @throws Exception
- */
- @Test
- public void testReadByDoc() throws Exception {
- InputStream is = new FileInputStream("D:\\table.docx");
- XWPFDocument doc = new XWPFDocument(is);
- List<XWPFParagraph> paras = doc.getParagraphs();
- for (XWPFParagraph para : paras) {
- //當前段落的屬性
- // CTPPr pr = para.getCTP().getPPr();
- System.out.println(para.getText());
- }
- //獲取文檔中全部的表格
- List<XWPFTable> tables = doc.getTables();
- List<XWPFTableRow> rows;
- List<XWPFTableCell> cells;
- for (XWPFTable table : tables) {
- //表格屬性
- // CTTblPr pr = table.getCTTbl().getTblPr();
- //獲取表格對應的行
- rows = table.getRows();
- for (XWPFTableRow row : rows) {
- //獲取行對應的單元格
- cells = row.getTableCells();
- for (XWPFTableCell cell : cells) {
- System.out.println(cell.getText());;
- }
- }
- }
- this.close(is);
- }
-
- /**
- * 關閉輸入流
- * @param is
- */
- private void close(InputStream is) {
- if (is != null) {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- }
2 寫docx文件
2.1 直接經過XWPFDocument生成
在使用XWPFDocument寫docx文件時不須要像使用HWPFDocument寫doc文件那樣必須從一個doc文件開始,咱們能夠直接new一個空的XWPFDocument,以後再往這個XWPFDocument裏面填充內容,而後再把它寫入到對應的輸出流中。下面是使用XWPFDocument生成docx文件的示例代碼:
Java代碼 ![收藏代碼](http://static.javashuo.com/static/loading.gif)
- public class XwpfTest {
-
- /**
- * 基本的寫操做
- * @throws Exception
- */
- @Test
- public void testSimpleWrite() throws Exception {
- //新建一個文檔
- XWPFDocument doc = new XWPFDocument();
- //建立一個段落
- XWPFParagraph para = doc.createParagraph();
-
- //一個XWPFRun表明具備相同屬性的一個區域。
- XWPFRun run = para.createRun();
- run.setBold(true); //加粗
- run.setText("加粗的內容");
- run = para.createRun();
- run.setColor("FF0000");
- run.setText("紅色的字。");
- OutputStream os = new FileOutputStream("D:\\simpleWrite.docx");
- //把doc輸出到輸出流
- doc.write(os);
- this.close(os);
- }
-
- /***
- * 寫一個表格
- * @throws Exception
- */
- @Test
- public void testWriteTable() throws Exception {
- XWPFDocument doc = new XWPFDocument();
- //建立一個5行5列的表格
- XWPFTable table = doc.createTable(5, 5);
- //這裏增長的列本來初始化建立的那5行在經過getTableCells()方法獲取時獲取不到,但經過row新增的就能夠。
- // table.addNewCol(); //給表格增長一列,變成6列
- table.createRow(); //給表格新增一行,變成6行
- List<XWPFTableRow> rows = table.getRows();
- //表格屬性
- CTTblPr tablePr = table.getCTTbl().addNewTblPr();
- //表格寬度
- CTTblWidth width = tablePr.addNewTblW();
- width.setW(BigInteger.valueOf(8000));
- XWPFTableRow row;
- List<XWPFTableCell> cells;
- XWPFTableCell cell;
- int rowSize = rows.size();
- int cellSize;
- for (int i=0; i<rowSize; i++) {
- row = rows.get(i);
- //新增單元格
- row.addNewTableCell();
- //設置行的高度
- row.setHeight(500);
- //行屬性
- // CTTrPr rowPr = row.getCtRow().addNewTrPr();
- //這種方式是能夠獲取到新增的cell的。
- // List<CTTc> list = row.getCtRow().getTcList();
- cells = row.getTableCells();
- cellSize = cells.size();
- for (int j=0; j<cellSize; j++) {
- cell = cells.get(j);
- if ((i+j)%2==0) {
- //設置單元格的顏色
- cell.setColor("ff0000"); //紅色
- } else {
- cell.setColor("0000ff"); //藍色
- }
- //單元格屬性
- CTTcPr cellPr = cell.getCTTc().addNewTcPr();
- cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);
- if (j == 3) {
- //設置寬度
- cellPr.addNewTcW().setW(BigInteger.valueOf(3000));
- }
- cell.setText(i + ", " + j);
- }
- }
- //文件不存在時會自動建立
- OutputStream os = new FileOutputStream("D:\\table.docx");
- //寫入文件
- doc.write(os);
- this.close(os);
- }
-
- /**
- * 關閉輸出流
- * @param os
- */
- private void close(OutputStream os) {
- if (os != null) {
- try {
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- }
2.2 以docx文件做爲模板
固然,咱們也能夠像寫doc文件那樣,先以一個docx文件做爲模板,而後創建基於該docx文件的XWPFDocument對象,再把裏面一些變化的信息在運行時進行替換,以後將XWPFDocument進行輸出就能夠了。所不一樣的是XWPFDocument中沒有像HWPFDocument中那樣的Range能夠用來直接替換內容。並且底層的XWPFParagraph和XWPFRun也不支持直接將文本進行替換。卻是XWPFRun提供了一個設置文本的方法,不過新的文本不會替換舊的文本,而是會追加到原來的文本以後。如今的一個作法是先找出含有須要替換的變量的XWPFRun,而後將其移除,以後在原來的位置新增一個XWPFRun,其對應的文本是替換變量以後的文本。不過你設置的那個的變量的位置不必定就在一個XWPFRun裏面,它有可能會被拆分到兩個甚至更多的XWPFRun中,因此不是頗有必要的話仍是不推薦使用這種方式。
假設咱們有一個docx文件,其內容是這樣的:
![](http://static.javashuo.com/static/loading.gif)
以後咱們以該文件做爲模板,利用相關數據把裏面的變量進行替換,而後把替換後的文檔輸出到另外一個docx文件中。具體作法以下:
Java代碼 ![收藏代碼](http://static.javashuo.com/static/loading.gif)
-
- public class XwpfTest {
-
- /**
- * 用一個docx文檔做爲模板,而後替換其中的內容,再寫入目標文檔中。
- * @throws Exception
- */
- @Test
- public void testTemplateWrite() throws Exception {
- Map<String, Object> params = new HashMap<String, Object>();
- params.put("reportDate", "2014-02-28");
- params.put("appleAmt", "100.00");
- params.put("bananaAmt", "200.00");
- params.put("totalAmt", "300.00");
- String filePath = "D:\\word\\template.docx";
- InputStream is = new FileInputStream(filePath);
- XWPFDocument doc = new XWPFDocument(is);
- //替換段落裏面的變量
- this.replaceInPara(doc, params);
- //替換表格裏面的變量
- this.replaceInTable(doc, params);
- OutputStream os = new FileOutputStream("D:\\word\\write.docx");
- doc.write(os);
- this.close(os);
- this.close(is);
- }
-
- /**
- * 替換段落裏面的變量
- * @param doc 要替換的文檔
- * @param params 參數
- */
- private void replaceInPara(XWPFDocument doc, Map<String, Object> params) {
- Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
- XWPFParagraph para;
- while (iterator.hasNext()) {
- para = iterator.next();
- this.replaceInPara(para, params);
- }
- }
-
- /**
- * 替換段落裏面的變量
- * @param para 要替換的段落
- * @param params 參數
- */
- private void replaceInPara(XWPFParagraph para, Map<String, Object> params) {
- List<XWPFRun> runs;
- Matcher matcher;
- if (this.matcher(para.getParagraphText()).find()) {
- runs = para.getRuns();
- for (int i=0; i<runs.size(); i++) {
- XWPFRun run = runs.get(i);
- String runText = run.toString();
- matcher = this.matcher(runText);
- if (matcher.find()) {
- while ((matcher = this.matcher(runText)).find()) {
- runText = matcher.replaceFirst(String.valueOf(params.get(matcher.group(1))));
- }
- //直接調用XWPFRun的setText()方法設置文本時,在底層會從新建立一個XWPFRun,把文本附加在當前文本後面,
- //因此咱們不能直接設值,須要先刪除當前run,而後再本身手動插入一個新的run。
- para.removeRun(i);
- para.insertNewRun(i).setText(runText);
- }
- }
- }
- }
-
- /**
- * 替換表格裏面的變量
- * @param doc 要替換的文檔
- * @param params 參數
- */
- private void replaceInTable(XWPFDocument doc, Map<String, Object> params) {
- Iterator<XWPFTable> iterator = doc.getTablesIterator();
- XWPFTable table;
- List<XWPFTableRow> rows;
- List<XWPFTableCell> cells;
- List<XWPFParagraph> paras;
- while (iterator.hasNext()) {
- table = iterator.next();
- rows = table.getRows();
- for (XWPFTableRow row : rows) {
- cells = row.getTableCells();
- for (XWPFTableCell cell : cells) {
- paras = cell.getParagraphs();
- for (XWPFParagraph para : paras) {
- this.replaceInPara(para, params);
- }
- }
- }
- }
- }
-
- /**
- * 正則匹配字符串
- * @param str
- * @return
- */
- private Matcher matcher(String str) {
- Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
- Matcher matcher = pattern.matcher(str);
- return matcher;
- }
-
- /**
- * 關閉輸入流
- * @param is
- */
- private void close(InputStream is) {
- if (is != null) {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- /**
- * 關閉輸出流
- * @param os
- */
- private void close(OutputStream os) {
- if (os != null) {
- try {
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- }
通過上面的代碼所示的過程處理後,咱們替換變量後新輸出來的docx文件的內容是這樣的:
![](http://static.javashuo.com/static/loading.gif)