java 导出word

引包: <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.7.3</version>
            <scope>compile</scope>
        </dependency>
实现:
	 public void exportWord(HttpServletResponse response, BdInspectRecord record) throws IOException {
        BdInspectRecord inspectRecord = baseMapper.selectById(record.getId());
        HashMap<String,Object> data = new HashMap<>();
        data.put("checkTime", DateUtil.format(inspectRecord.getCheckTime(), DateUtils.YYYY_MM_DD));
        data.put("isa",inspectRecord.getIsa());
        data.put("isc",inspectRecord.getIsc());
        data.put("ise",inspectRecord.getIse());
        data.put("isg",inspectRecord.getIsg());
        data.put("isb",inspectRecord.getIsb());
        data.put("isd",inspectRecord.getIsd());
        data.put("isf",inspectRecord.getIsf());
        data.put("ish",inspectRecord.getIsh());
        data.put("otherRes",inspectRecord.getOtherRes());

        data.put("talkTime",inspectRecord.getTalkTime());
        data.put("hornTime",inspectRecord.getHornTime());
        data.put("mobileTime",inspectRecord.getMobileTime());
        data.put("lampTime",inspectRecord.getLampTime());
        data.put("flashlightTime",inspectRecord.getFlashlightTime());
        data.put("inspectPer",inspectRecord.getInspectPer());
        data.put("runTime",inspectRecord.getRunTime());
        data.put("runRes",inspectRecord.getRunRes());
        data.put("approver",inspectRecord.getConfirmPer());
        //模板文件
        //加载资源文件
        ClassPathResource classPathResource = new ClassPathResource("/templates/防汛物资检查与维护记录表.docx");
        //获取路径
        XWPFTemplate template = XWPFTemplate.compile(classPathResource.getFile()).render(data);
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition","attachment;filename=\""+"防汛物资检查与维护记录表.docx"+"\"");
        ServletOutputStream outputStream = response.getOutputStream();
        template.write(outputStream);
        //关闭流
        template.close();//在servlet 完成生命周期之后servlet 容器会自动关闭流
        outputStream.close();
    }

你可能感兴趣的:(java,word,开发语言)