针对java操作word,大家有不懂的可以在评论区留言。
前言
最近做项目要用到word导出和导入,从网上找资料,发现都是一些零零散散的碎片,不能一步到位,经过项目的论证,站到业务和技术的角度,写一篇java通过poi,自定义word模板,对word文档内容进行替换,对文档中表格进行填充,特殊表格的复制、添加图片,希望对各读者有用😁。
maven引用:
作者使用的是4.1.2版本的poi,如果项目中使用的easyexcel,需要解决jar包冲突。
<properties>
<poi.version>4.1.2</poi.version>
</properties>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
2023.07.14更新
识别word中上标、下标
在工具类中添加以下代码
// 识别word中上标、下标
public List<String> getTextsBySubAndSup() {
List<String> list = new ArrayList<>();
List<XWPFParagraph> paras = xwpfDocument.getParagraphs();
Iterator<XWPFParagraph> paragraphsIterator = xwpfDocument.getParagraphsIterator();
while (paragraphsIterator.hasNext()){
String text = "";
XWPFParagraph next = paragraphsIterator.next();
for (XWPFRun xwrun : next.getRuns()) {
VerticalAlign subscript = xwrun.getSubscript();
String smalltext = xwrun.getText(0);
switch (subscript) {
case BASELINE:
// 正常文本
text += smalltext;
break;
case SUBSCRIPT:
// 下标
text += "<sub>" + smalltext + "</sub>";
break;
case SUPERSCRIPT:
// 上标
text += "<sup>" + smalltext + "</sup>";
break;
}
}
if(null != text && !"".equals(text)){
list.add(text);
}
}
return list;
}
示例:
代码识别后结果:
通过富文本解析即可实现需求。
导出测试类:
// word模板名称
private static String temName = "apiTemplate.docx";
public static void main(String[] args) throws NoSuchFieldException {
// 测试数据
Map m = new HashMap<>();
m.put("APINAME", "测试导出word");
m.put("year", "2023");
m.put("month", "02");
m.put("day", "22");
m.put("APIPATH", "/test");
m.put("remark", "11");
List<Map<String, String>> list = new ArrayList<>();
for(int i = 0; i < 5; i++){
Map map = new HashMap();
map.put("a", "1" + i);
map.put("b", "2" + i);
map.put("c", "3" + i);
map.put("d", "4" + i);
list.add(map);
}
// word工具类 下文提供工具类代码
WordReporterUtils wordReporter = new WordReporterUtils();
try {
// 初始化模板,调用执行方法前必须调用初始化方法
wordReporter.init("模板路径" + temName);
// 普通文本替换
wordReporter.exportText(m);
/**
* 特殊表格赋值
* text:文本值
* x:行下标
* y:列下标
* tablIndex:表格下标
* fontSize: 文字大小
*/
wordReporter.changeTableProvince("我终于输出了", 1, 1,0, 16);
/**
* @param params 循环输入表格数据
* @param x 模板行
* @param tableIndex 表格下标
*/
wordReporter.exportTable(list, 1, 1);
// 生成文件,最后调用
wordReporter.generate("D:\\file\\word\\a.docx");
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("执行完毕");
}
导入测试类:
poi识别文档中的行与可见行是有区别的,举个例子。
没有手动换行还是属于一行,没有手动换行还是属于一行,没有手动换行还是属于一行,没有手动换行还是属于一行,没有手动换行还是属于一行,没有手动换行还是属于一行。
这是单独的一行
这是
单独的一行
如上述例子,通过下方的测试类在识别文档文字时,序号1,2,3,4分别为行,既是序号1的内容太长,在识别中还是属于一行。
public static void main(String[] args) throws NoSuchFieldException {
String filePath = "导入的文档路径";
WordReporterUtils wordReporter = new WordReporterUtils();
try {
// 初始化模板
wordReporter.init(filePath);
// 获取文档所有的文字
List<String> texts = wordReporterUtils.getPictureText();
/**
* texts是word中所有的文字
*/
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("执行完毕");
}
word工具类:
package com.ruoyi.common.utils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import sun.misc.BASE64Decoder;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class WordReporterUtils {
private String tempLocalPath;
private XWPFDocument xwpfDocument = null;
private FileInputStream inputStream = null;
private OutputStream outputStream = null;
public void init(String path) throws Exception {
this.tempLocalPath = path;
inputStream = new FileInputStream(new File(this.tempLocalPath));
xwpfDocument = new XWPFDocument(inputStream);
}
/**
* 特殊表格赋值
*
* @param text 文本值
* @param x 行
* @param y 列
* @param tableIndex 表格下标
*/
public void changeTableProvince(String text, int x, int y, int tableIndex, int fontSize) {
//获取表格对象集合
List<XWPFTable> tables = xwpfDocument.getTables();
XWPFTable table = tables.get(tableIndex);
XWPFTableRow xwpfTable = table.getRow(x);
XWPFTableCell cell = xwpfTable.getTableCells().get(y);
XWPFParagraph xwpfParagraph = cell.getParagraphs().get(0);
XWPFRun run = xwpfParagraph.createRun();
run.setFontSize(fontSize);
run.setFontFamily("宋体");
run.setText(text);
}
/**
* @param params 循环输入表格数据
* @param x 模板行
* @param tableIndex 表格下标
* @return
* @throws Exception
*/
public boolean exportTable(List<Map<String, String>> params, int x, int tableIndex) throws Exception {
List<XWPFTable> tables = xwpfDocument.getTables();
if (tables.size() < tableIndex) {
throw new Exception("tableIndex对应的表格不存在");
}
//获取表格对象集合
XWPFTable table = tables.get(tableIndex);
// 获取模板行
XWPFTableRow temTableRow = table.getRow(x);
List<XWPFTableCell> tableCells = temTableRow.getTableCells();
String key = null;
for (int i = 0; i < params.size(); i++) {
// 插入的数据
Map<String, String> map = params.get(i);
// 创建新行
// XWPFTableRow row = table.createRow();
XWPFTableRow row = table.insertNewTableRow(x + i + 1);
// 复制属性
row.getCtRow().setTrPr(temTableRow.getCtRow().getTrPr());
//复制列对象
XWPFTableCell targetCell = null;
System.out.println(tableCells.size());
for (XWPFTableCell copyCell : tableCells) {
targetCell = row.addNewTableCell();
targetCell.getCTTc().setTcPr(copyCell.getCTTc().getTcPr());
if (copyCell.getParagraphs() != null && copyCell.getParagraphs().size() > 0) {
targetCell.getParagraphs().get(0).getCTP().setPPr(copyCell.getParagraphs().get(0).getCTP().getPPr());
if (copyCell.getParagraphs().get(0).getRuns() != null
&& copyCell.getParagraphs().get(0).getRuns().size() > 0) {
XWPFRun cellR = targetCell.getParagraphs().get(0).createRun();
cellR.setBold(copyCell.getParagraphs().get(0).getRuns().get(0).isBold());
}
}
}
List<XWPFTableCell> cells = row.getTableCells();
row.setHeight(temTableRow.getHeight());
for (int j = 0; j < cells.size(); j++) {
String text = "";
XWPFTableCell cell = tableCells.get(j);
String temKey = cell.getText();
key = temKey.replace("$", "").replace("{", "").replace("}", "");
if (map.containsKey(key)) {
text = map.get(key);
}
XWPFTableCell newCell = row.getTableCells().get(j);
XWPFParagraph xwpfParagraph = newCell.getParagraphs().get(0);
XWPFRun run = xwpfParagraph.createRun();
XWPFRun tmpR = cell.getParagraphs().get(0).getRuns().get(0);
if (!run.isBold()) {
run.setBold(tmpR.isBold());
}
run.setItalic(tmpR.isItalic());
run.setUnderline(tmpR.getUnderline());
run.setColor(tmpR.getColor());
run.setTextPosition(tmpR.getTextPosition());
if (tmpR.getFontSize() != -1) {
run.setFontSize(tmpR.getFontSize());
}
if (tmpR.getFontFamily() != null) {
run.setFontFamily(tmpR.getFontFamily());
} else {
run.setFontFamily("宋体");
}
run.setText(text);
}
}
table.removeRow(x);
return true;
}
/**
* @param picture 2进制图片
* @param signatureName 替换名称
* @throws IOException
*/
public void signaturePicture(String picture, String signatureName) throws IOException, InvalidFormatException {
String path = "D:\\ssss.png";
FileInputStream is = outOfPicture(picture, path);
List<XWPFParagraph> paras = xwpfDocument.getParagraphs();
for (XWPFParagraph para : paras) {
//当前段落的属性
String str = para.getText();
List<XWPFRun> list = para.getRuns();
for (XWPFRun run : list) {
if (signatureName.equals(run.text())) {
run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG, path, Units.toEMU(100), Units.toEMU(100));
run.setText(" ", 0);
}
}
}
}
/**
* 普通文本替换
*
* @param textMap
* @return
*/
public boolean exportText(Map textMap) {
replaceText(xwpfDocument, textMap);
return true;
}
/**
* 替换非表格埋点值
*
* @param xwpfDocument
* @param textMap 需要替换的文本入参
*/
public void replaceText(XWPFDocument xwpfDocument, Map<String, String> textMap) {
List<XWPFParagraph> paras = xwpfDocument.getParagraphs();
Set<String> keySet = textMap.keySet();
for (XWPFParagraph para : paras) {
//当前段落的属性
String str = para.getText().trim();
List<XWPFRun> list = para.getRuns();
for (XWPFRun run : list) {
for (String key : keySet) {
if (key.trim().equals(run.text().trim())) {
run.setText(textMap.get(key), 0);
}
}
}
}
}
// 获取word文档中所有的文字
public List<String> getTexts() {
List<String> list = new ArrayList<>();
List<XWPFParagraph> paras = xwpfDocument.getParagraphs();
for (XWPFParagraph para : paras) {
if (str != null && !"".equals(str)) {
list.add(str);
}
}
return list;
}
private FileInputStream outOfPicture(String picture, String path) throws IOException {
// 将2进制图片输出
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes = decoder.decodeBuffer(picture);
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
fos.flush();
fos.close();
return new FileInputStream(path);
}
/**
* 收尾方法
*
* @param outDocPath
* @return
* @throws IOException
*/
public boolean generate(String outDocPath) throws IOException {
outputStream = new FileOutputStream(outDocPath);
xwpfDocument.write(outputStream);
this.close(outputStream);
this.close(inputStream);
return true;
}
/**
* 关闭输出流
*
* @param os
*/
private void close(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 关闭输入流
*
* @param is
*/
private void close(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
word模板示例:
封面模板
普通文本替换:
特殊表格赋值:
表格循环添加数据:文章来源:https://www.toymoban.com/news/detail-529115.html
实现效果如下:文章来源地址https://www.toymoban.com/news/detail-529115.html
到了这里,关于JAVA POI操作word一篇就够(超级实用)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!