[英]Exporting HTML table on click
我试图在单击链接时导出HTML表。
使用https://tableexport.v5.travismclarke.com/,但欢迎您提出建议
当我具有以下链接时,它将生成一个工作按钮以导出表。 (如果将exportButtons设置为false
,则不会生成按钮。)
<a href="#" onclick="$('#initial_analysis').tableExport({formats: ['xlsx'], exportButtons: true});"> Generate Export</a>
但是,我需要此链接才能实际生成导出而不是按钮。 我该如何实现?
该库似乎更像是“按钮生成器”,而不是导出库:我们看到,要导出该库,需要使用SheetJS的第三方库xlsx.core.js(请参阅“附加组件”部分)
我建议您直接使用sheetjs,您将对导出有更多控制。
您可以看到我几天前在此链接上回答的工作代码: 使用sheets.js插件将HTML表转换为纯JavaScript中的Excel文件
无论如何,如果您想继续使用此导出按钮生成器库,可以这样做:
setTimeout(function(){ click_on_the_generated_button() }, 500);
点击新生成的按钮... 所以你可以这样
<a href="#" onclick="$('#initial_analysis').tableExport({formats: ['xlsx'], exportButtons: true}); setTimeout(function(){ click_on_the_generated_button() }, 500);"> Generate Export</a>
否则,如果您有兴趣切换到生成xlsx的主库SheetJS,下面是一个示例SheetJS代码,用于将HTML导出到xlsx:
<script type="text/javascript" src="//unpkg.com/xlsx/dist/shim.min.js"></script>
<script type="text/javascript" src="//unpkg.com/xlsx/dist/xlsx.full.min.js"></script>
<script type="text/javascript" src="//unpkg.com/blob.js@1.0.1/Blob.js"></script>
<script type="text/javascript" src="//unpkg.com/file-saver@1.3.3/FileSaver.js"></script>
<div id="container2">
<title>SheetJS Table Export</title>
<table id="data-table">
<tr>
<td>ID</td>
<td>Name</td>
</tr>
<tr>
<td>1</td>
<td>Johnny</td>
</tr>
</table>
</div>
<p id="xportxlsx" class="xport"><input type="submit" value="Export to XLSX!" onclick="doit('xlsx');"></p>
<script type="text/javascript">
function doit(type, fn, dl) {
var elt = document.getElementById('data-table');
var wb = XLSX.utils.table_to_book(elt, {sheet:"Sheet JS"});
return dl ?
XLSX.write(wb, {bookType:type, bookSST:true, type: 'base64'}) :
XLSX.writeFile(wb, fn || ('test.' + (type || 'xlsx')));
}
function tableau(pid, iid, fmt, ofile) {
if(typeof Downloadify !== 'undefined') Downloadify.create(pid,{
swf: 'downloadify.swf',
downloadImage: 'download.png',
width: 100,
height: 30,
filename: ofile, data: function() { return doit(fmt, ofile, true); },
transparent: false,
append: false,
dataType: 'base64',
onComplete: function(){ alert('Your File Has Been Saved!'); },
onCancel: function(){ alert('You have cancelled the saving of this file.'); },
onError: function(){ alert('You must put something in the File Contents or there will be nothing to save!'); }
});
}
tableau('xlsxbtn', 'xportxlsx', 'xlsx', 'test.xlsx');
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.