vue html 转 word | html-docx-js

【觉得有用的小伙伴最后点个赞吧蟹蟹】
1. 下载

npm/cnpm install htmldocx :直接报错GET 404 Not found,我用的下载指令是 npm/cnpm install html-docx-js, 然后可以下载一个保存文件的插件file-saver,当然你用传统的创建a标签再打开一样可行,亲测可用。

2. 避坑
2.1 引用

网上说 这样引用import htmlDocx from "htmldocx"; 我是发现不好使,后来我找了一下node_modules包下面的html-docx-js 改成这样引用:import htmlDocx from 'html-docx-js/dist/html-docx'; import { saveAs } from 'file-saver';

2.2 html

结合jQuery的开发方式直接写html 包括css样式,我没有采用获取id元素转化html的方式:const htmlContent = document.getElementById("app"); const blob = htmlDocx.asBlob(htmlContent.outerHTML);

3. 上代码
<script>
  import htmlDocx from 'html-docx-js/dist/html-docx'
  import { saveAs } from 'file-saver'
  export default {
    methods: {
      createDocx () {
        let mun = 100
        let month = 12
        let mun2 = 50
        let proportion = 80
        let time = '2024/04/01 23:59:59'
        let remark = '备注备注备注备注'
        let reportTime = '2024年4月28日'
        const htmlContent = `
          <!DOCTYPE HTML>
          <html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                <meta http-equiv="Content-Style-Type" content="text/css">
                <meta name="generator" content="Aspose.Words for .NET 15.1.0.0">
                <title></title>
            </head>
            <body>
              <div>
                <h1 style="font-size:22pt; line-height:115%; margin:24pt 0pt 0pt; page-break-after:avoid; page-break-inside:avoid; text-align:center">
                  <span style="font-family:宋体; font-size:22pt; font-weight:normal">抽检报告</span>
                </h1>
                <p style="margin:0pt 0pt 0pt 18pt"><span style="font-family:宋体; font-size:12pt">&nbsp;</span></p>
                <table cellspacing="0" cellpadding="0" style="border-collapse:collapse; margin-left:0pt">
                  <tbody>
                    <tr style="height:32pt">
                      <td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:61.2pt">
                        <p style="margin:0pt"><span style="font-family:宋体; font-size:12pt">业务名称</span></p>
                      </td>
                      <td colspan="4" style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:381.2pt">
                        <p style="margin:0pt"><span style="font-family:宋体; font-size:12pt">XXX业务</span></p>
                      </td>
                    </tr>
                    <tr style="height:28pt">
                      <td colspan="5" style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:453.2pt">
                        <p style="margin:0pt"><span style="font-family:宋体; font-size:12pt">采用XXX短信抽检模板</span></p>
                      </td>
                    </tr>
                    <tr style="height:20pt">
                      <td rowspan="2" style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:61.2pt">
                        <p style="margin:0pt"><span style="font-family:宋体; font-size:12pt">短信抽检</span></p>
                      </td>
                      <td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:61.2pt">
                        <p style="margin:0pt"><span style="font-family:宋体; font-size:12pt">短信总量</span></p>
                      </td>
                      <td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:94.2pt">
                        <p style="margin:0pt">
                          <span style="font-family:宋体; font-size:12pt">${mun}条(${month}月)</span>
                        </p>
                      </td>
                      <td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:61.2pt">
                        <p style="margin:0pt"><span style="font-family:宋体; font-size:12pt">抽检条数</span></p>
                      </td>
                      <td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:132.2pt">
                        <p style="margin:0pt">
                          <span style="font-family:宋体; font-size:12pt">${mun2}条,抽检比例${proportion}%</span>
                        </p>
                      </td>
                    </tr>
                    <tr style="height:20pt">
                      <td colspan="2" style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:147.2pt">
                        <p style="margin:0pt"><span style="font-family:宋体; font-size:12pt">人工抽检时间</span></p>
                      </td>
                      <td colspan="2" style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:223.2pt">
                        <p style="margin:0pt">
                          <span style="font-family:宋体; font-size:12pt">${time}</span>
                        </p>
                      </td>
                    </tr>
                    <tr style="height:110.65pt">
                      <td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:61.2pt">
                        <p style="margin:0pt"><span style="font-family:宋体; font-size:12pt">短信抽检结果及意见</span></p>
                      </td>
                      <td colspan="4" style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:381.2pt">
                        <ol type="1" style="margin:0pt; padding-left:0pt">
                          <li style="font-family:Cambria; font-size:12pt; margin:0pt 0pt 0pt 14.11pt; padding-left:3.89pt; text-indent:0pt">
                            <span style="font-family:宋体; font-size:12pt">抽检短信标识情况:</span>
                          </li>
                        </ol>
                        <table cellspacing="0" cellpadding="0" style="border-collapse:collapse; margin-left:0pt">
                          <tbody>
                            <tr>
                              <td style="border-bottom-color:#000000; border-bottom-style:dotted; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#bfbfbf; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:top; width:105.35pt">
                                <p style="margin:0pt; text-align:center">
                                  <span style="font-family:宋体; font-size:10.5pt; font-weight:bold; line-height: 14pt;">短信标识(代码)</span>
                                </p>
                              </td>
                              <td style="border-bottom-color:#000000; border-bottom-style:dotted; border-bottom-width:0.75pt; border-left-color:#bfbfbf; border-left-style:solid; border-left-width:0.75pt; border-right-color:#bfbfbf; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:top; width:105.35pt">
                                <p style="margin:0pt; text-align:center">
                                  <span style="font-family:宋体; font-size:10.5pt; font-weight:bold">全量条数</span>
                                </p>
                              </td>
                              <td style="border-bottom-color:#000000; border-bottom-style:dotted; border-bottom-width:0.75pt; border-left-color:#bfbfbf; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:top; width:105.35pt">
                                <p style="margin:0pt; text-align:center">
                                  <span style="font-family:宋体; font-size:10.5pt; font-weight:bold">抽检条数</span>
                                </p>
                              </td>
                            </tr>
                            <tr>
                              <td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#bfbfbf; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:dotted; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:top; width:105.35pt">
                                <p style="margin:0pt; text-align:center">
                                  <span style="font-family:宋体; font-size:10.5pt; font-weight:bold">合计</span>
                                </p>
                              </td>
                              <td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#bfbfbf; border-left-style:solid; border-left-width:0.75pt; border-right-color:#bfbfbf; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:dotted; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:top; width:105.35pt">
                                <p style="margin:0pt; text-align:center">
                                  <span style="font-family:宋体; font-size:12pt">20</span>
                                </p>
                              </td>
                              <td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#bfbfbf; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:dotted; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:top; width:105.35pt">
                                <p style="margin:0pt; text-align:center">
                                  <span style="font-family:宋体; font-size:12pt">50</span>
                                </p>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                        <p style="margin:0pt 0pt 0pt 18pt"><span style="font-family:宋体; font-size:12pt">&nbsp;</span></p>
                        <ol start="2" type="1" style="margin:0pt; padding-left:0pt">
                          <li style="font-family:Cambria; font-size:12pt; margin:0pt 0pt 0pt 14.11pt; padding-left:3.89pt; text-indent:0pt">
                            <span style="font-family:宋体; font-size:12pt">抽检人员填写(备注): </span>
                            <span style="font-family:宋体; font-size:12pt">${remark}</span>
                          </li>
                        </ol>
                      </td>
                    </tr>
                    <tr style="height:25.15pt">
                      <td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:61.2pt">
                        <p style="margin:0pt"><span style="font-family:宋体; font-size:12pt">&nbsp;</span></p>
                      </td>
                      <td colspan="4" style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:381.2pt">
                        <p style="line-height:28.6pt; margin:0pt 0pt 1pt; text-align:right">
                          <span style="font-family:宋体; font-size:12pt">报告时间:${reportTime}</span>
                        </p>
                      </td>
                    </tr>
                    <tr style="height:0pt">
                      <td style="width:72pt; border:none"></td>
                    </tr>
                  </tbody>
                </table>
                <p style="line-height:28.6pt; margin:0pt 0pt 1pt 180pt; text-align:justify; text-indent:36pt"><span style="font-family:宋体; font-size:15pt">&nbsp;</span></p>
              </div>
            </body>
          </html>
        `
        this.exportHtmlToWord(htmlContent)
      },
      exportHtmlToWord (htmlContent) {
        // 将 HTML 内容转换为 Blob 对象
        const blob = htmlDocx.asBlob(htmlContent)
        saveAs(blob, '导出.docx')
      }
    }
  }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342

推荐阅读更多精彩内容