1、pom引用依赖包
<dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>1.9.1</version> <exclusions> <exclusion> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>ooxml-schemas</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>io.github.draco1023</groupId> <artifactId>poi-tl-ext</artifactId> <version>0.3.3</version> <exclusions> <exclusion> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> </exclusion> <exclusion> <groupId>org.apache.poi</groupId> <artifactId>ooxml-schemas</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.w3c.css</groupId> <artifactId>sac</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>net.sourceforge.cssparser</groupId> <artifactId>cssparser</artifactId> <version>0.9.29</version> </dependency>
2、制作word模板,这里的富文本框就是用普通的文本占位符来写:如 {{basis}}
3、示例代码
/** * DOC文档生成 **/ private String generateProjectDocFile(Long projectId, ProjectsInfoRsp projectInfo) { // 文件基础路径 String baseDir = RuoYiConfig.getUploadPath(); String dirPath = baseDir + PROJECT_FILEPATH_PRE; File dirFile = new File(dirPath); if(!dirFile.exists()) { dirFile.mkdir(); } String filePath = baseDir + PROJECT_FILEPATH_PRE + projectInfo.getProjectName() + ".docx"; String absolutePath = PROJECT_FILEPATH_PRE + projectInfo.getProjectName() + ".docx"; File docFile = new File(filePath); if(docFile.exists()) { docFile.delete(); } Map<String, Object> renderData = new HashMap<>(); renderData.put("companyName", filterNullValue(projectInfo.getCompanyName())); renderData.put("companyLegalPerson", filterNullValue(projectInfo.getCompanyLegalPerson())); renderData.put("legalPersonPhone", filterNullValue(projectInfo.getLegalPersonPhone())); renderData.put("legalPersonFixPhone", filterNullValue(projectInfo.getLegalPersonFixPhone())); renderData.put("companyAddress", filterNullValue(projectInfo.getCompanyAddress())); renderData.put("bankName", filterNullValue(projectInfo.getBankName())); renderData.put("bankAccount", filterNullValue(projectInfo.getBankAccount())); renderData.put("contentAndGoals", filterHtmlValue(filterNullValue(projectInfo.getProjectDetails().getContentAndGoals()))); renderData.put("feasibilityAnalysis", filterHtmlValue(filterNullValue(projectInfo.getProjectDetails().getFeasibilityAnalysis()))); renderData.put("basis", filterHtmlValue(filterNullValue(projectInfo.getProjectDetails().getBasis()))); renderData.put("demonstrationAndInnovation", filterHtmlValue(filterNullValue(projectInfo.getProjectDetails().getDemonstrationAndInnovation()))); String templatePath = baseDir + "/templates/xmxq.docx"; FileOutputStream fos = null; try { HtmlRenderPolicy htmlRenderPolicy = new HtmlRenderPolicy(); // bind 为需要解析为html格式的富文本内容 Configure configure = Configure.builder() .bind("contentAndGoals", htmlRenderPolicy) .bind("feasibilityAnalysis", htmlRenderPolicy) .bind("basis", htmlRenderPolicy) .bind("demonstrationAndInnovation", htmlRenderPolicy) .build(); XWPFTemplate template = XWPFTemplate.compile(templatePath, configure).render(renderData); template.writeAndClose(new FileOutputStream(filePath)); } catch (Exception e) { filePath = null; e.printStackTrace(); } finally { try { if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } if(null != filePath) { return absolutePath; } return null; } private String filterHtmlValue(String val) { if(StringUtils.isEmpty(val)) { return "-"; } return val.replaceAll("src=\"/profile", "src=\"http://192.168.128.77:21281/profile"); }
标签:为富,java,String,projectInfo,renderData,poi,put,word,filterNullValue From: https://www.cnblogs.com/scode2/p/18405762