如何将html页面导出word格式?

  近期做的项目也是奇葩,页面上需要导出excel,pdf,还有导出图片等。。。,刚把前几个怂好,还有下载成word文件,如何处理?

function getOutword (id, fileName) {
  /*  id :文档dom节点 fileName:文件名称(.doc) */
  var header = "<html xmlns:o='urn:schemas-microsoft-com:office:office' " +
    "xmlns:w='urn:schemas-microsoft-com:office:word' " +
    "xmlns='http://www.w3.org/TR/REC-html40'>" +
    "<head><meta charset='utf-8'><title>Export HTML to Word Document with JavaScript</title></head><body>"
  var footer = '</body></html>'
  var sourceHTML = header + document.getElementById(id).innerHTML + footer

  var source = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(sourceHTML)
  var fileDownload = document.createElement('a')
  document.body.appendChild(fileDownload)
  fileDownload.href = source
  fileDownload.download = fileName // 下载名称
  fileDownload.click()
  document.body.removeChild(fileDownload)
}

export default getOutword

  页面使用:getOutword('downloadwrap', '名字.doc')

  备注:

    1.页面需要内联样式,其他样式不起作用

    2.IE上面有兼容性问题,未做处理。如果有好的方式可留言哦

猜你喜欢

转载自www.cnblogs.com/changxue/p/11075961.html