将html内容转换成word文档下载,纯js操作

53 篇文章 0 订阅
13 篇文章 0 订阅


一、?

二、操作步骤

1.html内容

代码如下(示例):

<div>
          <div id="source-html" style="750px">
            <div
              style="width: 100%; text-align: center; font-weight: 600; font-size: 17px;margin-bottom: 18px;"
            >验收表</div>
            <div style="width: 100%; text-align: center;">
              地址:
              <span style="text-decoration: underline;">&nbsp;&nbsp;</span>省(区、县)
              <span style="text-decoration: underline;">&nbsp;&nbsp;</span>市(地、州、盟)
              <span style="text-decoration: underline;">&nbsp;&nbsp;</span>县(市、区、旗)
            </div>
            <table
              class="fixed_headers"
              style="border: 1px solid #ccc;border-collapse: collapse; margin: 0; padding: 0;  width: 100%;table-layout: fixed;"
            >
              <tbody style="width: 100%;">
                <td style="line-height: 38px; text-align: center;border: 1px solid black;">小区</td>
                <td
                  style="line-height: 38px; text-align: center;border: 1px solid black;"
                  colspan="2"
                >
                  小区图
                  <br />情况
                </td>
                <td
                  style="line-height: 38px; text-align: center;border: 1px solid black;"
                  colspan="2"
                >数量</td>
                <td
                  style="line-height: 38px; text-align: center;border: 1px solid black;"
                  colspan="2"
                >单元</td>
                <td
                  style="line-height: 38px; text-align: center;border: 1px solid black;"
                  colspan="2"
                >人口</td>
                <td
                  style="line-height: 38px; text-align: center;border: 1px solid black;"
                  colspan="2"
                >人口</td>
                <td
                  style="line-height: 38px; text-align: center;border: 1px solid black;"
                  colspan="2"
                >人口</td>
                <tr v-for="(item,index) in tableData1" :key="index">
                  <td style="line-height: 38px; text-align: center;border: 1px solid black;"></td>
                  <td
                    style=" line-height: 38px; text-align: center;border: 1px solid black;"
                  >{{item.name}}</td>
                  <td
                    style=" line-height: 38px; text-align: center;border: 1px solid black;"
                  >{{item.name}}</td>
                  <td
                    style=" line-height: 38px; text-align: center;border: 1px solid black;"
                  >{{item.name}}</td>
                  <td
                    style=" line-height: 38px; text-align: center;border: 1px solid black;"
                  >{{item.name}}</td>
                  <td
                    style=" line-height: 38px; text-align: center;border: 1px solid black;"
                  >{{item.name}}</td>
                  <td
                    style=" line-height: 38px; text-align: center;border: 1px solid black;"
                  >{{item.name}}</td>
                  <td
                    style=" line-height: 38px; text-align: center;border: 1px solid black;"
                  >{{item.name}}</td>
                  <td
                    style=" line-height: 38px; text-align: center;border: 1px solid black;"
                  >{{item.name}}</td>
                  <td
                    style=" line-height: 38px; text-align: center;border: 1px solid black;"
                  >{{item.name}}</td>
                  <td
                    style=" line-height: 38px; text-align: center;border: 1px solid black;"
                  >{{item.name}}</td>
                  <td
                    style=" line-height: 38px; text-align: center;border: 1px solid black;"
                  >{{item.name}}</td>
                  <td
                    style=" line-height: 38px; text-align: center;border: 1px solid black;"
                  >{{item.name}}</td>
                </tr>
                <!-- 合计 -->
                <tr>
                  <td style="line-height: 38px; text-align: center;border: 1px solid black;">合计</td>
                  <td style=" line-height: 38px; text-align: center;border: 1px solid black;"></td>
                  <td style=" line-height: 38px; text-align: center;border: 1px solid black;"></td>
                  <td style=" line-height: 38px; text-align: center;border: 1px solid black;"></td>
                  <td style=" line-height: 38px; text-align: center;border: 1px solid black;"></td>
                  <td style=" line-height: 38px; text-align: center;border: 1px solid black;"></td>
                  <td style=" line-height: 38px; text-align: center;border: 1px solid black;"></td>
                  <td style=" line-height: 38px; text-align: center;border: 1px solid black;"></td>
                  <td style=" line-height: 38px; text-align: center;border: 1px solid black;"></td>
                  <td style=" line-height: 38px; text-align: center;border: 1px solid black;"></td>
                  <td style=" line-height: 38px; text-align: center;border: 1px solid black;"></td>
                  <td style=" line-height: 38px; text-align: center;border: 1px solid black;"></td>
                  <td style=" line-height: 38px; text-align: center;border: 1px solid black;"></td>
                </tr>
                <!-- 指标差错率(注) -->
                <tr>
                  <td
                    style="line-height: 38px; text-align: center;border: 1px solid black;"
                  >指标差错率(注)</td>
                  <td
                    style="line-height: 38px; text-align: center;border: 1px solid black;"
                    colspan="2"
                  ></td>
                  <td
                    style="line-height: 38px; text-align: center;border: 1px solid black;"
                    colspan="2"
                  ></td>
                  <td
                    style="line-height: 38px; text-align: center;border: 1px solid black;"
                    colspan="2"
                  ></td>
                  <td
                    style="line-height: 38px; text-align: center;border: 1px solid black;"
                    colspan="2"
                  ></td>
                  <td
                    style="line-height: 38px; text-align: center;border: 1px solid black;"
                    colspan="2"
                  ></td>
                  <td
                    style="line-height: 38px; text-align: center;border: 1px solid black;"
                    colspan="2"
                  ></td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
        <button id="btn-export" class="btn" @click="exportHTML">
          Export
        </button>

