关于word:word的导出发布

6次阅读

共计 1150 个字符,预计需要花费 3 分钟才能阅读完成。

const html = ` 导出的测试文字 `;
                    const htmlDom = `<!DOCTYPE html>
                                    <html>
                                    <head>
                                        <meta charset="utf-8">
                                        <meta name="viewport" content="width=device-width,initial-scale=1.0">
                                        <title> 题目 </title>
                                    </head>
                                    <body>
                                        <div class="t-l" style="width:1200px">
                                        ${html}
                                        </div>
                                    </body>
                                    </html>`;
                    const htmlDom_ = new Blob([htmlDom], {"type": "text/html;charset=utf-8"})
                    const formdata = new FormData();
                    formdata.append('file', htmlDom_, `sdf.html`);//sdf.html 是设置文件名
                    // axios({
                    //     method: 'post',
                    //     url: url,
                    //     data: formdata,
                    //     responseType: 'blob',// 这里如果不设置,下载会打不开文件
                    // }).then(res => {// console.log('download res', res);
                    // 通过后盾返回 的 word 文件流设置文件名并下载
                    const blob = new Blob([htmlDom], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'}); //application/vnd.openxmlformats-officedocument.wordprocessingml.document 这里示意 doc 类型
                    const downloadElement = document.createElement('a');
                    const href = window.URL.createObjectURL(blob); // 创立下载的链接
                    downloadElement.href = href;
                    downloadElement.download = `${data.tempName}.doc`; // 下载后文件名
                    document.body.appendChild(downloadElement);
                    downloadElement.click(); // 点击下载
                    document.body.removeChild(downloadElement); // 下载实现移除元素
                    window.URL.revokeObjectURL(href); // 开释掉 blob 对象
                    data.visible = false;// 开释掉 model
正文完
 0