文档生成合并 11.22

master
zhc077 13 hours ago
parent a5547a550d
commit 9e42d8ba45
  1. 12
      jeecg-boot/jeecg-module-demo/pom.xml
  2. 54
      jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/project/service/impl/Project4WordServiceImpl.java
  3. 189
      jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/utils/FileUtils.java
  4. 7
      jeecg-boot/jeecg-module-system/jeecg-system-start/pom.xml
  5. 56
      jeecg-boot/jeecg-module-system/jeecg-system-start/src/test/java/org/jeecg/modules/word/WordExportUtil_test.java

@ -37,6 +37,18 @@
<artifactId>easypoi-annotation</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>6.1.0</version>
<!--因为项目中slf4j版本冲突,忽悠这个依赖中的slf4j版本-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

@ -24,6 +24,7 @@ import org.jeecg.modules.demo.projectType.entity.ProjectType;
import org.jeecg.modules.demo.projectType.service.IProjectTypeService;
import org.jeecg.modules.demo.userext.entity.UserExt;
import org.jeecg.modules.demo.userext.service.IUserExtService;
import org.jeecg.modules.demo.utils.FileUtils;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
@ -34,6 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import java.io.File;
@ -251,31 +253,61 @@ public class Project4WordServiceImpl extends ServiceImpl<ProjectMapper, Project>
ProjectDto dto = getData4Xiangmushu(obj);
Map<String, Object> map = BeanUtil.beanToMap(dto);
System.out.println("map = " + JSON.toJSONString(map));
try {
File file = new File(model4wordPath + File.separator);
String basePath = file.getPath() + File.separator;
String modelFilePath = basePath + "baseInfoModel.doc";
// String modelFilePath = basePath + "baseInfoModel.docx";
// String modelFilePath = basePath + "model2v.doc";
String modelFilePath = "C:\\Users\\YX\\Documents\\model3v.docx";
log.info("the baseInfoModel path:{}", modelFilePath);
String mergeWordFileName = dto.getProjectName() + ".docx";
File outDir = new File(basePath + "out");
if (!outDir.exists()) {
outDir.mkdirs();
}
try {
//1 生成文件
XWPFDocument doc = WordExportUtil.exportWord07(modelFilePath, map);
List<XWPFDocument> wordList = new ArrayList<>();
wordList.add(doc);
FileInputStream fis = new FileInputStream("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\正文V15.docx");
FileInputStream fis = new FileInputStream(basePath + "正文.docx");
wordList.add(new XWPFDocument(fis));
// 2.把集合里面的word文档全部合并在一个文档
XWPFDocument mergeWord = WordUtils.mergeWord(wordList);
File outDir = new File(basePath+"out");
if (!outDir.exists()) {
outDir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(new File(outDir, "mergework1122.docx"));
XWPFDocument mergeWordObj = WordUtils.mergeWord(wordList);
// String mergeWordFileName = "mergework1122v3.docx";
FileOutputStream fos = new FileOutputStream(new File(outDir, mergeWordFileName));
// FileOutputStream fos2 = new FileOutputStream("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\test1121v7.docx");
mergeWord.write(fos);
mergeWordObj.write(fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
String pdfName = dto.getProjectName()+".pdf";
// createPdf(outDir + File.separator + mergeWordFileName, outDir, pdfName);
// createPdf(outDir + File.separator + "2025黄淮学院工程技术研究中心1121.docx", outDir, pdfName);
} catch (Exception e2) {
e2.printStackTrace();
}
return dto;
}
public Object createPdf(String wordPath, File file, String padFileName){
String pdfFileName = "convertDocx2Pdf.pdf";
String downloadFile = "pdf导出.pdf";
// HttpServletResponse response = getResponse();
if (wordPath.equals("")) {
log.info("word文件路径不能为空!");
Assert.notNull(wordPath, "word路径不能为空");
} else {
// FileUtils.convertDocx2Pdf(wordPath, file + File.separator + padFileName);
try {
FileUtils.convertDocxToPdf(wordPath, file + File.separator + padFileName);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}

@ -0,0 +1,189 @@
package org.jeecg.modules.demo.utils;
import cn.afterturn.easypoi.word.WordExportUtil;
import com.aliyuncs.utils.IOUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.docx4j.Docx4J;
import org.docx4j.convert.out.FOSettings;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.springframework.util.Assert;
import java.io.*;
import java.util.Map;
/**
* @description:
* @param:
* @return:
* @author: z.h.c
* @date: 24/11/22 10:17
*/
public class FileUtils {
/**
*
* @param templatePath
* @param saveDir
* @param fileName
* @param params
* @return
*/
public static String exportWord(String templatePath, String saveDir, String fileName, Map<String, Object> params) {
Assert.notNull(templatePath, "模板路径不能为空");
Assert.notNull(saveDir, "临时文件路径不能为空");
Assert.notNull(fileName, "导出文件名不能为空");
Assert.isTrue(fileName.endsWith(".docx"), "word导出请使用docx格式");
if (!saveDir.endsWith("/")) {
saveDir = saveDir + File.separator;
}
File dir = new File(saveDir);
if (!dir.exists()) {
dir.mkdirs();
}
String savePath = saveDir + fileName;
try {
XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);
FileOutputStream fos = new FileOutputStream(savePath);
doc.write(fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return savePath;
}
/**
* @param wordPath word文件路径
* @param pdfPath pdf输出路径
* @return
*/
public static String convertDocx2Pdf(String wordPath, String pdfPath) {
OutputStream os = null;
InputStream is = null;
if (pdfPath.endsWith("/")) {
pdfPath = pdfPath + File.separator;
}
try {
is = new FileInputStream(new File(wordPath));
WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(is);
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("simsun-extB"));
fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
mlPackage.setFontMapper(fontMapper);
os = new FileOutputStream(pdfPath);
//docx4j docx转pdf
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(mlPackage);
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
is.close();//关闭输入流
os.close();//关闭输出流
return "";
} catch (Exception e) {
e.printStackTrace();
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
} finally {
File file = new File(wordPath);
if (file != null && file.isFile() && file.exists()) {
file.delete();
}
}
return "";
}
/**
* docx文档转换为PDF
*
* @param pdfPath PDF文档存储路径
* @throws Exception 可能为Docx4JException, FileNotFoundException, IOException等
*/
public static void convertDocxToPdf(String docxPath, String pdfPath) throws Exception {
FileOutputStream fileOutputStream = null;
try {
File file = new File(docxPath);
fileOutputStream = new FileOutputStream(new File(pdfPath));
WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(file);
setFontMapper(mlPackage);
Docx4J.toPDF(mlPackage, new FileOutputStream(new File(pdfPath)));
}catch (Exception e){
e.printStackTrace();
// log.error("docx文档转换为PDF失败");
}finally {
IOUtils.closeQuietly(fileOutputStream);
}
}
private static void setFontMapper(WordprocessingMLPackage mlPackage) throws Exception {
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("simsun-extB"));
fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
mlPackage.setFontMapper(fontMapper);
}
/**
* @param path 图片路径
* @return
* @throws IOException
*/
public static byte[] getImageBase64(String path) throws IOException {
InputStream input = new FileInputStream(path);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
byte[] data = output.toByteArray();
output.close();
input.close();
return data;
}
}

@ -26,7 +26,7 @@
</dependency>
<dependency>
<!--<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.3.0</version>
@ -41,6 +41,11 @@
<artifactId>easypoi-annotation</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>6.1.0</version>
</dependency>-->
<!-- flyway 数据库自动升级 -->
<dependency>

@ -1,5 +1,7 @@
package org.jeecg.modules.word;
import cn.afterturn.easypoi.entity.ImageEntity;
import cn.afterturn.easypoi.word.WordExportUtil;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import org.apache.poi.openxml4j.opc.OPCPackage;
@ -9,22 +11,25 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.xmlbeans.XmlOptions;
import org.jeecg.JeecgSystemApplication;
import org.jeecg.common.util.WordUtils;
import org.jeecg.modules.demo.project.service.IProject4WordService;
import org.jeecg.modules.demo.project.service.IProjectService;
//import org.jeecgframework.poi.word.WordExportUtil;
import org.jeecg.modules.demo.utils.FileUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import cn.afterturn.easypoi.word.WordExportUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Description: TODO
@ -72,7 +77,7 @@ public class WordExportUtil_test {
try {
// XWPFDocument doc = WordExportUtil.exportWord07("C:\\Users\\YX\\Desktop\\testword\\model2.doc", map);
XWPFDocument doc = WordExportUtil.exportWord07("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\page1Model.doc", map);
FileOutputStream fos = new FileOutputStream( "C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\test1121v1.docx");
FileOutputStream fos = new FileOutputStream("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\test1121v1.docx");
doc.write(fos);
fos.close();
} catch (Exception e) {
@ -80,9 +85,50 @@ public class WordExportUtil_test {
}
}
@Test
public void test3() {
String wordPath = "C:\\Users\\YX\\Documents\\00.docx";
String padPath = "C:\\Users\\YX\\Documents\\00.pdf";
// FileUtils.convertDocx2Pdf(wordPath, file + File.separator + padFileName);
try {
FileUtils.convertDocxToPdf(wordPath, padPath);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void test4() {
File newFile = new File("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\合并1122v4.docx");
List<File> srcfile = new ArrayList<>();
File file1 = new File("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\正文V14.docx");
File file2 = new File("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\正文V15.docx");
// File file3 = new File("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\yiJianModel.doc");
srcfile.add(file1);
srcfile.add(file2);
List<XWPFDocument> wordList = new ArrayList<>();
try {
FileInputStream fis = new FileInputStream("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\正文V14.docx");
wordList.add(new XWPFDocument(fis));
FileInputStream fis2 = new FileInputStream("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\正文V15.docx");
wordList.add(new XWPFDocument(fis2));
XWPFDocument mergeWordObj = WordUtils.mergeWord(wordList);
FileOutputStream fos = new FileOutputStream("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\合并1122v4.docx");
mergeWordObj.write(fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
System.out.println("*****合成成功222********");
}
@Test
public void test2() {
File newFile = new File("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\合并1122v2.docx");
File newFile = new File("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\合并1122v3.docx");
List<File> srcfile = new ArrayList<>();
File file1 = new File("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\正文V14.docx");

Loading…
Cancel
Save