Java如动态word,及获取word页码
各位大佬们,有以下几个问题请教下1:如何实现动态word呢,比如在一个空白word中根据业务插入不同的表格、图片等。
比如图片中的两个表格,可能A业务只需要生成第一个。B业务需要两个都生成。都需要填充对应内容,数据从页面表单提交到后台
2:使用Java如何获取word中指定标题序号或者标题名称来获取对应的页码呢。 可以考虑用poi 新建word文档并添加表格:
package top.feiwyuya;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import java.io.FileOutputStream;
import java.io.IOException;
public class POITest {
public static void main(String[] args) throws IOException {
// 1. 新建 word 文档
XWPFDocument document = new XWPFDocument();
// 2. 插入表格1
XWPFTable tableOne = document.createTable(3,4);
tableOne.setWidth("100%");
// 遍历表格的每个单元格
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 4; col++) {
XWPFTableCell cell = tableOne.getRow(row).getCell(col);
// 设置单元格内容
cell.setText("Row " + (row + 1) + ", Col " + (col + 1));
// 如果是最后一列,设置行合并
if (col == 3) {
if (row == 0) {
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
} else {
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
}
}
// 保存文档到文件
FileOutputStream out = new FileOutputStream("test_word.word");
document.write(out);
out.close();
}
}
获取指定标题的页码:
import org.apache.poi.xwpf.usermodel.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
public class GetPageNumberForTitle {
public static void main(String[] args) {
try {
// 读取Word文档
FileInputStream fis = new FileInputStream(new File("your_document.docx"));
XWPFDocument document = new XWPFDocument(fis);
// 获取文档中的所有段落
List<XWPFParagraph> paragraphs = document.getParagraphs();
// 要查找的标题名称
String targetTitle = "Your Target Title";
// 遍历段落,查找指定标题
for (int i = 0; i < paragraphs.size(); i++) {
XWPFParagraph paragraph = paragraphs.get(i);
// 获取段落文本
String text = paragraph.getText();
// 判断当前段落是否是标题,可以根据实际情况调整
if (isTitle(paragraph)) {
// 获取标题级别
int titleLevel = getTitleLevel(paragraph);
// 获取标题文本
String titleText = getTitleText(paragraph);
// 比较标题级别和文本是否匹配
if (titleLevel == 1 && titleText.equals(targetTitle)) {
// 获取当前段落的页码
int pageNumber = getPageNumber(document, i);
System.out.println("Page number for title '" + targetTitle + "': " + pageNumber);
break;
}
}
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 判断段落是否是标题
private static boolean isTitle(XWPFParagraph paragraph) {
String style = paragraph.getStyle();
return style != null && style.startsWith("Heading");
}
// 获取标题级别
private static int getTitleLevel(XWPFParagraph paragraph) {
String style = paragraph.getStyle();
if (style != null && style.startsWith("Heading")) {
return Integer.parseInt(style.substring("Heading".length()));
}
return 0;
}
// 获取标题文本
private static String getTitleText(XWPFParagraph paragraph) {
return paragraph.getText();
}
// 获取指定段落的页码
private static int getPageNumber(XWPFDocument document, int paragraphIndex) {
List<XWPFFooter> footers = document.getFooterList();
for (XWPFFooter footer : footers) {
List<XWPFParagraph> paragraphs = footer.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
if (paragraph.getText().contains("PAGE")) {
return Integer.parseInt(paragraph.getText().split(" "));
}
}
}
return 0;
}
}
maven 依赖:
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.21.1</version>
</dependency> 新建表格测试 可以试试Java中最好的Word模板引擎POI-tl wjw0716 发表于 2023-11-21 11:08
可以考虑用poi
好的,谢谢 codedog999 发表于 2023-11-21 12:07
新建word文档并添加表格:
package top.feiwyuya;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
谢谢大佬,我试试{:1_893:} codedog999 发表于 2023-11-21 12:08
获取指定标题的页码:
import org.apache.poi.xwpf.usermodel.*;
谢谢大佬,我试试,感谢感谢{:1_893:}
页:
[1]
2