下载法规清单word文件接口开发。
根据项目库id 查询当前登陆人在该项目中是什么角色接口开发。
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
ComplianceReportTemplate.docx : 法规清单 - 导出报告模板
|
||||||
Binary file not shown.
@@ -166,6 +166,41 @@
|
|||||||
<artifactId>fastjson</artifactId>
|
<artifactId>fastjson</artifactId>
|
||||||
<version>1.2.62</version>
|
<version>1.2.62</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<!--导出docx文件-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.docx4j</groupId>
|
||||||
|
<artifactId>docx4j</artifactId>
|
||||||
|
<version>6.0.1</version>
|
||||||
|
<!--排除冲突jar-->
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.docx4j</groupId>
|
||||||
|
<artifactId>docx4j-export-fo</artifactId>
|
||||||
|
<version>6.0.1</version>
|
||||||
|
<!--排除冲突jar-->
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
+10
@@ -159,4 +159,14 @@ public class ProjectCertificationDirectoryEOController extends JeroController<Pr
|
|||||||
return super.importExcel(request, response, ProjectCertificationDirectoryEO.class);
|
return super.importExcel(request, response, ProjectCertificationDirectoryEO.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据项目库id 查询当前登陆人在该项目中是什么角色
|
||||||
|
* @param projectLibraryId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping(value = "/queryUserPremissionByProjectLibraryId")
|
||||||
|
public Result<?> queryUserPremissionByProjectLibraryId(@RequestParam(name="projectLibraryId",required=true) String projectLibraryId) {
|
||||||
|
return projectCertificationDirectoryEOService.queryUserPremissionByProjectLibraryId(projectLibraryId);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-6
@@ -380,12 +380,7 @@ public class ProjectLawsInventoryEOController extends JeroController<ProjectLaws
|
|||||||
@AutoLog(value = "下载pdf报告")
|
@AutoLog(value = "下载pdf报告")
|
||||||
@ApiOperation(value="下载pdf报告", notes="下载pdf报告")
|
@ApiOperation(value="下载pdf报告", notes="下载pdf报告")
|
||||||
@GetMapping(value = "/downloadReport")
|
@GetMapping(value = "/downloadReport")
|
||||||
// public void downloadReport(@RequestParam Map<String,Object> params, HttpServletRequest req, HttpServletResponse resp) throws DocumentException {
|
public void downloadReport(@RequestParam Map<String,Object> params, HttpServletRequest req, HttpServletResponse resp) throws DocumentException {
|
||||||
public void downloadReport(@RequestParam("cut")String cut,@RequestParam("id")String id, HttpServletRequest req, HttpServletResponse resp) throws DocumentException {
|
|
||||||
Map<String,Object> params = new HashMap<>();
|
|
||||||
params.put("cut",cut);
|
|
||||||
params.put("id",id);
|
|
||||||
|
|
||||||
this.projectLawsInventoryEOService.downloadReport(params,req,resp);
|
this.projectLawsInventoryEOService.downloadReport(params,req,resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -415,4 +410,20 @@ public class ProjectLawsInventoryEOController extends JeroController<ProjectLaws
|
|||||||
return jsonResult;
|
return jsonResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param params
|
||||||
|
* cut:中英文标识
|
||||||
|
* id:法规清单id
|
||||||
|
* @param req
|
||||||
|
* @param resp
|
||||||
|
* @throws DocumentException
|
||||||
|
*/
|
||||||
|
@AutoLog(value = "下载docx报告")
|
||||||
|
@ApiOperation(value="下载docx报告", notes="下载docx报告")
|
||||||
|
@GetMapping(value = "/downloadDocxReport")
|
||||||
|
public void downloadDocxReport(@RequestParam Map<String,Object> params, HttpServletRequest req, HttpServletResponse resp) throws DocumentException {
|
||||||
|
this.projectLawsInventoryEOService.downloadDocxReport(params,req,resp);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+7
@@ -71,4 +71,11 @@ public interface IProjectCertificationDirectoryEOService extends IService<Projec
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Result<?> queryPageList(ProjectCertificationDirectoryEO projectCertificationDirectoryEO, Integer pageNo, Integer pageSize, HttpServletRequest req);
|
Result<?> queryPageList(ProjectCertificationDirectoryEO projectCertificationDirectoryEO, Integer pageNo, Integer pageSize, HttpServletRequest req);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据项目库id 查询当前登陆人在该项目中是什么角色
|
||||||
|
* @param projectLibraryId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Result<?> queryUserPremissionByProjectLibraryId(String projectLibraryId);
|
||||||
}
|
}
|
||||||
|
|||||||
+15
@@ -121,5 +121,20 @@ public interface IProjectLawsInventoryEOService extends IService<ProjectLawsInve
|
|||||||
|
|
||||||
Result<?> change(Map<String, Object> parmas);
|
Result<?> change(Map<String, Object> parmas);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载pdf报告
|
||||||
|
* @param params
|
||||||
|
* @param req
|
||||||
|
* @param resp
|
||||||
|
* @throws DocumentException
|
||||||
|
*/
|
||||||
void downloadReport(Map<String, Object> params,HttpServletRequest req, HttpServletResponse resp)throws DocumentException;
|
void downloadReport(Map<String, Object> params,HttpServletRequest req, HttpServletResponse resp)throws DocumentException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载docx报告
|
||||||
|
* @param params
|
||||||
|
* @param req
|
||||||
|
* @param resp
|
||||||
|
*/
|
||||||
|
void downloadDocxReport(Map<String, Object> params, HttpServletRequest req, HttpServletResponse resp);
|
||||||
}
|
}
|
||||||
|
|||||||
+26
-16
@@ -145,38 +145,48 @@ public class ProjectCertificationDirectoryEOServiceImpl extends ServiceImpl<Proj
|
|||||||
return Result.OK(pageList);
|
return Result.OK(pageList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Result<?> queryUserPremissionByProjectLibraryId(String projectLibraryId) {
|
||||||
|
int roleCode = checkUserRole(projectLibraryId);
|
||||||
|
return Result.OK(roleCode);
|
||||||
|
}
|
||||||
|
|
||||||
public int checkUserRole(String projectLibraryId){
|
public int checkUserRole(String projectLibraryId){
|
||||||
int result = -1;
|
int result = -1;
|
||||||
LoginUser currentUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
LoginUser currentUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
||||||
try{
|
try{
|
||||||
|
QueryWrapper<ProjectLawsInventoryEO> inventoryEOQueryWrapperRZ = new QueryWrapper<>();
|
||||||
|
inventoryEOQueryWrapperRZ.lambda().eq(ProjectLawsInventoryEO::getProjectLibraryId,projectLibraryId);
|
||||||
|
inventoryEOQueryWrapperRZ.lambda().eq(ProjectLawsInventoryEO::getHomologationEngineerId,currentUser.getId());
|
||||||
|
Integer lawsInventoryCountRZ = projectLawsInventoryEOMapper.selectCount(inventoryEOQueryWrapperRZ);
|
||||||
|
if(lawsInventoryCountRZ > 0){
|
||||||
|
result = Integer.parseInt(ProjectRoleEnum.HOMOLOGATION_ENGINEER.getValue());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
QueryWrapper<ProjectLibraryBase> projectLibraryBaseQueryWrapper = new QueryWrapper<>();
|
QueryWrapper<ProjectLibraryBase> projectLibraryBaseQueryWrapper = new QueryWrapper<>();
|
||||||
projectLibraryBaseQueryWrapper.lambda().eq(ProjectLibraryBase::getId,projectLibraryId);
|
projectLibraryBaseQueryWrapper.lambda().eq(ProjectLibraryBase::getId,projectLibraryId);
|
||||||
projectLibraryBaseQueryWrapper.lambda().eq(ProjectLibraryBase::getStudioEngineer,currentUser.getId());
|
projectLibraryBaseQueryWrapper.lambda().eq(ProjectLibraryBase::getStudioEngineer,currentUser.getId());
|
||||||
Integer projectLibrayBaseCount = projectLibraryBaseMapper.selectCount(projectLibraryBaseQueryWrapper);
|
Integer projectLibrayBaseCount = projectLibraryBaseMapper.selectCount(projectLibraryBaseQueryWrapper);
|
||||||
if(projectLibrayBaseCount > 0){
|
if(projectLibrayBaseCount > 0){
|
||||||
result = Integer.parseInt(ProjectRoleEnum.STUDIO_ENGINEER.getValue());
|
result = Integer.parseInt(ProjectRoleEnum.STUDIO_ENGINEER.getValue());
|
||||||
}else {
|
return result;
|
||||||
QueryWrapper<ProjectLawsInventoryEO> inventoryEOQueryWrapperFG = new QueryWrapper<>();
|
}
|
||||||
inventoryEOQueryWrapperFG.lambda().eq(ProjectLawsInventoryEO::getProjectLibraryId,projectLibraryId);
|
|
||||||
inventoryEOQueryWrapperFG.lambda().eq(ProjectLawsInventoryEO::getRegulationOwnerId,currentUser.getId());
|
|
||||||
Integer lawsInventoryCountFG = projectLawsInventoryEOMapper.selectCount(inventoryEOQueryWrapperFG);
|
|
||||||
if(lawsInventoryCountFG > 0){
|
|
||||||
result = Integer.parseInt(ProjectRoleEnum.REGULATI_ENGINEER.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
QueryWrapper<ProjectLawsInventoryEO> inventoryEOQueryWrapperRZ = new QueryWrapper<>();
|
QueryWrapper<ProjectLawsInventoryEO> inventoryEOQueryWrapperFG = new QueryWrapper<>();
|
||||||
inventoryEOQueryWrapperRZ.lambda().eq(ProjectLawsInventoryEO::getProjectLibraryId,projectLibraryId);
|
inventoryEOQueryWrapperFG.lambda().eq(ProjectLawsInventoryEO::getProjectLibraryId,projectLibraryId);
|
||||||
inventoryEOQueryWrapperRZ.lambda().eq(ProjectLawsInventoryEO::getHomologationEngineerId,currentUser.getId());
|
inventoryEOQueryWrapperFG.lambda().eq(ProjectLawsInventoryEO::getRegulationOwnerId,currentUser.getId());
|
||||||
Integer lawsInventoryCountRZ = projectLawsInventoryEOMapper.selectCount(inventoryEOQueryWrapperRZ);
|
Integer lawsInventoryCountFG = projectLawsInventoryEOMapper.selectCount(inventoryEOQueryWrapperFG);
|
||||||
if(lawsInventoryCountRZ > 0){
|
if(lawsInventoryCountFG > 0){
|
||||||
result = Integer.parseInt(ProjectRoleEnum.HOMOLOGATION_ENGINEER.getValue());
|
result = Integer.parseInt(ProjectRoleEnum.REGULATI_ENGINEER.getValue());
|
||||||
}
|
return result;
|
||||||
}
|
}
|
||||||
}catch (Exception ex){
|
}catch (Exception ex){
|
||||||
log.error("验证用户在该项目中的角色失败:" + ex.getMessage());
|
log.error("验证用户在该项目中的角色失败:" + ex.getMessage());
|
||||||
throw new JeroBootException("验证用户在该项目中的角色失败!");
|
throw new JeroBootException("验证用户在该项目中的角色失败!");
|
||||||
}
|
}
|
||||||
return result;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+331
-4
@@ -1,11 +1,12 @@
|
|||||||
package com.jero.modules.project.service.impl;
|
package com.jero.modules.project.service.impl;
|
||||||
|
|
||||||
import cn.hutool.core.util.ZipUtil;
|
import cn.hutool.core.util.ZipUtil;
|
||||||
|
//import com.jero.modules.project.util.WordUtil;
|
||||||
|
import cn.hutool.poi.word.DocUtil;
|
||||||
import com.itextpdf.text.*;
|
import com.itextpdf.text.*;
|
||||||
import com.itextpdf.text.Font;
|
import com.itextpdf.text.Font;
|
||||||
import com.itextpdf.text.pdf.*;
|
import com.itextpdf.text.pdf.*;
|
||||||
import com.jero.modules.dummy.entity.DummyInventoryBaseEO;
|
import com.jero.modules.dummy.entity.DummyInventoryBaseEO;
|
||||||
import com.jero.modules.dummy.entity.DummyInventoryInfoEOEn;
|
|
||||||
import com.jero.modules.dummy.enums.DummyInventoryBaseFieldEnum;
|
import com.jero.modules.dummy.enums.DummyInventoryBaseFieldEnum;
|
||||||
import com.jero.modules.dummy.service.impl.DummyInventoryBaseEOServiceImpl;
|
import com.jero.modules.dummy.service.impl.DummyInventoryBaseEOServiceImpl;
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
@@ -26,11 +27,8 @@ import com.jero.common.system.vo.LoginUser;
|
|||||||
import com.jero.common.util.DateUtils;
|
import com.jero.common.util.DateUtils;
|
||||||
import com.jero.modules.document.entity.BussDocumentLibraryEO;
|
import com.jero.modules.document.entity.BussDocumentLibraryEO;
|
||||||
import com.jero.modules.document.service.IBussDocumentLibraryEOService;
|
import com.jero.modules.document.service.IBussDocumentLibraryEOService;
|
||||||
import com.jero.modules.dummy.entity.DummyInventoryBaseEO;
|
|
||||||
import com.jero.modules.dummy.entity.DummyInventoryInfoEO;
|
import com.jero.modules.dummy.entity.DummyInventoryInfoEO;
|
||||||
import com.jero.modules.dummy.enums.DummyInventoryBaseFieldEnum;
|
|
||||||
import com.jero.modules.dummy.service.IDummyInventoryInfoEOService;
|
import com.jero.modules.dummy.service.IDummyInventoryInfoEOService;
|
||||||
import com.jero.modules.dummy.service.impl.DummyInventoryBaseEOServiceImpl;
|
|
||||||
import com.jero.modules.dummy.util.ListDiff;
|
import com.jero.modules.dummy.util.ListDiff;
|
||||||
import com.jero.modules.feishu.service.IFeishuService;
|
import com.jero.modules.feishu.service.IFeishuService;
|
||||||
import com.jero.modules.message.websocket.WebSocket;
|
import com.jero.modules.message.websocket.WebSocket;
|
||||||
@@ -43,6 +41,7 @@ import com.jero.modules.project.mapper.ProjectLibraryBaseMapper;
|
|||||||
import com.jero.modules.project.mapper.ProjectNameInfoEOMapper;
|
import com.jero.modules.project.mapper.ProjectNameInfoEOMapper;
|
||||||
import com.jero.modules.project.mapper.ProjectRelatedPersonnelMapper;
|
import com.jero.modules.project.mapper.ProjectRelatedPersonnelMapper;
|
||||||
import com.jero.modules.project.service.*;
|
import com.jero.modules.project.service.*;
|
||||||
|
import com.jero.modules.project.util.WordUtil;
|
||||||
import com.jero.modules.split.common.FileUnZip;
|
import com.jero.modules.split.common.FileUnZip;
|
||||||
import com.jero.modules.system.entity.SysAnnouncement;
|
import com.jero.modules.system.entity.SysAnnouncement;
|
||||||
import com.jero.modules.system.entity.SysCategory;
|
import com.jero.modules.system.entity.SysCategory;
|
||||||
@@ -54,6 +53,9 @@ import com.jero.modules.system.service.ISysAnnouncementService;
|
|||||||
import com.jero.modules.system.service.ISysUserService;
|
import com.jero.modules.system.service.ISysUserService;
|
||||||
import com.jero.modules.system.service.impl.SysCategoryServiceImpl;
|
import com.jero.modules.system.service.impl.SysCategoryServiceImpl;
|
||||||
import com.jero.modules.system.service.impl.SysDictItemServiceImpl;
|
import com.jero.modules.system.service.impl.SysDictItemServiceImpl;
|
||||||
|
import com.jero.modules.wkflow.entity.ProcessHistoryEO;
|
||||||
|
import com.jero.modules.wkflow.service.IProcessHistoryEOService;
|
||||||
|
import com.jero.modules.wkflow.service.impl.ProcessHistoryEOServiceImpl;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import org.apache.commons.collections4.CollectionUtils;
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
import org.apache.commons.collections4.MapUtils;
|
import org.apache.commons.collections4.MapUtils;
|
||||||
@@ -66,6 +68,18 @@ import org.apache.poi.ss.util.CellRangeAddress;
|
|||||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
import org.apache.shiro.SecurityUtils;
|
import org.apache.shiro.SecurityUtils;
|
||||||
import org.aspectj.util.FileUtil;
|
import org.aspectj.util.FileUtil;
|
||||||
|
import org.docx4j.Docx4J;
|
||||||
|
import org.docx4j.TraversalUtil;
|
||||||
|
import org.docx4j.XmlUtils;
|
||||||
|
import org.docx4j.finders.ClassFinder;
|
||||||
|
import org.docx4j.finders.RangeFinder;
|
||||||
|
import org.docx4j.openpackaging.exceptions.Docx4JException;
|
||||||
|
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
||||||
|
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
|
||||||
|
import org.docx4j.wml.Body;
|
||||||
|
import org.docx4j.wml.CTBookmark;
|
||||||
|
import org.docx4j.wml.Tbl;
|
||||||
|
import org.docx4j.wml.Tr;
|
||||||
import org.jeecgframework.poi.excel.ExcelExportUtil;
|
import org.jeecgframework.poi.excel.ExcelExportUtil;
|
||||||
import org.jeecgframework.poi.excel.entity.ExportParams;
|
import org.jeecgframework.poi.excel.entity.ExportParams;
|
||||||
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
|
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
|
||||||
@@ -166,6 +180,9 @@ public class ProjectLawsInventoryEOServiceImpl extends ServiceImpl<ProjectLawsIn
|
|||||||
@Autowired
|
@Autowired
|
||||||
private SysDictItemMapper sysDictItemMapper;
|
private SysDictItemMapper sysDictItemMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private IProcessHistoryEOService processHistoryEOService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存
|
* 保存
|
||||||
*
|
*
|
||||||
@@ -4118,4 +4135,314 @@ public class ProjectLawsInventoryEOServiceImpl extends ServiceImpl<ProjectLawsIn
|
|||||||
}
|
}
|
||||||
document.add(tableBase);
|
document.add(tableBase);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载docx报告
|
||||||
|
* @param params
|
||||||
|
* @param req
|
||||||
|
* @param resp
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void downloadDocxReport(Map<String, Object> params, HttpServletRequest req, HttpServletResponse resp) {
|
||||||
|
File templateFile = null;
|
||||||
|
File exportFile = null;
|
||||||
|
InputStream fin = null;
|
||||||
|
ServletOutputStream out = null;
|
||||||
|
|
||||||
|
String id = (String) params.get("id");
|
||||||
|
String cut = (String) params.get("cut");
|
||||||
|
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
|
||||||
|
//查询数据
|
||||||
|
QueryWrapper<ProjectLawsInventoryEO> lawsInventoryQueryWrapper = new QueryWrapper<>();
|
||||||
|
lawsInventoryQueryWrapper.lambda().eq(ProjectLawsInventoryEO::getId,id);
|
||||||
|
ProjectLawsInventoryEO projectLawsInventoryEO = this.baseMapper.selectOne(lawsInventoryQueryWrapper);
|
||||||
|
params.put("projectLawsInventoryEO",projectLawsInventoryEO);
|
||||||
|
|
||||||
|
QueryWrapper<ProjectTaskInventoryEO> taskInventoryQueryWrapper = new QueryWrapper<>();
|
||||||
|
taskInventoryQueryWrapper.lambda().eq(ProjectTaskInventoryEO::getProjectLawsInventoryId,id);
|
||||||
|
ProjectTaskInventoryEO projectTaskInventoryEO = projectTaskInventoryEOService.getBaseMapper().selectOne(taskInventoryQueryWrapper);
|
||||||
|
params.put("projectTaskInventoryEO",projectTaskInventoryEO);
|
||||||
|
|
||||||
|
QueryWrapper<ProjectLibraryBase> libraryBaseQueryWrapper = new QueryWrapper<>();
|
||||||
|
libraryBaseQueryWrapper.lambda().eq(ProjectLibraryBase::getId,projectLawsInventoryEO.getProjectLibraryId());
|
||||||
|
ProjectLibraryBase projectLibraryBase = projectLibraryBaseMapper.selectOne(libraryBaseQueryWrapper);
|
||||||
|
params.put("projectLibraryBase",projectLibraryBase);
|
||||||
|
|
||||||
|
QueryWrapper<ProjectNameInfoEO> projectNameQueryWrapper = new QueryWrapper<>();
|
||||||
|
projectNameQueryWrapper.lambda().eq(ProjectNameInfoEO::getId,projectLibraryBase.getProjectNameId());
|
||||||
|
ProjectNameInfoEO projectNameInfoEO = projectNameInfoEOMapper.selectOne(projectNameQueryWrapper);
|
||||||
|
params.put("projectNameInfoEO",projectNameInfoEO);
|
||||||
|
|
||||||
|
|
||||||
|
final Map<String, String> dataMap = new HashMap<>();
|
||||||
|
//对应 CR No
|
||||||
|
dataMap.put("crNo", String.valueOf(new SimpleDateFormat("yyyyMMddHHmmssSSSS").format(new Date())));
|
||||||
|
|
||||||
|
String flowStatus = "";
|
||||||
|
if(StringUtils.isNotEmpty(projectTaskInventoryEO.getVerifyStatus())){
|
||||||
|
flowStatus = projectTaskInventoryEO.getVerifyStatus() + ",";
|
||||||
|
}
|
||||||
|
if(StringUtils.isNotEmpty(projectTaskInventoryEO.getPrehomoStatus())){
|
||||||
|
flowStatus += projectTaskInventoryEO.getPrehomoStatus() + ",";
|
||||||
|
}
|
||||||
|
if(StringUtils.isNotEmpty(projectTaskInventoryEO.getVerifyStatus())){
|
||||||
|
flowStatus += projectTaskInventoryEO.getVerifyStatus() + ",";
|
||||||
|
}
|
||||||
|
if(StringUtils.isNotEmpty(flowStatus)){
|
||||||
|
flowStatus = flowStatus.substring(0,flowStatus.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//对应 crStatus
|
||||||
|
dataMap.put("FLOW_STATUS", StringUtils.isNotEmpty(flowStatus) ? flowStatus : "");
|
||||||
|
//对应 Regulation
|
||||||
|
dataMap.put("REGULATION", StringUtils.isNotEmpty(projectLawsInventoryEO.getSerialNumber()) ? projectLawsInventoryEO.getSerialNumber() : "");
|
||||||
|
//对应 Subject
|
||||||
|
dataMap.put("SUBJECT", StringUtils.isNotEmpty(projectLawsInventoryEO.getTitle()) ? projectLawsInventoryEO.getTitle() : "");
|
||||||
|
//对应 Project
|
||||||
|
dataMap.put("PROJECT_NAME", StringUtils.isNotEmpty(projectNameInfoEO.getProjectName()) ? projectNameInfoEO.getProjectName() : "");
|
||||||
|
|
||||||
|
List<SysDictItem> regionDictList = sysDictItemServiceImpl.selectItemsByDictCode("region");
|
||||||
|
String market = "";
|
||||||
|
if(StringUtils.equals(cut,CutEnum.CN.getValue())){
|
||||||
|
market = regionDictList.stream()
|
||||||
|
.filter(e -> projectLibraryBase.getTargetMarket().contains(e.getItemValue()))
|
||||||
|
.map(SysDictItem::getItemText)
|
||||||
|
.collect(Collectors.joining(","));
|
||||||
|
}else if(StringUtils.equals(cut,CutEnum.EN.getValue())){
|
||||||
|
market = regionDictList.stream()
|
||||||
|
.filter(e -> projectLibraryBase.getTargetMarket().contains(e.getItemValue()))
|
||||||
|
.map(SysDictItem::getEnName)
|
||||||
|
.collect(Collectors.joining(","));
|
||||||
|
}
|
||||||
|
//对应 Market
|
||||||
|
dataMap.put("MARKET", StringUtils.isNotEmpty(market) ? market : "");
|
||||||
|
//对应 nt 数字平台
|
||||||
|
dataMap.put("NT", StringUtils.isNotEmpty(projectNameInfoEO.getDigitalPlatform()) ? projectNameInfoEO.getDigitalPlatform() : "");
|
||||||
|
//对应 np 车型平台
|
||||||
|
dataMap.put("NP", StringUtils.isNotEmpty(projectNameInfoEO.getVehiclePlatform()) ? projectNameInfoEO.getVehiclePlatform() : "");
|
||||||
|
String processEndTimeStr = "";
|
||||||
|
if(projectTaskInventoryEO.getProcessEndTime() != null){
|
||||||
|
processEndTimeStr = sdf.format(projectTaskInventoryEO.getProcessEndTime());
|
||||||
|
}
|
||||||
|
dataMap.put("COMPLETION_DATE", StringUtils.isNotEmpty(processEndTimeStr) ? processEndTimeStr : "");
|
||||||
|
|
||||||
|
String dutyTerritory = projectLawsInventoryEO.getDutyTerritory();
|
||||||
|
String finalDutyTerritory = dutyTerritory;
|
||||||
|
List<SysDictItem> dutyTerritoryDictItemList = sysDictItemServiceImpl.selectItemsByDictCode("duty_territory");
|
||||||
|
String dutyTerritoryName = "";
|
||||||
|
if(StringUtils.equals(cut,CutEnum.CN.getValue())){
|
||||||
|
dutyTerritoryName = dutyTerritoryDictItemList.stream()
|
||||||
|
.filter(e -> finalDutyTerritory.contains(e.getItemValue()))
|
||||||
|
.map(SysDictItem::getItemText)
|
||||||
|
.collect(Collectors.joining(","));
|
||||||
|
}else if(StringUtils.equals(cut,CutEnum.EN.getValue())){
|
||||||
|
dutyTerritoryName = dutyTerritoryDictItemList.stream()
|
||||||
|
.filter(e -> finalDutyTerritory.contains(e.getItemValue()))
|
||||||
|
.map(SysDictItem::getEnName)
|
||||||
|
.collect(Collectors.joining(","));
|
||||||
|
}
|
||||||
|
dataMap.put("RESPONSIBLE_FIELD", StringUtils.isNotEmpty(dutyTerritoryName) ? dutyTerritoryName : "");
|
||||||
|
|
||||||
|
String studioEngineer = projectLibraryBase.getStudioEngineer();
|
||||||
|
QueryWrapper<SysUser> queryStudioUserWrapper = new QueryWrapper<>();
|
||||||
|
queryStudioUserWrapper.lambda().eq(SysUser::getId,studioEngineer);
|
||||||
|
SysUser studioUserInfo = sysUserMapper.selectOne(queryStudioUserWrapper);
|
||||||
|
dataMap.put("STUDIO", StringUtils.isNotEmpty(studioUserInfo.getUsername()) ? studioUserInfo.getUsername() : "");
|
||||||
|
|
||||||
|
String engineeringInterfacePerson = projectLawsInventoryEO.getEngineeringInterfacePerson();
|
||||||
|
QueryWrapper<SysUser> queryEngineeringInterfaceUserWrapper = new QueryWrapper<>();
|
||||||
|
queryEngineeringInterfaceUserWrapper.lambda().eq(SysUser::getId,engineeringInterfacePerson);
|
||||||
|
SysUser engineeringInterfaceInfo = sysUserMapper.selectOne(queryEngineeringInterfaceUserWrapper);
|
||||||
|
dataMap.put("ENGINEERING", StringUtils.isNotEmpty(engineeringInterfaceInfo.getUsername()) ? engineeringInterfaceInfo.getUsername() : "");
|
||||||
|
|
||||||
|
String homologationEngineerId = projectLawsInventoryEO.getHomologationEngineerId();
|
||||||
|
QueryWrapper<SysUser> queryHomologationEngineerUserWrapper = new QueryWrapper<>();
|
||||||
|
queryHomologationEngineerUserWrapper.lambda().eq(SysUser::getId,homologationEngineerId);
|
||||||
|
SysUser homologationEngineerInfo = sysUserMapper.selectOne(queryHomologationEngineerUserWrapper);
|
||||||
|
dataMap.put("HOMOLOGATION", StringUtils.isNotEmpty(homologationEngineerInfo.getUsername()) ? homologationEngineerInfo.getUsername() : "");
|
||||||
|
|
||||||
|
String regulationOwnerId = projectLawsInventoryEO.getRegulationOwnerId();
|
||||||
|
QueryWrapper<SysUser> queryRegulationOwnerUserWrapper = new QueryWrapper<>();
|
||||||
|
queryRegulationOwnerUserWrapper.lambda().eq(SysUser::getId,regulationOwnerId);
|
||||||
|
SysUser regulationOwnerInfo = sysUserMapper.selectOne(queryRegulationOwnerUserWrapper);
|
||||||
|
dataMap.put("REGULATION", StringUtils.isNotEmpty(regulationOwnerInfo.getUsername()) ? regulationOwnerInfo.getUsername() : "");
|
||||||
|
|
||||||
|
templateFile = new File(exportPdfTempPath + "temp/ComplianceReportTemplate.docx");
|
||||||
|
if(templateFile != null){
|
||||||
|
try {
|
||||||
|
String uuid = UUID.randomUUID().toString();
|
||||||
|
// final String TEMP_FILE_PATH = System.getProperty("os.name").toLowerCase().contains("windows") ? System.getProperty("user.dir") + File.separator : "/home/admin/tempfile/";
|
||||||
|
final String TEMP_FILE_PATH = exportPdfTempPath + "exportFile/";
|
||||||
|
|
||||||
|
File pathFile = new File(TEMP_FILE_PATH);
|
||||||
|
if (!pathFile.exists()) {
|
||||||
|
pathFile.mkdirs();
|
||||||
|
}
|
||||||
|
String outFile = TEMP_FILE_PATH + uuid + ".docx";
|
||||||
|
exportFile = new File(outFile);
|
||||||
|
FileOutputStream os = new FileOutputStream(outFile);
|
||||||
|
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new FileInputStream(templateFile));
|
||||||
|
|
||||||
|
//验证符合性流程实例id
|
||||||
|
String verifyPId = projectTaskInventoryEO.getVerifyPId();
|
||||||
|
if(StringUtils.isNotEmpty(verifyPId)){
|
||||||
|
//Compliance Attachments 验证符合性流程里最后通过审批的“工程确认”中的附件
|
||||||
|
setTableContent(1,wordMLPackage,params,verifyPId);
|
||||||
|
//Validation Compliance Check Approvals(验证符合性确认的审批历史数据)
|
||||||
|
setTableContent(4,wordMLPackage,params,verifyPId);
|
||||||
|
}
|
||||||
|
//设计符合性流程实例id
|
||||||
|
String designPId = projectTaskInventoryEO.getDesignPId();
|
||||||
|
if(StringUtils.isNotEmpty(designPId)){
|
||||||
|
//Design Compliance Check Approvals (设计符合性确认的审批历史数据)
|
||||||
|
setTableContent(3,wordMLPackage,params,designPId);
|
||||||
|
}
|
||||||
|
|
||||||
|
WordUtil.replaceVariable(wordMLPackage, dataMap);
|
||||||
|
Docx4J.save(wordMLPackage, os);
|
||||||
|
|
||||||
|
//将替换后的文档输出出去。
|
||||||
|
fin = new FileInputStream(outFile);
|
||||||
|
resp.setCharacterEncoding("utf-8");
|
||||||
|
resp.setContentType("application/docx");
|
||||||
|
|
||||||
|
String filename = "";
|
||||||
|
if(StringUtils.equals(cut,CutEnum.CN.getValue())){
|
||||||
|
filename = "法规清单报告";
|
||||||
|
}else if(StringUtils.equals(cut,CutEnum.EN.getValue())){
|
||||||
|
filename = "Compliance List report";
|
||||||
|
}
|
||||||
|
// 设置浏览器以下载的方式处理该文件
|
||||||
|
if (req.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
|
||||||
|
filename = URLEncoder.encode(filename, "UTF-8");
|
||||||
|
} else {
|
||||||
|
filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");
|
||||||
|
}
|
||||||
|
resp.setHeader("Content-Disposition", "attachment;filename=" + filename + ".docx");
|
||||||
|
out = resp.getOutputStream();
|
||||||
|
|
||||||
|
// 缓冲区
|
||||||
|
byte[] buffer = new byte[512];
|
||||||
|
int bytesToRead = -1;
|
||||||
|
|
||||||
|
// 通过循环将读入的Word文件的内容输出到浏览器中
|
||||||
|
while ((bytesToRead = fin.read(buffer)) != -1) {
|
||||||
|
out.write(buffer, 0, bytesToRead);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (fin != null){
|
||||||
|
fin.close();
|
||||||
|
}
|
||||||
|
if (out != null){
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (exportFile != null){
|
||||||
|
// 删除导出去的文件
|
||||||
|
exportFile.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
throw new JeroBootException("路径 :" + exportPdfTempPath + "temp/ComplianceReportTemplate.docx" + " 中没有找到文件");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置表内容
|
||||||
|
* @param tableIndex 表的下标
|
||||||
|
* @param wordMLPackage
|
||||||
|
* @param params
|
||||||
|
* @param pId 流程实例id
|
||||||
|
*/
|
||||||
|
public void setTableContent(int tableIndex,WordprocessingMLPackage wordMLPackage, Map<String, Object> params,String pId){
|
||||||
|
try{
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
|
||||||
|
List<Map<String , Object>> dataList = new ArrayList<>();
|
||||||
|
// 设置数据
|
||||||
|
if(tableIndex == 1){
|
||||||
|
QueryWrapper<ProjectTaskInventoryFeedbackEO> taskFeedbackQueryWrapper = new QueryWrapper<>();
|
||||||
|
taskFeedbackQueryWrapper.lambda().eq(ProjectTaskInventoryFeedbackEO::getActiProcInstId,pId);
|
||||||
|
List<ProjectTaskInventoryFeedbackEO> projectTaskInventoryFeedbackEOList = projectTaskInventoryFeedbackEOService.getBaseMapper().selectList(taskFeedbackQueryWrapper);
|
||||||
|
if(CollectionUtils.isNotEmpty(projectTaskInventoryFeedbackEOList)){
|
||||||
|
List<String> fileIdList = projectTaskInventoryFeedbackEOList.stream().map(ProjectTaskInventoryFeedbackEO::getFileId).distinct().collect(Collectors.toList());
|
||||||
|
String fileId = StringUtils.join(fileIdList,",");
|
||||||
|
List<OSSFile> fileList = iOSSFileService.getFileInfos(fileId);
|
||||||
|
if(CollectionUtils.isNotEmpty(fileList)){
|
||||||
|
fileList.forEach(file -> {
|
||||||
|
Map<String,Object> fileMap = new HashMap<>();
|
||||||
|
fileMap.put("item.fileName", StringUtils.isNotEmpty(file.getFileName()) ? file.getFileName() : "");
|
||||||
|
fileMap.put("item.filePath", StringUtils.isNotEmpty(file.getUrl()) ? file.getUrl() : "");
|
||||||
|
dataList.add(fileMap);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else if(tableIndex == 3){
|
||||||
|
params.put("actiProcInstId",pId);
|
||||||
|
List<ProcessHistoryEO> processHistoryEOList = processHistoryEOService.queryList(params);
|
||||||
|
if(CollectionUtils.isNotEmpty(processHistoryEOList)){
|
||||||
|
processHistoryEOList.forEach(processHistory -> {
|
||||||
|
String createTime = sdf.format(processHistory.getCreateTime());
|
||||||
|
|
||||||
|
Map<String,Object> designMap = new HashMap<>();
|
||||||
|
designMap.put("design.result", StringUtils.isNotEmpty(processHistory.getReviewResult()) ? processHistory.getReviewResult() : "");
|
||||||
|
designMap.put("design.opinion", StringUtils.isNotEmpty(processHistory.getApprovalOpinion()) ? processHistory.getApprovalOpinion() : "");
|
||||||
|
designMap.put("design.node", StringUtils.isNotEmpty(processHistory.getName()) ? processHistory.getName() : "");
|
||||||
|
designMap.put("design.operator", StringUtils.isNotEmpty(processHistory.getAssignee()) ? processHistory.getAssignee() :"");
|
||||||
|
designMap.put("design.dateTime", StringUtils.isNotEmpty(createTime) ? createTime : "");
|
||||||
|
dataList.add(designMap);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}else if(tableIndex == 4){
|
||||||
|
params.put("actiProcInstId",pId);
|
||||||
|
List<ProcessHistoryEO> processHistoryEOList = processHistoryEOService.queryList(params);
|
||||||
|
if(CollectionUtils.isNotEmpty(processHistoryEOList)){
|
||||||
|
processHistoryEOList.forEach(processHistory -> {
|
||||||
|
String createTime = sdf.format(processHistory.getCreateTime());
|
||||||
|
|
||||||
|
Map<String,Object> verifyMap = new HashMap<>();
|
||||||
|
verifyMap.put("verify.result", StringUtils.isNotEmpty(processHistory.getReviewResult()) ? processHistory.getReviewResult() : "");
|
||||||
|
verifyMap.put("verify.opinion", StringUtils.isNotEmpty(processHistory.getApprovalOpinion()) ? processHistory.getApprovalOpinion() : "");
|
||||||
|
verifyMap.put("verify.node", StringUtils.isNotEmpty(processHistory.getName()) ? processHistory.getName() : "");
|
||||||
|
verifyMap.put("verify.operator", StringUtils.isNotEmpty(processHistory.getAssignee()) ? processHistory.getAssignee() :"");
|
||||||
|
verifyMap.put("verify.dateTime", StringUtils.isNotEmpty(createTime) ? createTime : "");
|
||||||
|
dataList.add(verifyMap);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//构造循环列表的数据
|
||||||
|
ClassFinder find = new ClassFinder(Tbl.class);
|
||||||
|
new TraversalUtil(wordMLPackage.getMainDocumentPart().getContent(), find);
|
||||||
|
Tbl table = (Tbl) find.results.get(tableIndex);
|
||||||
|
//第二行约定为模板
|
||||||
|
Tr dynamicTr = (Tr) table.getContent().get(1);
|
||||||
|
//获取模板行的xml数据
|
||||||
|
String dynamicTrXml = XmlUtils.marshaltoString(dynamicTr);
|
||||||
|
|
||||||
|
for (Map<String, Object> mapList : dataList) {
|
||||||
|
Tr newTr = (Tr) XmlUtils.unmarshallFromTemplate(dynamicTrXml, mapList);//填充模板行数据
|
||||||
|
table.getContent().add(newTr);
|
||||||
|
}
|
||||||
|
//删除模板行的占位行
|
||||||
|
table.getContent().remove(1);
|
||||||
|
}catch (Exception ex){
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static WordprocessingMLPackage getTemplate(String name)throws Docx4JException,FileNotFoundException {
|
||||||
|
WordprocessingMLPackage template = WordprocessingMLPackage.load(new FileInputStream(new File(name)));
|
||||||
|
return template;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-4
@@ -527,10 +527,12 @@ public class ProjectLibraryBaseServiceImpl extends ServiceImpl<ProjectLibraryBas
|
|||||||
if(CollectionUtils.isNotEmpty(datas)){
|
if(CollectionUtils.isNotEmpty(datas)){
|
||||||
for (Map<String, Object> data : datas) {
|
for (Map<String, Object> data : datas) {
|
||||||
List<SysDictItem> dutyTerritory = dictItemList.stream().filter(dict -> StringUtils.equals(dict.getItemValue(), data.get("dutyTerritory").toString())).collect(Collectors.toList());
|
List<SysDictItem> dutyTerritory = dictItemList.stream().filter(dict -> StringUtils.equals(dict.getItemValue(), data.get("dutyTerritory").toString())).collect(Collectors.toList());
|
||||||
if(StringUtils.equals(cut,CutEnum.CN.getValue())){
|
if(CollectionUtils.isNotEmpty(dutyTerritory)){
|
||||||
data.put("dutyTerritory",dutyTerritory.get(0).getItemText());
|
if(StringUtils.equals(cut,CutEnum.CN.getValue())){
|
||||||
}else if(StringUtils.equals(cut,CutEnum.EN.getValue())){
|
data.put("dutyTerritory",dutyTerritory.get(0).getItemText());
|
||||||
data.put("dutyTerritory",dutyTerritory.get(0).getEnName());
|
}else if(StringUtils.equals(cut,CutEnum.EN.getValue())){
|
||||||
|
data.put("dutyTerritory",dutyTerritory.get(0).getEnName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+246
@@ -0,0 +1,246 @@
|
|||||||
|
package com.jero.modules.project.util;
|
||||||
|
|
||||||
|
|
||||||
|
import org.docx4j.XmlUtils;
|
||||||
|
import org.docx4j.jaxb.Context;
|
||||||
|
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
||||||
|
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
|
||||||
|
import org.docx4j.wml.Document;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.xml.bind.JAXBContext;
|
||||||
|
import javax.xml.bind.JAXBException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关于文件操作的工具类
|
||||||
|
*
|
||||||
|
* @author kaizen
|
||||||
|
* @date 2018-10-23 17:21:36
|
||||||
|
*/
|
||||||
|
public final class Docx4jUtils {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(Docx4jUtils.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 替换变量并下载word文档
|
||||||
|
*
|
||||||
|
* @param inputStream
|
||||||
|
* @param map
|
||||||
|
* @param response
|
||||||
|
* @param fileName
|
||||||
|
*/
|
||||||
|
public static void downloadDocUseDoc4j(InputStream inputStream, Map<String, String> map,
|
||||||
|
HttpServletResponse response, String fileName) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 设置响应头
|
||||||
|
fileName = URLEncoder.encode(fileName, "UTF-8");
|
||||||
|
response.setContentType("application/octet-stream;charset=UTF-8");
|
||||||
|
response.setCharacterEncoding("utf-8");
|
||||||
|
response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".docx");
|
||||||
|
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
|
||||||
|
|
||||||
|
OutputStream outs = response.getOutputStream();
|
||||||
|
Docx4jUtils.replaceDocUseDoc4j(inputStream,map,outs);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 替换变量并输出word文档
|
||||||
|
* @param inputStream
|
||||||
|
* @param map
|
||||||
|
* @param outputStream
|
||||||
|
*/
|
||||||
|
public static void replaceDocUseDoc4j(InputStream inputStream, Map<String, String> map,
|
||||||
|
OutputStream outputStream) {
|
||||||
|
try {
|
||||||
|
WordprocessingMLPackage doc = WordprocessingMLPackage.load(inputStream);
|
||||||
|
MainDocumentPart mainDocumentPart = doc.getMainDocumentPart();
|
||||||
|
if (null != map && !map.isEmpty()) {
|
||||||
|
// 将${}里的内容结构层次替换为一层
|
||||||
|
Docx4jUtils .cleanDocumentPart(mainDocumentPart);
|
||||||
|
// 替换文本内容
|
||||||
|
mainDocumentPart.variableReplace((HashMap<String, String>) map);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 输出word文件
|
||||||
|
doc.save(outputStream);
|
||||||
|
outputStream.flush();
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cleanDocumentPart
|
||||||
|
*
|
||||||
|
* @param documentPart
|
||||||
|
*/
|
||||||
|
public static boolean cleanDocumentPart(MainDocumentPart documentPart) throws Exception {
|
||||||
|
if (documentPart == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Document document = documentPart.getContents();
|
||||||
|
String wmlTemplate =
|
||||||
|
XmlUtils.marshaltoString(document, true, false, Context.jc);
|
||||||
|
document = (Document) XmlUtils.unwrap(DocxVariableClearUtils.doCleanDocumentPart(wmlTemplate, Context.jc));
|
||||||
|
documentPart.setContents(document);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清扫 docx4j 模板变量字符,通常以${variable}形式
|
||||||
|
* <p>
|
||||||
|
* XXX: 主要在上传模板时处理一下, 后续
|
||||||
|
*
|
||||||
|
* @author liliang
|
||||||
|
* @since 2018-11-07
|
||||||
|
*/
|
||||||
|
private static class DocxVariableClearUtils {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 去任意XML标签
|
||||||
|
*/
|
||||||
|
private static final Pattern XML_PATTERN = Pattern.compile("<[^>]*>");
|
||||||
|
|
||||||
|
private DocxVariableClearUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* start符号
|
||||||
|
*/
|
||||||
|
private static final char PREFIX = '$';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 中包含
|
||||||
|
*/
|
||||||
|
private static final char LEFT_BRACE = '{';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 结尾
|
||||||
|
*/
|
||||||
|
private static final char RIGHT_BRACE = '}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 未开始
|
||||||
|
*/
|
||||||
|
private static final int NONE_START = -1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 未开始
|
||||||
|
*/
|
||||||
|
private static final int NONE_START_INDEX = -1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开始
|
||||||
|
*/
|
||||||
|
private static final int PREFIX_STATUS = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 左括号
|
||||||
|
*/
|
||||||
|
private static final int LEFT_BRACE_STATUS = 2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 右括号
|
||||||
|
*/
|
||||||
|
private static final int RIGHT_BRACE_STATUS = 3;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* doCleanDocumentPart
|
||||||
|
*
|
||||||
|
* @param wmlTemplate
|
||||||
|
* @param jc
|
||||||
|
* @return
|
||||||
|
* @throws JAXBException
|
||||||
|
*/
|
||||||
|
private static Object doCleanDocumentPart(String wmlTemplate, JAXBContext jc) throws JAXBException {
|
||||||
|
// 进入变量块位置
|
||||||
|
int curStatus = NONE_START;
|
||||||
|
// 开始位置
|
||||||
|
int keyStartIndex = NONE_START_INDEX;
|
||||||
|
// 当前位置
|
||||||
|
int curIndex = 0;
|
||||||
|
char[] textCharacters = wmlTemplate.toCharArray();
|
||||||
|
StringBuilder documentBuilder = new StringBuilder(textCharacters.length);
|
||||||
|
documentBuilder.append(textCharacters);
|
||||||
|
// 新文档
|
||||||
|
StringBuilder newDocumentBuilder = new StringBuilder(textCharacters.length);
|
||||||
|
// 最后一次写位置
|
||||||
|
int lastWriteIndex = 0;
|
||||||
|
for (char c : textCharacters) {
|
||||||
|
switch (c) {
|
||||||
|
case PREFIX:
|
||||||
|
// TODO 不管其何状态直接修改指针,这也意味着变量名称里面不能有PREFIX
|
||||||
|
keyStartIndex = curIndex;
|
||||||
|
curStatus = PREFIX_STATUS;
|
||||||
|
break;
|
||||||
|
case LEFT_BRACE:
|
||||||
|
if (curStatus == PREFIX_STATUS) {
|
||||||
|
curStatus = LEFT_BRACE_STATUS;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case RIGHT_BRACE:
|
||||||
|
if (curStatus == LEFT_BRACE_STATUS) {
|
||||||
|
// 接上之前的字符
|
||||||
|
newDocumentBuilder.append(documentBuilder.substring(lastWriteIndex, keyStartIndex));
|
||||||
|
// 结束位置
|
||||||
|
int keyEndIndex = curIndex + 1;
|
||||||
|
// 替换
|
||||||
|
String rawKey = documentBuilder.substring(keyStartIndex, keyEndIndex);
|
||||||
|
// 干掉多余标签
|
||||||
|
String mappingKey = XML_PATTERN.matcher(rawKey).replaceAll("");
|
||||||
|
if (!mappingKey.equals(rawKey)) {
|
||||||
|
char[] rawKeyChars = rawKey.toCharArray();
|
||||||
|
// 保留原格式
|
||||||
|
StringBuilder rawStringBuilder = new StringBuilder(rawKey.length());
|
||||||
|
// 去掉变量引用字符
|
||||||
|
for (char rawChar : rawKeyChars) {
|
||||||
|
if (rawChar == PREFIX || rawChar == LEFT_BRACE || rawChar == RIGHT_BRACE) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
rawStringBuilder.append(rawChar);
|
||||||
|
}
|
||||||
|
// FIXME 要求变量连在一起
|
||||||
|
String variable = mappingKey.substring(2, mappingKey.length() - 1);
|
||||||
|
int variableStart = rawStringBuilder.indexOf(variable);
|
||||||
|
if (variableStart > 0) {
|
||||||
|
rawStringBuilder = rawStringBuilder.replace(variableStart, variableStart + variable.length(), mappingKey);
|
||||||
|
}
|
||||||
|
newDocumentBuilder.append(rawStringBuilder.toString());
|
||||||
|
} else {
|
||||||
|
newDocumentBuilder.append(mappingKey);
|
||||||
|
}
|
||||||
|
lastWriteIndex = keyEndIndex;
|
||||||
|
|
||||||
|
curStatus = NONE_START;
|
||||||
|
keyStartIndex = NONE_START_INDEX;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
curIndex++;
|
||||||
|
}
|
||||||
|
// 余部
|
||||||
|
if (lastWriteIndex < documentBuilder.length()) {
|
||||||
|
newDocumentBuilder.append(documentBuilder.substring(lastWriteIndex));
|
||||||
|
}
|
||||||
|
return XmlUtils.unmarshalString(newDocumentBuilder.toString(), jc);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+315
@@ -0,0 +1,315 @@
|
|||||||
|
package com.jero.modules.project.util;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.docx4j.Docx4J;
|
||||||
|
import org.docx4j.TraversalUtil;
|
||||||
|
import org.docx4j.XmlUtils;
|
||||||
|
import org.docx4j.convert.out.FOSettings;
|
||||||
|
import org.docx4j.convert.out.HTMLSettings;
|
||||||
|
import org.docx4j.dml.wordprocessingDrawing.Inline;
|
||||||
|
import org.docx4j.finders.ClassFinder;
|
||||||
|
import org.docx4j.finders.RangeFinder;
|
||||||
|
import org.docx4j.fonts.IdentityPlusMapper;
|
||||||
|
import org.docx4j.fonts.Mapper;
|
||||||
|
import org.docx4j.fonts.PhysicalFonts;
|
||||||
|
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
||||||
|
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
|
||||||
|
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
|
||||||
|
import org.docx4j.wml.Body;
|
||||||
|
import org.docx4j.wml.CTBookmark;
|
||||||
|
import org.docx4j.wml.Document;
|
||||||
|
import org.docx4j.wml.Drawing;
|
||||||
|
import org.docx4j.wml.ObjectFactory;
|
||||||
|
import org.docx4j.wml.P;
|
||||||
|
import org.docx4j.wml.R;
|
||||||
|
import org.docx4j.wml.Tbl;
|
||||||
|
import org.docx4j.wml.Text;
|
||||||
|
import org.docx4j.wml.Tr;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.util.FileCopyUtils;
|
||||||
|
|
||||||
|
public class WordUtil {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(WordUtil.class);
|
||||||
|
|
||||||
|
public WordUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void replaceVariable(String templatePath, Map<String, String> map, String outPath) throws Exception {
|
||||||
|
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(templatePath));
|
||||||
|
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
|
||||||
|
Docx4jUtils.cleanDocumentPart(documentPart);
|
||||||
|
documentPart.variableReplace((HashMap<String, String>) map);
|
||||||
|
Docx4J.save(wordMLPackage, new File(outPath));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void replaceVariable(InputStream inputStream, Map<String, String> map, OutputStream outputStream) throws Exception {
|
||||||
|
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(inputStream);
|
||||||
|
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
|
||||||
|
Docx4jUtils.cleanDocumentPart(documentPart);
|
||||||
|
documentPart.variableReplace((HashMap<String, String>) map);
|
||||||
|
Docx4J.save(wordMLPackage, outputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void replaceVariable(WordprocessingMLPackage wordMLPackage, Map<String, String> map) throws Exception {
|
||||||
|
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
|
||||||
|
Docx4jUtils.cleanDocumentPart(documentPart);
|
||||||
|
documentPart.variableReplace((HashMap<String, String>) map);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void replaceTable(String templatePath, int tableNum, int rowNum, List<Map<String, Object>> dataList, String outPath) throws Exception {
|
||||||
|
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(templatePath));
|
||||||
|
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
|
||||||
|
Docx4jUtils.cleanDocumentPart(mainDocumentPart);
|
||||||
|
ClassFinder find = new ClassFinder(Tbl.class);
|
||||||
|
new TraversalUtil(mainDocumentPart.getContent(), find);
|
||||||
|
Tbl table = (Tbl)find.results.get(tableNum - 1);
|
||||||
|
Tr dynamicTr = (Tr)table.getContent().get(rowNum - 1);
|
||||||
|
String dynamicTrXml = XmlUtils.marshaltoString(dynamicTr);
|
||||||
|
Iterator var11 = dataList.iterator();
|
||||||
|
|
||||||
|
while(var11.hasNext()) {
|
||||||
|
Map<String, Object> dataMap = (Map)var11.next();
|
||||||
|
Tr newTr = (Tr)XmlUtils.unmarshallFromTemplate(dynamicTrXml, dataMap);
|
||||||
|
table.getContent().add(newTr);
|
||||||
|
}
|
||||||
|
|
||||||
|
table.getContent().remove(rowNum - 1);
|
||||||
|
Docx4J.save(wordMLPackage, new File(outPath));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void replaceTable(InputStream inputStream, int tableNum, int rowNum, List<Map<String, Object>> dataList, OutputStream outputStream) throws Exception {
|
||||||
|
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(inputStream);
|
||||||
|
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
|
||||||
|
Docx4jUtils.cleanDocumentPart(mainDocumentPart);
|
||||||
|
ClassFinder find = new ClassFinder(Tbl.class);
|
||||||
|
new TraversalUtil(mainDocumentPart.getContent(), find);
|
||||||
|
Tbl table = (Tbl)find.results.get(tableNum - 1);
|
||||||
|
Tr dynamicTr = (Tr)table.getContent().get(rowNum - 1);
|
||||||
|
String dynamicTrXml = XmlUtils.marshaltoString(dynamicTr);
|
||||||
|
Iterator var11 = dataList.iterator();
|
||||||
|
|
||||||
|
while(var11.hasNext()) {
|
||||||
|
Map<String, Object> dataMap = (Map)var11.next();
|
||||||
|
Tr newTr = (Tr)XmlUtils.unmarshallFromTemplate(dynamicTrXml, dataMap);
|
||||||
|
table.getContent().add(newTr);
|
||||||
|
}
|
||||||
|
|
||||||
|
table.getContent().remove(rowNum - 1);
|
||||||
|
Docx4J.save(wordMLPackage, outputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void replaceTable(WordprocessingMLPackage wordMLPackage, int tableNum, int rowNum, List<Map<String, Object>> dataList) throws Exception {
|
||||||
|
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
|
||||||
|
Docx4jUtils.cleanDocumentPart(mainDocumentPart);
|
||||||
|
ClassFinder find = new ClassFinder(Tbl.class);
|
||||||
|
new TraversalUtil(mainDocumentPart.getContent(), find);
|
||||||
|
Tbl table = (Tbl)find.results.get(tableNum - 1);
|
||||||
|
Tr dynamicTr = (Tr)table.getContent().get(rowNum - 1);
|
||||||
|
String dynamicTrXml = XmlUtils.marshaltoString(dynamicTr);
|
||||||
|
Iterator var9 = dataList.iterator();
|
||||||
|
|
||||||
|
while(var9.hasNext()) {
|
||||||
|
Map<String, Object> dataMap = (Map)var9.next();
|
||||||
|
Tr newTr = (Tr)XmlUtils.unmarshallFromTemplate(dynamicTrXml, dataMap);
|
||||||
|
table.getContent().add(newTr);
|
||||||
|
}
|
||||||
|
|
||||||
|
table.getContent().remove(rowNum - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void replacePicture(WordprocessingMLPackage wordMLPackage, String bookmarkName, InputStream inputStream) throws Exception {
|
||||||
|
try {
|
||||||
|
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
|
||||||
|
Document wmlDoc = (Document)mainDocumentPart.getJaxbElement();
|
||||||
|
Body body = wmlDoc.getBody();
|
||||||
|
List<Object> paragraphs = body.getContent();
|
||||||
|
RangeFinder rt = new RangeFinder("CTBookmark", "CTMarkupRange");
|
||||||
|
new TraversalUtil(paragraphs, rt);
|
||||||
|
Iterator var8 = rt.getStarts().iterator();
|
||||||
|
|
||||||
|
while(var8.hasNext()) {
|
||||||
|
CTBookmark bm = (CTBookmark)var8.next();
|
||||||
|
if (bm.getName().equals(bookmarkName)) {
|
||||||
|
byte[] bytes = IOUtils.toByteArray(inputStream);
|
||||||
|
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
|
||||||
|
Inline inline = imagePart.createImageInline("", "", 0, 1, true);
|
||||||
|
P p = (P)((P)bm.getParent());
|
||||||
|
ObjectFactory factory = new ObjectFactory();
|
||||||
|
R run = factory.createR();
|
||||||
|
Drawing drawing = factory.createDrawing();
|
||||||
|
drawing.getAnchorOrInline().add(inline);
|
||||||
|
run.getContent().add(drawing);
|
||||||
|
p.getContent().add(run);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception var17) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void replaceForTemplate(String templatePath, Map<String, String> map, int tableNum, List<Map<String, Object>> dataList, String outPath) throws Exception {
|
||||||
|
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(templatePath));
|
||||||
|
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
|
||||||
|
Docx4jUtils.cleanDocumentPart(mainDocumentPart);
|
||||||
|
ClassFinder find = new ClassFinder(Tbl.class);
|
||||||
|
new TraversalUtil(mainDocumentPart.getContent(), find);
|
||||||
|
Tbl table = (Tbl)find.results.get(tableNum - 1);
|
||||||
|
Tr dynamicTr = (Tr)table.getContent().get(1);
|
||||||
|
String dynamicTrXml = XmlUtils.marshaltoString(dynamicTr);
|
||||||
|
Iterator var11 = dataList.iterator();
|
||||||
|
|
||||||
|
while(var11.hasNext()) {
|
||||||
|
Map<String, Object> dataMap = (Map)var11.next();
|
||||||
|
Tr newTr = (Tr)XmlUtils.unmarshallFromTemplate(dynamicTrXml, dataMap);
|
||||||
|
table.getContent().add(newTr);
|
||||||
|
}
|
||||||
|
|
||||||
|
table.getContent().remove(1);
|
||||||
|
wordMLPackage.getMainDocumentPart().variableReplace((HashMap<String, String>) map);
|
||||||
|
Docx4J.save(wordMLPackage, new File(outPath));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void replaceForTemplate(InputStream inputStream, Map<String, String> map, int tableNum, List<Map<String, Object>> dataList, OutputStream outputStream) throws Exception {
|
||||||
|
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(inputStream);
|
||||||
|
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
|
||||||
|
Docx4jUtils.cleanDocumentPart(mainDocumentPart);
|
||||||
|
ClassFinder find = new ClassFinder(Tbl.class);
|
||||||
|
new TraversalUtil(mainDocumentPart.getContent(), find);
|
||||||
|
Tbl table = (Tbl)find.results.get(tableNum - 1);
|
||||||
|
Tr dynamicTr = (Tr)table.getContent().get(1);
|
||||||
|
String dynamicTrXml = XmlUtils.marshaltoString(dynamicTr);
|
||||||
|
Iterator var11 = dataList.iterator();
|
||||||
|
|
||||||
|
while(var11.hasNext()) {
|
||||||
|
Map<String, Object> dataMap = (Map)var11.next();
|
||||||
|
Tr newTr = (Tr)XmlUtils.unmarshallFromTemplate(dynamicTrXml, dataMap);
|
||||||
|
table.getContent().add(newTr);
|
||||||
|
}
|
||||||
|
|
||||||
|
table.getContent().remove(1);
|
||||||
|
wordMLPackage.getMainDocumentPart().variableReplace((HashMap<String, String>) map);
|
||||||
|
Docx4J.save(wordMLPackage, outputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void toPDF(InputStream inputStream, OutputStream outputStream) throws Exception {
|
||||||
|
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(inputStream);
|
||||||
|
Mapper fontMapper = new IdentityPlusMapper();
|
||||||
|
wordMLPackage.setFontMapper(fontMapper);
|
||||||
|
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"));
|
||||||
|
FOSettings foSettings = Docx4J.createFOSettings();
|
||||||
|
foSettings.setWmlPackage(wordMLPackage);
|
||||||
|
Docx4J.toFO(foSettings, outputStream, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void toPDF(WordprocessingMLPackage wordMLPackage, OutputStream outputStream) throws Exception {
|
||||||
|
Mapper fontMapper = new IdentityPlusMapper();
|
||||||
|
wordMLPackage.setFontMapper(fontMapper);
|
||||||
|
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"));
|
||||||
|
FOSettings foSettings = Docx4J.createFOSettings();
|
||||||
|
foSettings.setWmlPackage(wordMLPackage);
|
||||||
|
Docx4J.toFO(foSettings, outputStream, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void toHTML(InputStream inputStream, OutputStream outputStream) throws Exception {
|
||||||
|
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(inputStream);
|
||||||
|
HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
|
||||||
|
String folder = "optimages";
|
||||||
|
htmlSettings.setImageDirPath(folder);
|
||||||
|
htmlSettings.setImageTargetUri("images");
|
||||||
|
htmlSettings.setWmlPackage(wordMLPackage);
|
||||||
|
Docx4J.toHTML(htmlSettings, outputStream, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void toHTML(WordprocessingMLPackage wordMLPackage, OutputStream outputStream) throws Exception {
|
||||||
|
HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
|
||||||
|
String folder = "optimages";
|
||||||
|
htmlSettings.setImageDirPath(folder);
|
||||||
|
htmlSettings.setImageTargetUri("images");
|
||||||
|
htmlSettings.setWmlPackage(wordMLPackage);
|
||||||
|
Docx4J.toHTML(htmlSettings, outputStream, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addImageInlineToPackage(WordprocessingMLPackage wordMLPackage, byte[] bytes) throws Exception {
|
||||||
|
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
|
||||||
|
int docPrId = 1;
|
||||||
|
int cNvPrId = 2;
|
||||||
|
Inline inline = imagePart.createImageInline("Filename hint", "Alternative text", docPrId, cNvPrId, false);
|
||||||
|
ObjectFactory factory = new ObjectFactory();
|
||||||
|
P paragraph = factory.createP();
|
||||||
|
R run = factory.createR();
|
||||||
|
paragraph.getContent().add(run);
|
||||||
|
Drawing drawing = factory.createDrawing();
|
||||||
|
run.getContent().add(drawing);
|
||||||
|
drawing.getAnchorOrInline().add(inline);
|
||||||
|
wordMLPackage.getMainDocumentPart().addObject(paragraph);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addImageInlineAndTextToPackage(WordprocessingMLPackage wordMLPackage, byte[] bytes, String text) throws Exception {
|
||||||
|
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
|
||||||
|
int docPrId = 1;
|
||||||
|
int cNvPrId = 2;
|
||||||
|
Inline inline = imagePart.createImageInline("Filename hint", "Alternative text", docPrId, cNvPrId, false);
|
||||||
|
ObjectFactory factory = new ObjectFactory();
|
||||||
|
P paragraph = factory.createP();
|
||||||
|
R run = factory.createR();
|
||||||
|
paragraph.getContent().add(run);
|
||||||
|
if (text != null) {
|
||||||
|
Text txt = factory.createText();
|
||||||
|
txt.setValue(text);
|
||||||
|
txt.setSpace("preserve");
|
||||||
|
run.getContent().add(txt);
|
||||||
|
}
|
||||||
|
|
||||||
|
Drawing drawing = factory.createDrawing();
|
||||||
|
run.getContent().add(drawing);
|
||||||
|
drawing.getAnchorOrInline().add(inline);
|
||||||
|
wordMLPackage.getMainDocumentPart().addObject(paragraph);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
File file = new File("d:" + File.separator + "opt" + File.separator + "template" + File.separator + "请假条_template.docx");
|
||||||
|
InputStream is = new FileInputStream(file);
|
||||||
|
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);
|
||||||
|
File png = new File("d:" + File.separator + "opt" + File.separator + "template" + File.separator + "badge.png");
|
||||||
|
byte[] bytes = FileCopyUtils.copyToByteArray(new FileInputStream(png));
|
||||||
|
addImageInlineToPackage(wordMLPackage, bytes);
|
||||||
|
addImageInlineAndTextToPackage(wordMLPackage, bytes, "这是图3,name.png,看图片的嵌入方式");
|
||||||
|
wordMLPackage.save(new File("d:/opt/测试" + System.currentTimeMillis() + ".docx"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user