如果遇到poi讀取例如{name}不能識別為一個整體,可以使用word的域操作,如果不太清楚域的使用,可以這么操作,先在text文檔中寫好,例如{name},然后再整個復制到word中,不要一個一個在word中敲,不然有可能不會被poi識別為一個整體
XWPFDocument對象
POI是apache提供的可以操作word文檔的第三方jar 。POI能操作word是使用XWPFDocument對象 。
XWPFDocument對象可以解析docx文件,在XWPFDocument對象通過輸入流解析docx的時候,會獲取到docx文檔中的各種對象,例如表格,段落,圖片等,通過操作XWPFDocument對象就可以修改模板內容XWPFDocument API結構org.apache.poi.xwpf.usermodel.XWPFDocumentXWPFDocument 提供write(OutputStream stream)方法將修改后的對象重新寫入xml并生成新的docx
通過XWPFDocument 可以獲得的docx中的各種對象
通過XWPFDocument 獲取對象
//解析docx模板并獲取document對象XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));//獲取整個文本對象List<XWPFParagraph> allParagraph = document.getParagraphs();//獲取整個表格對象List<XWPFTable> allTable = document.getTables();//獲取圖片對象XWPFPictureData pic = document.getPictureDataByID("PICId");首先建一個很簡單的word模板001.docx,我們通過操作對象獲取word中的文本內容
@Component("xWPRUNTest")public class XWPRUNTest {//模板文件地址private static String inputUrl = "C:\Users\zhihe\Desktop\demo\001.docx";public void runTest(){try {//解析docx模板并獲取document對象XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));//獲取整個文本對象List<XWPFParagraph> allParagraph = document.getParagraphs();//獲取XWPFRun對象輸出整個文本內容StringBuffer tempText = new StringBuffer();for (XWPFParagraph xwpfParagraph : allParagraph) {List<XWPFRun> runList = xwpfParagraph.getRuns();for (XWPFRun xwpfRun : runList) {tempText.append(xwpfRun.toString());}}System.out.println(tempText.toString());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}測試
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class runTest {@Resourceprivate XWPRUNTest xWPRUNTest;@Testpublic void runTest(){xWPRUNTest.runTest();}}控制臺輸出結果
@Component("xWPRUNTableTest")public class XWPRUNTableTest {//模板文件地址private static String inputUrl = "C:\Users\zhihe\Desktop\demo\001.docx";public void tableTest(){try {StringBuffer tableText = new StringBuffer();//解析docx模板并獲取document對象XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));//獲取全部表格對象List<XWPFTable> allTable = document.getTables();for (XWPFTable xwpfTable : allTable) {//獲取表格行數據List<XWPFTableRow> rows = xwpfTable.getRows();for (XWPFTableRow xwpfTableRow : rows) {//獲取表格單元格數據List<XWPFTableCell> cells = xwpfTableRow.getTableCells();for (XWPFTableCell xwpfTableCell : cells) {List<XWPFParagraph> paragraphs = xwpfTableCell.getParagraphs();for (XWPFParagraph xwpfParagraph : paragraphs) {List<XWPFRun> runs = xwpfParagraph.getRuns();for(int i = 0; i < runs.size();i++){XWPFRun run = runs.get(i);tableText.append(run.toString());}}}}}System.out.println(tableText.toString());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}測試成功獲取表格
public class FirstWordTest {//模板文件地址private static String inputUrl = "C:\Users\zhihe\Desktop\demo\001.docx";//新生產的模板文件private static String outputUrl = "C:\Users\zhihe\Desktop\demo\test.docx";/**** @param inputUrl 模板路徑* @param outputUrl 模板保存路徑*/public static void changeWord(String inputUrl, String outputUrl ){try {//獲取word文檔解析對象XWPFDocument doucument = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));//獲取段落文本對象List<XWPFParagraph> paragraph = doucument.getParagraphs();//獲取首行run對象XWPFRun run = paragraph.get(0).getRuns().get(0);//設置文本內容run.setText("修改了的word");//生成新的wordFile file = new File(outputUrl);FileOutputStream stream = new FileOutputStream(file);doucument.write(stream);stream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String[] args) {changeWord(inputUrl,outputUrl);}}測試運行后生成新的word
首先創建一個word的模板
package com.lovo.utils.wordToPdf;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import org.apache.poi.POIXMLDocument;import org.apache.poi.xwpf.usermodel.XWPFDocument;import org.apache.poi.xwpf.usermodel.XWPFParagraph;import org.apache.poi.xwpf.usermodel.XWPFRun;import org.apache.poi.xwpf.usermodel.XWPFTable;import org.apache.poi.xwpf.usermodel.XWPFTableCell;import org.apache.poi.xwpf.usermodel.XWPFTableRow;/** * 通過word模板生成新的word工具類 ** @author zhiheng **/public class WorderToNewWordUtils {/*** 根據模板生成新word文檔* 判斷表格是需要替換還是需要插入,判斷邏輯有$為替換,表格無$為插入* @param inputUrl 模板存放地址* @param outPutUrl 新文檔存放地址* @param textMap 需要替換的信息集合* @param tableList 需要插入的表格信息集合* @return 成功返回true,失敗返回false*/public static boolean changWord(String inputUrl, String outputUrl,Map<String, String> textMap, List<String[]> tableList) {//模板轉換默認成功boolean changeFlag = true;try {//獲取docx解析對象XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));//解析替換文本段落對象WorderToNewWordUtils.changeText(document, textMap);//解析替換表格對象WorderToNewWordUtils.changeTable(document, textMap, tableList);//生成新的wordFile file = new File(outputUrl);FileOutputStream stream = new FileOutputStream(file);document.write(stream);stream.close();} catch (IOException e) {e.printStackTrace();changeFlag = false;}return changeFlag;}/*** 替換段落文本* @param document docx解析對象* @param textMap 需要替換的信息集合*/public static void changeText(XWPFDocument document, Map<String, String> textMap){//獲取段落集合List<XWPFParagraph> paragraphs = document.getParagraphs();for (XWPFParagraph paragraph : paragraphs) {//判斷此段落時候需要進行替換String text = paragraph.getText();if(checkText(text)){List<XWPFRun> runs = paragraph.getRuns();for (XWPFRun run : runs) {//替換模板原來位置run.setText(changeValue(run.toString(), textMap),0);}}}}/*** 替換表格對象方法* @param document docx解析對象* @param textMap 需要替換的信息集合* @param tableList 需要插入的表格信息集合*/public static void changeTable(XWPFDocument document, Map<String, String> textMap,List<String[]> tableList){//獲取表格對象集合List<XWPFTable> tables = document.getTables();for (int i = 0; i < tables.size(); i++) {//只處理行數大于等于2的表格,且不循環表頭XWPFTable table = tables.get(i);if(table.getRows().size()>1){//判斷表格是需要替換還是需要插入,判斷邏輯有$為替換,表格無$為插入if(checkText(table.getText())){List<XWPFTableRow> rows = table.getRows();//遍歷表格,并替換模板eachTable(rows, textMap);}else{//System.out.println("插入"+table.getText());insertTable(table, tableList);}}}}/*** 遍歷表格* @param rows 表格行對象* @param textMap 需要替換的信息集合*/public static void eachTable(List<XWPFTableRow> rows ,Map<String, String> textMap){for (XWPFTableRow row : rows) {List<XWPFTableCell> cells = row.getTableCells();for (XWPFTableCell cell : cells) {//判斷單元格是否需要替換if(checkText(cell.getText())){List<XWPFParagraph> paragraphs = cell.getParagraphs();for (XWPFParagraph paragraph : paragraphs) {List<XWPFRun> runs = paragraph.getRuns();for (XWPFRun run : runs) {run.setText(changeValue(run.toString(), textMap),0);}}}}}}/*** 為表格插入數據,行數不夠添加新行* @param table 需要插入數據的表格* @param tableList 插入數據集合*/public static void insertTable(XWPFTable table, List<String[]> tableList){//創建行,根據需要插入的數據添加新行,不處理表頭for(int i = 1; i < tableList.size(); i++){XWPFTableRow row =table.createRow();}//遍歷表格插入數據List<XWPFTableRow> rows = table.getRows();for(int i = 1; i < rows.size(); i++){XWPFTableRow newRow = table.getRow(i);List<XWPFTableCell> cells = newRow.getTableCells();for(int j = 0; j < cells.size(); j++){XWPFTableCell cell = cells.get(j);cell.setText(tableList.get(i-1)[j]);}}}/*** 判斷文本中時候包含$* @param text 文本* @return 包含返回true,不包含返回false*/public static boolean checkText(String text){boolean check=false;if(text.indexOf("$")!= -1){check = true;}return check;}/*** 匹配傳入信息集合與模板* @param value 模板需要替換的區域* @param textMap 傳入信息集合* @return 模板需要替換區域信息集合對應值*/public static String changeValue(String value, Map<String, String> textMap){Set<Entry<String, String>> textSets = textMap.entrySet();for (Entry<String, String> textSet : textSets) {//匹配模板與替換值 格式${key}String key = "${"+textSet.getKey()+"}";if(value.indexOf(key)!= -1){value = http://www.mnbkw.com/jxjc/168007/textSet.getValue();}}//模板未匹配到區域替換為空if(checkText(value)){value ="";}return value;}public static void main(String[] args) {//模板文件地址String inputUrl = "C:\Users\zhihe\Desktop\demo\001.docx";//新生產的模板文件String outputUrl = "C:\Users\zhihe\Desktop\demo\test.docx";Map<String, String> testMap = new HashMap<String, String>();testMap.put("name", "小明");testMap.put("sex", "男");testMap.put("address", "軟件園");testMap.put("phone", "88888888");List<String[]> testList = new ArrayList<String[]>();testList.add(new String[]{"1","1AA","1BB","1CC"});testList.add(new String[]{"2","2AA","2BB","2CC"});testList.add(new String[]{"3","3AA","3BB","3CC"});testList.add(new String[]{"4","4AA","4BB","4CC"});WorderToNewWordUtils.changWord(inputUrl, outputUrl, testMap, testList);}}測試
【easypoi導出word表格】如果遇到poi讀取例如{name}不能識別為一個整體,可以使用word的域操作,如果不太清楚域的使用,可以這么操作,先在text文檔中寫好,例如{name},然后再整個復制到word中,不要一個一個在word中敲,不然有可能不會被poi識別為一個整體
- docx文檔是什么文檔 docx文檔是什么格式
- word加密文件怎么解密 加密word怎么解密碼
- word轉換成jpg格式 jpg怎么轉換成word文檔軟件
- 把word轉換成ppt的五種方法 如何word轉換成ppt
- Word文檔加密破解 word2010加密文檔如何破解
- 圖片如何轉化為word文檔格式 圖片轉化為Word
- PPT轉成文檔 PPT轉成word文檔
- word怎么計算總和:如何在Word2007表格中進行求和運算?
- 項目符號怎么設置,word項目符號和編號在哪,項目符號和編號怎么用?
- cad輸出為圖片,cad怎么導出jpg圖片?
