【发布时间】:2022-11-14 19:25:49
【问题描述】:
我正在尝试将我的 html 导出为 word。我的 HTML 页面有一个表格,当我将其导出为 word 时,它可以工作,但我的表格不再有边框。或者如何使用 js 将特定样式添加到我的表格中?
这是我的代码:
<div align="right">
<a class="btn" onclick="exportfile('exportContent', 'test');">save</a>
<a class="btn" href="/lab_device/add/">add</a>
</div>
</div>
<div class="card-body" id="exportContent">
<div class="table-responsive">
<table class="" width="100%" cellspacing="0">
<thead>
<tr style ="align-items: left;">
<th>1</th>
<th>2</th>
<th>3</th>
<th>op</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ a }}</td>
<td>{{ a }}</td>
<td>{{ a }}</td>
<td>
<a class="btn" href="/lab_device//edit/">edit</a>
<a class="btn" href="/lab_device/delete/">delete</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<script>
function exportfile(element, filename=''){
var prehtml ="<html xmins:0='urn:schemas-microsoft-com:office:office' xmins:w='urn:schemas-microsoft-com:office:office'>";
var posthtml = "</body></table></html>";
var html = prehtml+document.getElementById(element).innerHTML+posthtml;
var blob = new Blob(['\ufeff', html],{
type: 'application/msword'
});
var url = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(html)
filename = filename?filename+'.doc': 'document.doc';
var downloadLink = document.createElement("a");
document.body.appendChild(downloadLink);
if(navigator.msSaveOrOpenBlob){
navigator.msSaveOrOpenBlob(blob, filename);
}else{
downloadLink.href = url;
downloadLink.download = filename;
downloadLink.click();
}
}
</script>
先感谢您!
【问题讨论】:
-
您没有导出到 Word。您正在导出 HTML,声称 HTML 是 Word,并希望用户拥有的用于阅读 Word 文档的任何软件都能够加载错误标记的 HTML 文档。
标签: javascript html