Javascript 单击导出HTML表
我试图通过点击链接导出一个HTML表 使用最新版本,但接受建议 当我有以下链接时,它会生成一个工作按钮来导出表。(如果我将exportButtons设置为Javascript 单击导出HTML表,javascript,jquery,Javascript,Jquery,我试图通过点击链接导出一个HTML表 使用最新版本,但接受建议 当我有以下链接时,它会生成一个工作按钮来导出表。(如果我将exportButtons设置为false,则不会生成按钮。) 但是,我需要这个链接来实际生成导出,而不是按钮。如何实现这一点?此库似乎更像是“按钮生成器”,而不是导出库:我们看到,要导出它,需要SheetJS提供的第三方库xlsx.core.js(请参阅“插件”部分) 我建议您直接使用sheetjs,这样您就可以更好地控制导出 您可以在这个链接上看到我几天前回答的工作代
false
,则不会生成按钮。)
但是,我需要这个链接来实际生成导出,而不是按钮。如何实现这一点?此库似乎更像是“按钮生成器”,而不是导出库:我们看到,要导出它,需要SheetJS提供的第三方库xlsx.core.js(请参阅“插件”部分) 我建议您直接使用sheetjs,这样您就可以更好地控制导出 您可以在这个链接上看到我几天前回答的工作代码: 无论如何,如果要继续使用此导出按钮生成器库,可以执行以下操作:
- 当我们点击当前链接时,它会生成按钮
- 您可以在当前onClick上添加以下代码
setTimeout(函数(){单击\u生成的\u按钮()},500)代码>将单击新生成的按钮
<a href="#" onclick="$('#initial_analysis').tableExport({formats: ['xlsx'], exportButtons: true}); setTimeout(function(){ click_on_the_generated_button() }, 500);"> Generate Export</a>
否则,如果您有兴趣切换到生成xlsx的主库SheetJS,下面是用于将HTML导出到xlsx的示例SheetJS代码:
<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>
表格导出
身份证件
名称
1.
约翰尼
函数doit(类型、fn、dl){
var elt=document.getElementById('data-table');
var wb=XLSX.utils.table_to_book(elt,{sheet:“sheet JS”});
返回dl?
write(wb,{bookType:type,bookst:true,type:'base64'}):
writeFile(wb,fn | | |('test.+(type | |'XLSX'));
}
功能表(pid、iid、fmt、ofile){
如果(typeof Downloadify!=“未定义”)Downloadify.create(pid{
swf:'downloadify.swf',
downloadImage:'download.png',
宽度:100,
身高:30,
文件名:ofile,数据:function(){返回doit(fmt,ofile,true);},
透明:假,
附加:false,
数据类型:“base64”,
onComplete:function(){alert('您的文件已保存!');},
onCancel:function(){alert('您已取消保存此文件');},
onError:function(){alert('您必须在文件内容中放入一些内容,否则将无法保存任何内容!';}
});
}
tableau('xlsxbtn','xportxlsx','xlsx','test.xlsx');
你的tableexport.js建议也做了同样的事情,生成了一个按钮。另一个很棒。你也有一个.csv版本吗?@HoosierCoder为我迟来的回答感到抱歉,不得不离开stackoverflow-(为了花时间陪我的宝宝;-)……关于csv导出,不确定SheetJS是否允许这样做。对于使用jquery将HTML转换为CSV,您可以看到这篇文章
<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>