导出word文件 表格file-saver、html-docx-js-typescript

根据html代码转换为word文件
1、下载file-saver、html-docx-js-typescript

npm install file-saver html-docx-js-typescript

2、

<span @click="downloadWordWithHtmlString()">导出doc</span>
<div id="eltable" ref="eltable" style="display: none">
	<table
	 border="1"
     cellspacing="0"
     width="600"
     style="font-size: 12px; color: #000; text-align: center"
    >
    	<tr height="50">
        	<td>1</td>
        	<td>2</td>
        </tr>
        <tr height="50">
        	<td>1-1</td>
            <td>1-2</td>
        </tr>
        <tr height="50">
            <td>2-1</td>
            <td>2-2</td>
        </tr>
    </table>
</div>
import { saveAs } from 'file-saver'
import { asBlob } from 'html-docx-js-typescript'
downloadWordWithHtmlString() {
	// let eltable = this.$refs.eltable
    // let html = eltable.$el.innerHTML
    let html = document.getElementById('eltable').innerHTML
    let name = `${this.fieldName}-条件信息`
    let htmlString = `
    	
        
        
        
        Document
        
        
        ${html}
        
        
    `
    asBlob(htmlString).then((data) => {
        saveAs(data, `${name}.doc`)
    })
}

你可能感兴趣的:(javascript,word,html)