注意:

1、此Demo操作的是word的表格

2、本次使用的word后缀为docx

1、pom引用jar

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>

2、代码

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* word模板赋值下载
*
* @author a
*/
@SpringBootApplication
public class ReadResourceApplication {

/**
* 主方法
*
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(ReadResourceApplication.class, args);

Map map = new HashMap(3);
map.put("${name}", "测试");
map.put("${sexName}", "男");
map.put("${mzName}", "汉");
getBuild("wordXML/领导干部基本情况信息表.docx", map, "D:/aaa.doc");
}

/**
* 赋值并下载文件
*
* @param tmpFile
* @param contentMap
* @param exportFile
*/
public static void getBuild(String tmpFile, Map<String, String> contentMap, String exportFile) {
InputStream inputStream = null;
try {
// 加载Word文件
inputStream = new ClassPathResource(tmpFile).getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
XWPFDocument document = null;
try {
// 加载流到document
document = new XWPFDocument(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
// 获取文档中的表格
List<XWPFTable> tables = document.getTables();
for (int j = 0; j < tables.size(); j++) {
XWPFTable table = tables.get(j);
// 获取表格行
List<XWPFTableRow> rows = table.getRows();
for (int i = 0; i < rows.size(); i++) {
// 得到每一行
XWPFTableRow newRow = table.getRow(i);
// 得到所有的单元格
List<XWPFTableCell> cells = newRow.getTableCells();
for (int k = 0; k < cells.size(); k++) {
// 得到每一列
XWPFTableCell cell = cells.get(k);
// 以下为更新替换值逻辑
List<XWPFParagraph> paragraphs = cell.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
List<XWPFRun> runs = paragraph.getRuns();
String cellText = cell.getText();
if (contentMap.keySet().contains(cellText)) {
for (int m = 0; m < runs.size(); m++) {
if (m == runs.size() - 1) {
runs.get(m).setText(contentMap.get(cellText), 0);
} else {
runs.get(m).setText("", 0);
}
}
} else if (cellText.equals("${picture}")) {
for (int m = 0; m < runs.size(); m++) {
if (m == runs.size() - 1) {
try {
runs.get(m).setText("", 0);
runs.get(m).addPicture(new ClassPathResource("wordXML/培训二维码.png").getInputStream(), XWPFDocument.PICTURE_TYPE_PNG, "培训二维码.png,", Units.toEMU(200), Units.toEMU(200));
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
runs.get(m).setText("", 0);
}
}
}
}
}
}
}
//导出文件到磁盘
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
document.write(byteArrayOutputStream);
OutputStream outputStream = new FileOutputStream(exportFile);
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
需要更改的地方:
1、word文档路径
2、图片路径(不需要图片的可直接删除相关代码)

posted on 2021-10-18 17:11  田坤坤  阅读(582)  评论(0编辑  收藏  举报