|
@@ -1,8 +1,8 @@
|
|
|
package com.rongwei.zhsw.system.service.impl;
|
|
|
|
|
|
-import com.aspose.words.ImportFormatMode;
|
|
|
-import com.aspose.words.SaveFormat;
|
|
|
+import com.aspose.words.*;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
import com.rongwe.zhsw.system.domain.SwBusinesshallDo;
|
|
|
import com.rongwe.zhsw.system.domain.SwEnterpriseConfigInfoDo;
|
|
|
import com.rongwei.rwcommon.base.BaseDo;
|
|
@@ -14,6 +14,7 @@ import com.rongwei.zhsw.system.service.SwCollectionNoticeService;
|
|
|
import com.rongwei.zhsw.system.service.SwEnterpriseConfigInfoService;
|
|
|
import org.apache.poi.util.IOUtils;
|
|
|
import org.apache.poi.util.Units;
|
|
|
+import org.apache.poi.xwpf.usermodel.BreakType;
|
|
|
import org.apache.poi.xwpf.usermodel.*;
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.*;
|
|
@@ -28,6 +29,7 @@ import org.springframework.stereotype.Service;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
+import java.util.List;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -318,12 +320,13 @@ public class SwCollectionNoticeServiceImpl implements SwCollectionNoticeService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@Override
|
|
|
public void generatePdf(List<Map<String, String>> dataList, HttpServletResponse response) {
|
|
|
|
|
|
SwEnterpriseConfigInfoDo swEnterpriseConfigInfoDo = swEnterpriseConfigInfoService.getOne(new LambdaQueryWrapper<SwEnterpriseConfigInfoDo>()
|
|
|
- .eq(BaseDo::getDeleted,"0"));
|
|
|
- if(swEnterpriseConfigInfoDo==null){
|
|
|
+ .eq(BaseDo::getDeleted, "0"));
|
|
|
+ if (swEnterpriseConfigInfoDo == null) {
|
|
|
throw new CustomException("无法获取企业配置信息");
|
|
|
}
|
|
|
String tempName = swEnterpriseConfigInfoDo.getTempName();
|
|
@@ -338,7 +341,7 @@ public class SwCollectionNoticeServiceImpl implements SwCollectionNoticeService
|
|
|
String addresStr = list.stream().map(data -> data.getAddress()).collect(Collectors.joining("\n"));
|
|
|
|
|
|
ClassPathResource classPathResource = new ClassPathResource("temp/" + tempName);
|
|
|
- try(InputStream templateStream = classPathResource.getInputStream()) {
|
|
|
+ try (InputStream templateStream = classPathResource.getInputStream()) {
|
|
|
Date now = new Date();
|
|
|
Calendar instance = Calendar.getInstance();
|
|
|
instance.setTime(now);
|
|
@@ -346,17 +349,35 @@ public class SwCollectionNoticeServiceImpl implements SwCollectionNoticeService
|
|
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
String dateStr = formatter.format(instance.getTime());
|
|
|
String nowStr = formatter.format(now);
|
|
|
- // 模板
|
|
|
- com.aspose.words.Document temp = new com.aspose.words.Document(templateStream);
|
|
|
+
|
|
|
// 合并的word
|
|
|
com.aspose.words.Document mergedDoc = null;
|
|
|
|
|
|
- for (Map<String, String> dataMapp : dataList) {
|
|
|
+ com.aspose.words.Document temp = new com.aspose.words.Document(templateStream);
|
|
|
+ List<List<Map<String, String>>> partition = Lists.partition(dataList, 2);
|
|
|
+ Map<String, String> secondMap;
|
|
|
+ for (List<Map<String, String>> dataMapp : partition) {
|
|
|
com.aspose.words.Document currentDoc = temp.deepClone(); // 深拷贝模板
|
|
|
- currentDoc.getMailMerge().execute(new String[]{"USERNAME", "USERNUMBER", "YEAR", "QYMC", "RQ","sfdt"},
|
|
|
- new Object[]{dataMapp.getOrDefault("USERNAME", ""),
|
|
|
- dataMapp.getOrDefault("USERNUMBER", ""), dateStr,
|
|
|
- swEnterpriseConfigInfoDo.getEnterprisename(), nowStr,addresStr});
|
|
|
+ Map<String, String> firstMap = dataMapp.get(0);
|
|
|
+
|
|
|
+ if (dataMapp.size() > 1) {
|
|
|
+ secondMap = dataMapp.get(1);
|
|
|
+ } else {
|
|
|
+ secondMap = new HashMap<>();
|
|
|
+ }
|
|
|
+ currentDoc.getMailMerge().execute(new String[]{"USERNAME", "USERNUMBER", "YEAR", "QYMC", "RQ", "sfdt", "GZHMC",
|
|
|
+ "USERNAME2", "USERNUMBER2",
|
|
|
+ },
|
|
|
+ new Object[]{firstMap.getOrDefault("USERNAME", ""),
|
|
|
+ firstMap.getOrDefault("USERNUMBER", ""), dateStr,
|
|
|
+ swEnterpriseConfigInfoDo.getEnterprisename(), nowStr, addresStr,
|
|
|
+ swEnterpriseConfigInfoDo.getGzhmc(),
|
|
|
+ secondMap.getOrDefault("USERNAME", ""),
|
|
|
+ secondMap.getOrDefault("USERNUMBER", "")
|
|
|
+ });
|
|
|
+ currentDoc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS);
|
|
|
+ currentDoc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS);
|
|
|
+ currentDoc.getMailMerge().deleteFields();
|
|
|
if (mergedDoc == null) {
|
|
|
mergedDoc = currentDoc;
|
|
|
} else {
|
|
@@ -370,6 +391,15 @@ public class SwCollectionNoticeServiceImpl implements SwCollectionNoticeService
|
|
|
// for (com.aspose.words.Document doc : docList) {
|
|
|
// finalDoc.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
|
|
|
// }
|
|
|
+ // SaveOptions options = new PdfSaveOptions();
|
|
|
+ // options.setSaveFormat(com.aspose.words.SaveFormat.PDF);
|
|
|
+ // 删除所有分页符
|
|
|
+ NodeCollection<Paragraph> pageBreaks = mergedDoc.getChildNodes(NodeType.PARAGRAPH, true);
|
|
|
+ for (Paragraph para : pageBreaks) {
|
|
|
+ if (para.getParagraphFormat().getPageBreakBefore()) {
|
|
|
+ para.getParagraphFormat().setPageBreakBefore(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
response.setContentType("application/octet-stream;charset=ISO8859-1");
|
|
|
response.setHeader("Content-Disposition", "inline; filename=");
|