导出备忘录Word文档

这里写自定义目录标题


在这里插入图片描述

在这里插入图片描述

 /**
     * 导出备忘录Word文档
     *
     * @param id
     * @return
     */
    @Override
    public String exportWordMemo(Long id) throws Exception {
        String tmpPath = ResourceUtils.getURL("classpath:").getPath() + "static" + "/" + "doc" + "/" + "memo.doc";
        String tmpPath2 = ResourceUtils.getURL("classpath:").getPath() + "static" + "/" + "doc" + "/" + "memo2.doc";
        Map<String, Object> memo = memoMapper.selectMemoForWord(id);
        List<MeetingMemoTaker> takers = meetingMemoTakerMapper.getMemoTaker(id);
        Map<String, String> data = new HashMap<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
        String date = null;
        String date1 = null;
        if (memo.get("MEETING_TIME") != null && !"".equals(memo.get("MEETING_TIME"))) {
            date = sdf.format(memo.get("MEETING_TIME"));
            data.put("${meetingTime}", date);
            date1 = sdf1.format(memo.get("MEETING_TIME"));
            data.put("${time}", date1);
        } else {
            data.put("${meetingTime}", "");
        }
        if (memo.get("MEETING_NAME") != null && !"".equals(memo.get("MEETING_NAME"))) {
            data.put("${meetingName}", memo.get("MEETING_NAME").toString());
        } else {
            data.put("${meetingName}", "");
        }
        if (memo.get("MEETING_MEMO_TAKER") != null && !"".equals(memo.get("MEETING_MEMO_TAKER"))) {
            data.put("${meetingMemoTaker}", memo.get("MEETING_MEMO_TAKER").toString());
        } else {
            data.put("${meetingMemoTaker}", "");
        }
        if (memo.get("MEETING_PLACE") != null && !"".equals(memo.get("MEETING_PLACE"))) {
            data.put("${meetingPlace}", memo.get("MEETING_PLACE").toString());
        } else {
            data.put("${meetingPlace}", "");
        }
        if (memo.get("REMAINING_PROBLEMS") != null && !"".equals(memo.get("REMAINING_PROBLEMS"))) {
            data.put("${remainingProblems}", memo.get("REMAINING_PROBLEMS").toString());
        } else {
            data.put("${remainingProblems}", "");
        }
        if (memo.get("CONCLUSION") != null && !"".equals(memo.get("CONCLUSION"))) {
            String conclusion="";
            Document document=Jsoup.parse(memo.get("CONCLUSION").toString());
            conclusion=document.text();
            data.put("${conclusion}", conclusion);
        } else {
            data.put("${conclusion}", "");
        }
        if (memo.get("MEETING_MEMO_NUM") != null && !"".equals(memo.get("MEETING_MEMO_NUM"))) {
            data.put("${code}", memo.get("MEETING_MEMO_NUM").toString());
        } else {
            data.put("${code}", "");
        }
        MeetingMemoTaker[] memoTakers = new MeetingMemoTaker[takers.size()];
        takers.toArray(memoTakers);
        String nameList = "";
        String deptList = "";
        for (int i = 0; i < memoTakers.length; i++) {
            nameList = nameList + memoTakers[i].getName() + (char) 11;
            deptList = deptList + memoTakers[i].getDeptName() + (char) 11;
        }
        data.put("${name}", nameList);
        data.put("${dept}", deptList);
        data.put("${year}", String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));

        Set<Map.Entry<String, String>> entrySet = data.entrySet();
        for (Map.Entry<String, String> entry : entrySet) {
            System.out.println(entry.getKey() + "===" + entry.getValue());
        }
        File file = new File(tmpPath);
        System.out.println(file.getAbsolutePath() + "\t" + file.exists());

        HWPFDocument changWord2003 = WordToNewWordUtil.changWord2003(tmpPath, data);
        FileOutputStream fileOutputStream = new FileOutputStream(tmpPath2);
        changWord2003.write(fileOutputStream);
        fileOutputStream.close();
        return "success";
    }
 /**
     * 文件下载
     *
     * @param type
     * @return
     */
    @Override
    public String fileDownload(String type, HttpServletResponse response) throws Exception {
        String wordName = "";
        // 下载后的文件名
        String fileName = "";
        if ("1".equals(type)) {
            wordName = "meeting2.doc";
            fileName = "会议通知.doc";
        } else if ("2".equals(type)) {
            wordName = "memo2.doc";
            fileName = "备忘录.doc";
        } else if ("3".equals(type)) {
            wordName = "weekly2.doc";
            fileName = "周报.doc ";
        }
        // 下载的文件路径
        String filePath = ResourceUtils.getURL("classpath:").getPath() + "static" + "/" + "doc" + "/" + wordName;
        //使用流的形式下载文件
        try {
            //加载文件
            File file = new File(filePath);
            InputStream is = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[is.available()];
            is.read(buffer);
            is.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            return "文件下载出错";
        }
    }
/**
     * 导出备忘录Word文档
     * @param id
     * @return
     */
    @RequestMapping("/exportWordMemo")
    @ResponseBody
    public String exportWordMemo(Long id){
        String exportWordMemo = null;
        try {
            exportWordMemo = projectMeetingService.exportWordMemo(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return exportWordMemo;
    }

    /**
     * 文档下载
     * @param
     * @return
     */
    @RequestMapping("/fileDownload")
    @ResponseBody
    public String fileDownload(String type,HttpServletResponse response){
        String str= null;
        try {
            str = projectMeetingService.fileDownload(type,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值