Spring-Boot富文本内容导出成word文档

常量:HTML头

    /**
     * html头
     */
    public static final String WORD_HTML_HEAD = "<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n" +
            "xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\"\n" +
            "xmlns=\"http://www.w3.org/TR/REC-html40\"><head>\n" +
            "    <!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:TrackMoves>false</w:TrackMoves><w:TrackFormatting/><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:DoNotPromoteQF/><w:LidThemeOther>EN-US</w:LidThemeOther><w:LidThemeAsian>ZH-CN</w:LidThemeAsian><w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript><w:Compatibility><w:BreakWrappedTables/><w:SnapToGridInCell/><w:WrapTextWithPunct/><w:UseAsianBreakRules/><w:DontGrowAutofit/><w:SplitPgBreakAndParaMark/><w:DontVertAlignCellWithSp/><w:DontBreakConstrainedForcedTables/><w:DontVertAlignInTxbx/><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><m:mathPr><m:mathFont m:val=\"Cambria Math\"/><m:brkBin m:val=\"before\"/><m:brkBinSub m:val=\"--\"/><m:smallFrac m:val=\"off\"/><m:dispDef/><m:lMargin m:val=\"0\"/> <m:rMargin m:val=\"0\"/><m:defJc m:val=\"centerGroup\"/><m:wrapIndent m:val=\"1440\"/><m:intLim m:val=\"subSup\"/><m:naryLim m:val=\"undOvr\"/></m:mathPr></w:WordDocument></xml><![endif]-->\n" +
            "</head>";

    /**
     * 图片
     */
    public static final String IMG= "<div style=\"margin-top: 30px; text-align: right;\"><span style=\"font-size: 18px;\">签字:</span> <img style=\"height: 40px;\" src='{img}' /> </div>";

    /**
     * 更换图片
     */
    public static final String REPLACE_IMG = "{img}";

​Controller

    @PostMapping("/doc")
    public void downloadDoc(HttpServletResponse response){
        service.downloadDoc(response,param);
    }

​Service

void downloadDoc(HttpServletResponse response);

ServiceImpl

    @Override
    public void downloadDoc(HttpServletResponse response) {
        //替换常量中的图片 url
        String imageUrl= "";
        String img= CommonConstants.IMG.replace(REPLACE_IMG, imageUrl);
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            //数据源
            String contents = "富文本内容";
            String content = CommonConstants.WORD_HTML_HEAD + "<body>" + contents + "" + img+ "</body></html>";
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/msword");
            response.setHeader("Content-disposition", "attachment;filename=" + new String((文件名 + ".docx").getBytes(StandardCharsets.UTF_8), "ISO8859-1"));
            out.write(content.getBytes(StandardCharsets.UTF_8));
            out.flush();
        } catch (Exception e) {
            log.error("导出word异常", e);
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

前端下载代码  res就是后台返回的文件流

        const url = window.URL.createObjectURL(new Blob([res], { type: 'application/msword;charset=utf-8 ' }));
        const link = document.createElement('a');
        link.style.display = 'none';
        link.href = url;
        link.setAttribute('download', '文件名.doc');
        document.body.appendChild(link);
        link.click();

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值