有人能帮我弄明白。如何导出一个html表格到excel与边框在其格式。我们的项目目前正在使用Spring,和前端部分目前正在使用引导程序。我尝试了不同的解决方案,但我对它的堆栈。
HTML语言
<div class="generateExcelFile">
<button onclick="exportTableToExcel('transactionsTable', 'SettlementTransactions')" class="btn btn-primary">
<i class="fa fa-download"></i> Export Transaction History
</button>
</div>
JavaScript语言
function exportTableToExcel(transactionsTable, filename = ''){
var downloadLink;
var dataType = 'application/vnd.ms-excel';
var tableSelect = document.getElementById(transactionsTable);
var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
// Specify file name
filename = filename?filename+'.xls':'excel_data.xls';
// Create download link element
downloadLink = document.createElement("a");
document.body.appendChild(downloadLink);
if(navigator.msSaveOrOpenBlob){
var blob = new Blob(['\ufeff', tableHTML], {
type: dataType
});
navigator.msSaveOrOpenBlob( blob, filename);
}else{
// Create a link to the file
downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
// Setting the file name
downloadLink.download = filename;
//triggering the function
downloadLink.click();
}
}
代码基于此站点:https://www.codexworld.com/export-html-table-data-to-excel-using-javascript/
- 谢谢-谢谢
1条答案
按热度按时间643ylb081#
exportTableToExcel('交易表','结算交易')
请确保 * id ='transactionsTable '* 位于表标记中。
第一个
我的代码:https://codepen.io/maulanadata/pen/KKeoOPW