導(dǎo)出docx為字節(jié)流

注釋:由于poi-tl對(duì)循環(huán)列表的導(dǎo)出說明的不是很詳細(xì),故此處導(dǎo)出時(shí)使用poi和poi-tl結(jié)合導(dǎo)出。

? ? 基本思路為:使用poi導(dǎo)出文本和列表,存為臨時(shí)文件,再以臨時(shí)文件為模板,使用poi-tl導(dǎo)出圖表,最終生成導(dǎo)出結(jié)果。

? ? 應(yīng)注意把臨時(shí)文件刪除
@ApiOperation(value = "項(xiàng)目報(bào)告導(dǎo)出")

@ResponseBody

@GetMapping("/docx/{id}")

public Object wordExporttest2(@PathVariable Integer id,HttpServletResponse re) throws Exception {

Map<String,Object> infos = service.docxInfos(id);

Map<String,List<Map<String,Object>>> infolist = new HashMap<>();

infolist.put("tool", service.toolInfos(id));

infolist.put("ruleset", service.rulesetInfos(id));

infolist.put("build", service.buildInfos(id));

infolist.put("qg", service.qualityGateInfos(id));

infolist.put("sf", service.sourcefileInfos(id));

infolist.put("sr", service.fileMetricInfos(id));

infolist.put("issue", service.issueInfos(id));

// FileInputStream in = new FileInputStream("C:\\Users\\Desktop\\e2\\模板 .docx");

WordDocxWriter docx = new WordDocxWriter(

// in);

ProjectExportController.class.getResourceAsStream("/static/template/projectExportTemplate.docx"));

docx.setMarker(infos).setMarkers(infolist);

byte[] bytes = docx.getBytes();

File tmp = CstDirs.getDir(CstDir.tmp);

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");

File xdoc = new File(tmp,id+sdf.format(new Date())+".docx");

FileOutputStream fos = new FileOutputStream(xdoc);

fos.write(bytes);

fos.close();

Map<String,Object> plinfos = new HashMap<>();

String name = "";

if(service.getById(id) != null) {

name = StringKit.of(service.getById(id).getName());

}

plinfos.put("page", name+" 文件");

plinfos.put("severityPie", service.issueServerityPie(id));

plinfos.put("unFixedPie", service.unFixedIssueServerityPie(id));

plinfos.put("statePie", service.issueStatePie(id));

plinfos.put("statusPie", service.issueStatusPie(id));

plinfos.put("ruleCodeBar", service.issueRuleTop10(id));

plinfos.put("buildLine", service.BuildLineTop10(id));

plinfos.put("ownerBar", service.issueOwnerTop10(id));

plinfos.put("sourcefileBar", service.issueSourcefileTop10(id));

XWPFTemplate template = XWPFTemplate.compile(xdoc).render(plinfos);

xdoc.delete();

ByteArrayOutputStream? baos = new ByteArrayOutputStream();

template.write(baos);

template.close();

// baos.close();

// 設(shè)置http協(xié)議頭部

HttpHeaders headers = new HttpHeaders();

// 設(shè)置文件名

String fileName = "";

fileName = new String((service.getById(id).getName() + "項(xiàng)目報(bào)告.docx").getBytes("UTF-8"), "iso-8859-1");// 為了解決中文名稱亂碼問題

headers.setContentDispositionFormData("attachment", fileName);

headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

re.setHeader("Content-Disposition", "attachment;fileName=" + fileName);

return baos.toByteArray();

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容