官方地址
<poi-tl-version>1.12.1</poi-tl-version>
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>${poi-tl-version}</version>
</dependency>
NiceXWPFDocument document = docList.get(0);
docList.remove(document);
NiceXWPFDocument combine = document.merge(docList, document.getXWPFDocument().createParagraph().createRun());
docList.forEach(doc-> {
try {
doc.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
response.setContentType("application/octet-stream");
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
response.setHeader("Content-disposition","attachment;filename=\""+"apiDocument" + format + ".docx"+"\"");
OutputStream out = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);
combine.write(bos);
bos.flush();
out.flush();
PoitlIOUtils.closeQuietlyMulti(combine, bos, out);
public Integer downloadDocument(DownloadDocumentDTO parameter, HttpServletResponse response) throws Exception {
long tenantId = parameter.getTenantId();
String parameterApiNoList = parameter.getApiNoList();
String[] split = parameterApiNoList.split(",");
List<String> strings = Arrays.asList(split);
List<Long> apiNoList = strings.stream().map(Long::valueOf).collect(Collectors.toList());
User user = UserContextHolder.getContext().getUser();
String accountName = user.getAccountName();
if (StringUtils.isEmpty(accountName)) {
accountName = user.getDisplayName();
}
List<NiceXWPFDocument> docList = new ArrayList();
ClassPathResource classPathResource = new ClassPathResource("/java_sdk/xx/xx/xxx-zh.docx");
InputStream resourceInputStream = classPathResource.getInputStream();
XWPFTemplate xwpfTemplate = XWPFTemplate.compile(resourceInputStream);
docList.add(xwpfTemplate.getXWPFDocument());
List<Parameter> commonRequestList = getCommonRequestList();
List<DownloadDocumentBO> downloadDocumentBOList = dataApiProMapper.downloadList(apiNoList, tenantId);
List<Integer> idList = downloadDocumentBOList.stream().map(DownloadDocumentBO::getId).collect(Collectors.toList());
int count = 0;
for (DownloadDocumentBO apiInfo : downloadDocumentBOList) {
apiInfo.setModelType(DataLogicUnitConstants.ModelType.get(Integer.valueOf(apiInfo.getModelTypeValue())).getDesc());
apiInfo.setCallType(DataApiConstants.RequestMethod.getDescByCode(apiInfo.getCallTypeValue()));
String example = apiInfo.getExample();
apiInfo.setHost(getHost());
String jsonString = "";
if (!StringUtils.isEmpty(example)) {
jsonString = JSON.toJSONString(JSONObject.parseObject(example), SerializerFeature.PrettyFormat);
}
List<Parameter> requestList = getParameterList(idList, DataApiParamConstants.Type.REQUEST);
List<Parameter> responseList = getParameterList(idList, DataApiParamConstants.Type.RETURN);
ConfigureBuilder builder = Configure.builder();
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
builder.bind("commonRequestList",policy)
.bind("requestList",policy)
.bind("responseList",policy);
ClassPathResource resource = new ClassPathResource("/download/template/apiDocument.docx");
InputStream inputStream = resource.getInputStream();
XWPFTemplate template = XWPFTemplate.compile(inputStream,builder.build());
count = count + 1;
String finalAccountName = accountName;
String finalJsonString = jsonString;
int finalCount = count;
template.render(
new HashMap<String,Object>(){{
put("title", finalCount + "." + apiInfo.getApiName());
put("downloader", finalAccountName);
put("downloadTime", DateTime.now());
put("apiInfo",apiInfo);
put("commonRequestList",commonRequestList);
put("requestList",requestList);
put("responseList",responseList);
put("responseExample", finalJsonString);
}}
);
docList.add(template.getXWPFDocument());
};
NiceXWPFDocument document = docList.get(0);
docList.remove(document);
NiceXWPFDocument combine = document.merge(docList, document.getXWPFDocument().createParagraph().createRun());
docList.forEach(doc-> {
try {
doc.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
response.setContentType("application/octet-stream");
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
response.setHeader("Content-disposition","attachment;filename=\""+"apiDocument" + format + ".docx"+"\"");
OutputStream out = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);
combine.write(bos);
bos.flush();
out.flush();
PoitlIOUtils.closeQuietlyMulti(combine, bos, out);
return 1;
}