1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.aspose.words.*; import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import java.io.File; import java.io.FileInputStream; import java.time.LocalDateTime; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern;
@Slf4j @Service public class TemplateDocServiceImpl implements TemplateDocService {
@Resource private SysOssService sysOssService;
@Override public List<FileDto> fillTemplateDoc(ReportDTO reportDTO, List<RoomReportFileTemplateEntity> templateList) { String structStr = JSONUtil.toJsonStr(reportDTO); log.info("填充模版文档结构化字符串: fillTemplateDoc structStr -> {}", structStr);
List<FileDto> fileDtoList = Lists.newArrayList(); for (RoomReportFileTemplateEntity template : templateList) { if (template.getTemplateType() == 0) { continue; } String templateName = template.getTemplateName(); String templateDocExt = template.getTemplateDocExt(); String filename = templateName + "." + templateDocExt; String templateDocUrl = template.getTemplateDocUrl();
String ossUrl = writeDoc(structStr, filename, templateDocUrl);
FileDto fileDto = new FileDto(); fileDto.setFileName(templateName); fileDto.setFileExt(templateDocExt); fileDto.setFileSize(FileUtil.getFileSize(ossUrl)); fileDto.setFileUrl(ossUrl); fileDtoList.add(fileDto); } log.info("填充模版文档结果:fileDtoList -> {}", JSONUtil.toJsonStr(fileDtoList)); return fileDtoList; }
private String writeDoc(String structStr, String filename, String templateUrl) { String url = ""; try { log.info("开始书写入文档: structStr:{}, filename:{}, templateUrl:{}", structStr, filename, templateUrl); String path = FileUtil.downloadFile(templateUrl, LocalDateTime.now().getNano() + FileTypeEnum.docx.getSuffix()); Document document = new Document(new FileInputStream(path)); dealDoc(structStr, document); String fullFileName = FileUtil.BASE_DIR + filename; document.save(fullFileName); url = sysOssService.uploadSuffix(new File(fullFileName)); log.info("上传OSS地址: url -> {}", url); CompletableFutureUtil.supplyAsync(Lists.newArrayList(path), FileUtil::deleteFile); } catch (Exception e) { log.error("下载模版文档发生异常: {}", e.getMessage(), e); } return url; }
private void dealDoc(String structStr, Document document) throws Exception { JSONObject jsonObject = JSON.parseObject(structStr); document.stopTrackRevisions();
FindReplaceOptions options = new FindReplaceOptions(); NodeCollection paragraphs = document.getChildNodes(NodeType.PARAGRAPH, true); log.info("开始遍历段落整理模版: {}", paragraphs.getCount()); for (int i = 0; i < paragraphs.getCount(); i++) { Paragraph node = (Paragraph) paragraphs.get(i); String text = node.getRange().getText(); String regex = "\\$(.*?)\\$"; Matcher matcher = Pattern.compile(regex).matcher(text); if (matcher.find()) { String match = matcher.group(1); Object o = jsonObject.get(match); if (o instanceof JSONArray) { JSONArray arr = (JSONArray) o; if (arr.size() > 1) { for (int arrIndex = arr.size() - 1; arrIndex >= 0; arrIndex--) { Paragraph newParagraph = (Paragraph) node.deepClone(true); newParagraph.getRange().replace(match, match + (arrIndex + 1), options); document.getSections().get(0).getBody().insertAfter(newParagraph, node); i++; } document.getSections().get(0).getBody().removeChild(node); } } } }
log.info("替换模版内容: json -> {}", jsonObject.toJSONString()); for (String s : jsonObject.keySet()) { Object o = jsonObject.get(s); if (o instanceof JSONArray) { JSONArray arr = (JSONArray) o; if (arr.size() > 1) { for (int i = 0; i < arr.size(); i++) { document.getRange().replace("$" + s + (i + 1) + "$", arr.get(i).toString(), options); } } else { document.getRange().replace("$" + s + "$", arr.get(0).toString(), options); } } else { document.getRange().replace("$" + s + "$", o.toString(), options); } } } }
|