html导出成word

springmvc 后台

public void exportWord(HttpServletResponse response, String body)
            throws Exception {
        String fileName = "a.doc";
        String content = "<html><head></head><body>"+StringEscapeUtils.unescapeHtml4(body)+"</body><html/>";
        System.out.println(content);
        byte b[] = content.getBytes();
        response.reset();
        response.setHeader("Content-Disposition",
                "attachment;filename=" +
                        new String( (fileName + ".doc").getBytes(),
                                "iso-8859-1"));
        response.setContentType("application/msword");
        ByteArrayInputStream bais = new ByteArrayInputStream(b);
        POIFSFileSystem poifs = new POIFSFileSystem();
        DirectoryEntry directory = poifs.getRoot();
        DocumentEntry documentEntry = directory.createDocument("word", bais);
        //输出文件
        response.reset();
        response.addHeader("Content-Disposition",
                "attachment;filename=" + new String(documentEntry.getName() + ".doc"));
        OutputStream ostream = new BufferedOutputStream(response.getOutputStream());
        response.setContentType("application/octet-stream");
        ostream.write(b);
        ostream.flush();
        ostream.close();
    }

富文本使用kindeditor编辑器

<textarea id="editor_id" name="content" style="width:700px;height:300px;">
</textarea>
<a>导出</a>

获取文本的html,提交到后台。此处获取到html需要做处理,否则会导致编译

var editor;
KindEditor.ready(function(K) {
      editor = K.create('#editor_id');
});
$('a').click(function () {
      var form = $('<form style="display:none" method="POST"   
      action="/test/download"><input type="hidden" name="body" value="' +         
      html_encode(editor.html()) + '"/></form>');
      $('body').append(form);
      form.submit();
      form.remove();
});
function html_encode(strHTML){
      var s = "";
      if (strHTML.length == 0) return "";
      s = strHTML.replace(/&/g, "&amp;");
      s = s.replace(/</g, "&lt;");
      s = s.replace(/>/g, "&gt;");
      s = s.replace(/\'/g, "&#39;");
      s = s.replace(/\"/g, "&quot;");
      s = s.replace(/\n/g, "");
      return s;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值