2.js内容

数据部分

代码如下(示例):

tableData1 = [
        {
          name: 'Apple',
          color: 'Red',
          description: 'These are red.',
        },
        {
          name: 'Apple',
          color: 'Red',
          description: 'These are red.',
        },
        {
          name: 'Apple',
          color: 'Red',
          description: 'These are red.',
        },
      ]

调用方法部分

代码如下(示例):

//将html导出为word
    exportHTML() {
      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 = '<div>' + document.getElementById('source-html').innerHTML + '</div>'

      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 = 'document.doc'
      fileDownload.click()
      document.body.removeChild(fileDownload)
    },

3.css福利内容

代码如下(示例):

#btn-export.btn{
  width: 130px;
  height: 40px;
  color: #fff;
  border-radius: 5px;
  padding: 10px 25px;
  font-family: 'Lato', sans-serif;
  font-weight: 500;
  background: transparent;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  display: inline-block;
   box-shadow:inset 2px 2px 2px 0px rgba(255,255,255,.5),
   7px 7px 20px 0px rgba(0,0,0,.1),
   4px 4px 5px 0px rgba(0,0,0,.1);
}

.btn#btn-export {
  border: none;
  background: rgb(251,33,117);
    background: linear-gradient(0deg, rgba(251,33,117,1) 0%, rgba(234,76,137,1) 100%);
    color: #fff;
    overflow: hidden;
}

.btn#btn-export:hover {
    text-decoration: none;
    color: #fff;
}
.btn#btn-export:before {
    position: absolute;
    content: '';
    display: inline-block;
    top: -180px;
    left: 0;
    width: 30px;
    height: 100%;
    background-color: #fff;
    animation: shiny-btn1 3s ease-in-out infinite;
}
.btn#btn-export:hover{
  opacity: .7;
}
.btn#btn-export:active{
  box-shadow:  4px 4px 6px 0 rgba(255,255,255,.3),
              -4px -4px 6px 0 rgba(116, 125, 136, .2), 
    inset -4px -4px 6px 0 rgba(255,255,255,.2),
    inset 4px 4px 6px 0 rgba(0, 0, 0, .2);
}

@-webkit-keyframes shiny-btn1 {
    0% { -webkit-transform: scale(0) rotate(45deg); opacity: 0; }
    80% { -webkit-transform: scale(0) rotate(45deg); opacity: 0.5; }
    81% { -webkit-transform: scale(4) rotate(45deg); opacity: 1; }
    100% { -webkit-transform: scale(50) rotate(45deg); opacity: 0; }
}

4.结果展示

在这里插入图片描述
在这里插入图片描述
github链接: https://github.com/NotBerlin/vue-router-vuex.

dev_services分支!!!

总结

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值