部分主要代码:
@GetMapping("/export")
public void exporttopdf(@Param("lunwenid") Integer lunwenid,HttpServletResponse response)
throws IOException, InvocationTargetException, IllegalAccessException {
QueryWrapper<LunwenDetailEntity> wrapper=new QueryWrapper();
wrapper.lambda().eq(LunwenDetailEntity::getLunwenid,lunwenid);
List<LunwenDetailEntity> lunwenDetail = lunwenDetailService.list(wrapper);
LunwenEntity lunwenEntity=lunwenService.getById(lunwenid);
WordParam wordParam = new WordParam();
wordParam.setLunwenname(lunwenEntity.getLunwenname());
wordParam.setName(lunwenEntity.getName());
wordParam.setNumber(lunwenEntity.getNumber());
wordParam.setZy(lunwenEntity.getZy());
wordParam.setWcdate(lunwenEntity.getWcdate());
wordParam.setTeacher(lunwenEntity.getTeacher());
wordParam.setXy(lunwenEntity.getXy());
lunwenDetail.forEach(LunwenEntity->{
if(LunwenEntity.getTabtitleid().equals("1402871389312622592")){
wordParam.setZhaiyao(LunwenEntity.getContent());
}else if(LunwenEntity.getTabtitleid().equals("1402871389312622593")){
wordParam.setGjc(LunwenEntity.getContent());
}else if(LunwenEntity.getTabtitleid().equals("1402871389312622594")){
wordParam.setEnglishzy(LunwenEntity.getContent());
}else if(LunwenEntity.getTabtitleid().equals("1402871389312622595")){
wordParam.setEnglishgjz(LunwenEntity.getContent());
}
else if(LunwenEntity.getTabtitleid().equals("1402871389312622595")){
wordParam.setEnglishgjz(LunwenEntity.getContent());
}
else if(LunwenEntity.getTabtitleid().equals("1402871389312622595")){
wordParam.setEnglishgjz(LunwenEntity.getContent());
}
else if(LunwenEntity.getTabtitleid().equals("1402871389312622595")){
wordParam.setEnglishgjz(LunwenEntity.getContent());
}else if(LunwenEntity.getTabtitleid().equals("1402871389312622595")){
wordParam.setEnglishgjz(LunwenEntity.getContent());
}else if(LunwenEntity.getTabtitleid().equals("1480445351848153088")){//参考文献
wordParam.setWx(LunwenEntity.getContent());
}
});
// 模版位置
File file = new File(fileSpace+"\\test.docx");
InputStream inputStream = new FileInputStream(file);
XWPFDocument document = new XWPFDocument(inputStream);
// 获取整个文本对象
List<XWPFParagraph> paragraphs = document.getParagraphs();
// 存放匹配字段的值
Map map=new HashMap();
map.put("${lunwenname}","Lunwenname");
map.put("${name}","Name");
map.put("${number}","Number");
map.put("${teacher}","Teacher");
map.put("${xy}","Xy");
map.put("${zy}","Zy");
map.put("${wcdate}","Wcdate");
map.put("${zhaiyao}","Zhaiyao");
map.put("${gjc}","Gjc");
map.put("${englishzy}","Englishzy");
map.put("${englishgjz}","Englishgjz");
.......省略
// for (XWPFParagraph paragraph : paragraphs) {
for (int par=0;par<paragraphs.size();par++) {
XWPFParagraph paragraph=paragraphs.get(par);
List<XWPFRun> runs = paragraph.getRuns();
for (int i=0;i<runs.size();i++) {
XWPFRun run=runs.get(i);
// 获取word中每一个段落的内容
String runString = run.toString();
// 正则匹配word要替换的内容
Matcher m = Pattern.compile("\\$\\{(.*?)}").matcher(runString);
if (m.find()) {
for (Object key : map.keySet()) {
if (runString.equals(key)) {
Object param = map.get(key);
String getParam = "get" + param.toString();
Method method = ReflectUtil.getMethod(WordParam.class, getParam);
Object o = method.invoke(wordParam);
run.setText(o.toString(), 0);
}
}
}
}
}
FileOutputStream fileOutputStream = new FileOutputStream(fileSpace+"\\"+wordParam.getLunwenname()+".docx");
document.write(fileOutputStream);
document.close();
try {
WordprocessingMLPackage pkg = Docx4J.load(new File(fileSpace+"\\"+wordParam.getLunwenname()+".docx"));
Mapper fontMapper = new IdentityPlusMapper();
fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
fontMapper.put("华文新魏", PhysicalFonts.get("STXinwei"));
fontMapper.put("等线", PhysicalFonts.get("SimSun"));
fontMapper.put("等线 Light", PhysicalFonts.get("SimSun"));
fontMapper.put("华文琥珀", PhysicalFonts.get("STHupo"));
fontMapper.put("华文隶书", PhysicalFonts.get("STLiti"));
fontMapper.put("华文新魏", PhysicalFonts.get("STXinwei"));
fontMapper.put("华文彩云", PhysicalFonts.get("STCaiyun"));
fontMapper.put("方正姚体", PhysicalFonts.get("FZYaoti"));
fontMapper.put("方正舒体", PhysicalFonts.get("FZShuTi"));
fontMapper.put("华文细黑", PhysicalFonts.get("STXihei"));
fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB"));
fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
fontMapper.put("新細明體", PhysicalFonts.get("SimSun"));
pkg.setFontMapper(fontMapper);
Docx4J.toPDF(pkg, new FileOutputStream(fileSpace+"\\"+wordParam.getLunwenname()+".pdf"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Docx4JException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally {
byte[] bytes = fileHolder.findByKey(wordParam.getLunwenname()+".pdf",fileSpace);
MvcUtil.viewData(response, bytes,wordParam.getLunwenname()+".pdf");
}
}
部分运行效果图: