Java使用POI导出Word文档的操作教程,poiword
分享于 点击 6568 次 点评:165
Java使用POI导出Word文档的操作教程,poiword
一、主要pom依赖
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.16</version> </dependency>
二、需要导出word模板
三、相关导出代码
package com.iflytek.chy; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; 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; public class App { private static String regex = "[a-zA-Z]+"; private static Pattern pattern = Pattern.compile(regex); public static void main(String[] args) throws IOException { Map<String, String> map = new HashMap<String, String>(); map.put("qbdj", "你好"); map.put("zlhtbt", "姓名"); map.put("fwqh", "性别"); map.put("qbly", "年龄"); map.put("lrsj", "生日"); map.put("bt", "职业"); map.put("xxzw", "工作城市"); map.put("bb", "武汉光谷"); map.put("ss", "武汉光谷"); map.put("ff", "武汉光谷"); map.put("gg", "武汉光谷"); map.put("hh", "武汉光谷"); map.put("dd", "武汉光谷"); String filePath = "D:\\java_workspace_qbNew\\qb-yp\\qb-xsfx\\target\\classes\\config\\template\\SWSJ-test.docx"; InputStream is = new FileInputStream(filePath); @SuppressWarnings("resource") XWPFDocument doc = new XWPFDocument(is); List<XWPFParagraph> list = doc.getParagraphs(); // 替换占位符 for (int i = 0; i < list.size(); i++) { XWPFParagraph xwpfParagraph = list.get(i); List<XWPFRun> runs = xwpfParagraph.getRuns(); if (runs != null && runs.size() > 0) { for (int j = runs.size() - 1; j >= 0; j--) { if (map.containsKey(runs.get(j).text())) { System.out.println("run:" + runs.get(j).text()); runs.get(j).setText(map.get(runs.get(j).text()), 0); } } } } // 替换表格中的占位符 List<XWPFTable> tables = doc.getTables(); for (XWPFTable xwpfTable : tables) { for (int i = 0; i < xwpfTable.getNumberOfRows(); i++) { XWPFTableRow row = xwpfTable.getRow(i); List<XWPFTableCell> tableCells = row.getTableCells(); for (XWPFTableCell xwpfTableCell : tableCells) { List<XWPFParagraph> paragraphs = xwpfTableCell.getParagraphs(); for (XWPFParagraph xwpfParagraph : paragraphs) { List<XWPFRun> runs = xwpfParagraph.getRuns(); for (XWPFRun xwpfRun : runs) { String trim = xwpfRun.text(); Matcher matcher = pattern.matcher(trim); String temp = ""; while (matcher.find()) { temp = matcher.group(); } if (StringUtils.isNotBlank(temp)) { trim = trim.replace(temp, map.get(temp)); } xwpfRun.setText(trim, 0); } } } } } OutputStream os = new FileOutputStream( "D:\\\\java_workspace_qbNew\\\\qb-yp\\\\qb-xsfx\\\\target\\\\classes\\\\config\\\\template\\\\MY-SWSJ-test.docx"); doc.write(os); os.close(); is.close(); } }
用户点评