JAVA 操作WORD2007,插入表格,插入文本:
package word2007; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import org.apache.poi.POIXMLDocument; import org.apache.poi.openxml4j.opc.OPCPackage; 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; import org.apache.xmlbeans.XmlCursor; /** * Word2007工具类 * * curvesapi-1.03、poi-3.14、poi-ooxml-3.14、poi-ooxml-schemas-3.14、stax-api-1.0.1、xmlbeans-2.6.0 * * https://mvnrepository.com/artifact/com.github.virtuald/curvesapi/1.03 * https://mvnrepository.com/artifact/org.apache.poi/poi/3.14 * https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml/3.14 * https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas/3.14 * https://mvnrepository.com/artifact/stax/stax-api/1.0.1 * https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans/2.6.0 * * @author ZengWenFeng * @date 2020.10.20 * @email 117791303@QQ.COM * @phone 13805029595 */ public class Word2007MiniUtil { private static Logger logger = Logger.getLogger(Word2007MiniUtil.class); private static final String PATH = "D:/"; /** * 根据文档路径获取WORD文档 * * @author ZengWenFeng * @date 2010.10.11 * @email 117791303@QQ.COM * @phone 13805029595 * @param wordFilePath 模板路径D:\1.docx */ public static XWPFDocument getWord(String wordFilePath) { logger.info("获取模板路径:" + wordFilePath); XWPFDocument doc = null; try { OPCPackage pack = POIXMLDocument.openPackage(wordFilePath); doc = new XWPFDocument(pack); } catch (Exception e) { logger.info("获取模板异常错误:" + e.getMessage()); e.printStackTrace(); } return doc; } /** * 指定位置key插入文本value * * * @author ZengWenFeng * @date 2010.10.11 * @email 117791303@QQ.COM * @phone 13805029595 * @param doc 文档 * @param key 指定位置 * @param value 文本值 */ public static void insertText(XWPFDocument doc, String key, String value) { List<XWPFParagraph> listParagraph = doc.getParagraphs(); if (listParagraph != null && listParagraph.size() > 0) { for (XWPFParagraph paragraph : listParagraph) { List<XWPFRun> runs = paragraph.getRuns(); for (XWPFRun run : runs) { String text = run.getText(0); if (text != null) { boolean isSetText = false; if (text.equals(key)) { isSetText = true; logger.info("替换文本标签:将键【" + key + "】换成【" + value + "】"); text = text.replace(key, value); } if (isSetText) { run.setText(text, 0); } } } } } } /** * 指定位置key插入表格 * * @author ZengWenFeng * @date 2010.10.11 * @email 117791303@QQ.COM * @phone 13805029595 * @param doc * @param key * @param listHeader * @param listData */ public static void insertTable(XWPFDocument doc, String key, String[] listHeader, List<String[]> listData) { List<XWPFParagraph> paragraphList = doc.getParagraphs(); if (paragraphList != null && paragraphList.size() > 0) { for (XWPFParagraph paragraph : paragraphList) { List<XWPFRun> runs = paragraph.getRuns(); for (XWPFRun run : runs) { String text = run.getText(0); // if (text != null && text.equals(key)) { logger.info("替换表格标签:将键【" + key + "】换成表格"); //把${table}标签移除了 run.setText("", 0); //指针移动到添加表格的位置,插入表格 XmlCursor cursor = paragraph.getCTP().newCursor(); XWPFTable table = doc.insertNewTbl(cursor); //表格头 if (listHeader != null && listHeader.length > 0) { XWPFTableRow rowHeader = table.getRow(0); for (int i = 0; i < listHeader.length; i++) { XWPFTableCell cell = null; if (i == 0) { cell = rowHeader.getCell(0); } else { cell = rowHeader.createCell(); } if (cell == null) { continue; } cell.setText(listHeader[i]); } } //表格数据 if (listData != null && listData.size() > 0) { for (int i = 0; i < listData.size(); i++) { XWPFTableRow rowData = table.createRow();//table.getRow(1); String[] curData = listData.get(i); if (curData != null && curData.length > 0) { for (int j = 0; j < curData.length; j++) { XWPFTableCell cell = rowData.getCell(j); /* if (j == 0) { cell = rowData.getCell(0); } else { cell = rowData.createCell(); } */ if (cell == null) { continue; } cell.setText(curData[j]); } } } } } } } } } /** * * @author ZengWenFeng * @date 2010.10.11 * @email 117791303@QQ.COM * @phone 13805029595 * @param doc * @param filePath */ public static void createNewWord(XWPFDocument doc, String filePath) { FileOutputStream fopts = null; try { fopts = new FileOutputStream(filePath); doc.write(fopts); if (fopts != null) { fopts.close(); } } catch (IOException e1) { e1.printStackTrace(); } } public static void test() throws Exception { // String[] listHeader = new String[]{"id", "code", "name"}; // List<String[]> listData = new ArrayList<String[]>(); String[] data_01 = new String[]{"zwf-0001", "005129", "zwf00000011"}; String[] data_02 = new String[]{"zwf-0002", "005128", "zengwenfeng"}; String[] data_03 = new String[]{"zwf-0003", "005127", "zeng00000wf"}; String[] data_04 = new String[]{"zwf-0004", "005126", "wenfeng3434"}; listData.add(data_01); listData.add(data_02); listData.add(data_03); listData.add(data_04); // XWPFDocument doc = Word2007MiniUtil.getWord(PATH + "3.docx"); Word2007MiniUtil.insertText(doc, "${text}", "哪里来的随便随便成功!"); Word2007MiniUtil.insertText(doc, "${text1}", "游刃有余都是汗水!"); Word2007MiniUtil.insertTable(doc, "${ttttt}", listHeader, listData); // /----------创建表 // createNewWord(doc, PATH + "4.docx"); } public static void main(String[] args) throws Exception { test(); System.out.println("ok!"); System.out.println("我把我自己感动到哭了!"); } }
模板【3.docx】一定要注意正确:
下面这个就有可能是错误的,会找不到:
-------------------------得到结果-【4.docx】----------------------------------