将Html表格导出为Excel格式

vshtjzan  于 2022-11-27  发布在  其他
关注(0)|答案(1)|浏览(168)

有人能帮我弄明白。如何导出一个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/

  • 谢谢-谢谢
643ylb08

643ylb081#

exportTableToExcel('交易表','结算交易')
请确保 * id ='transactionsTable '* 位于表标记中。
第一个
我的代码:https://codepen.io/maulanadata/pen/KKeoOPW

相关问题