Export HTML Table Data to Excel

Export data to Excel is very useful on data list for nearly every web application. The export feature helps to download the data list as a file format for offline use. Excel format is ideal for exporting data in a file. Mostly the server-side method is used for export data to excel using PHP. But if you want a client-side solution to export table data to excel, it can be easily done using JavaScript.

The client-side export functionality makes the web application user-friendly. Using JavaScript, the HTML table data can be easily exported without page refresh. In this tutorial, we will show you how to export HTML table data to excel using JavaScript. The JavaScript export functionality can be used in the member list, product list, or other lists to download data list in excel file format.

Export HTML Table Data to Excel

JavaScript Code:
The exportTableToExcel() function convert HTML table data to excel and download as XLS file (.xls).

  • tableID – Required. Specify the HTML table ID to export data from.
  • filename – Optional. Specify the file name to download excel data.
function exportTableToExcel(tableID, filename = ''){
    var downloadLink;
    var dataType = 'application/vnd.ms-excel';
    var tableSelect = document.getElementById(tableID);
    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();
    }
}

HTML Table Data:
The HTML table contains some users data with some basic fields like name, email, country.

<table id="tblData">
    <tr>
        <th>Name</th>
        <th>Email</th>
        <th>Country</th>
    </tr>
    <tr>
        <td>John Doe</td>
        <td>john@gmail.com</td>
        <td>USA</td>
    </tr>
    <tr>
        <td>Michael Addison</td>
        <td>michael@gmail.com</td>
        <td>UK</td>
    </tr>
    <tr>
        <td>Sam Farmer</td>
        <td>sam@gmail.com</td>
        <td>France</td>
    </tr>
</table>

The button triggers exportTableToExcel() function to export HTML table data using JavaScript.

<button onclick="exportTableToExcel('tblData')">Export Table Data To Excel File</button>

If you want to export data with the custom file name, pass your desired file name in the exportTableToExcel() function.

<button onclick="exportTableToExcel('tblData', 'members-data')">Export Table Data To Excel File</button>

Conclusion

Our example code helps you to add export functionality in the table data without any third-party jQuery plugin or server-side script. You can easily export the table data using minimal JavaScript code. Also, the functionality of the example code can be extended as per your needs.

 

 

使用jqueryscript

https://www.jqueryscript.net/table/Exporting-Html-Tables-To-CSV-XLS-XLSX-Text-TableExport.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将HTML表格导出到Excel并支持Excel的函数,可以使用JavaScript。 首先,我们需要在HTML中创建一个按钮,用于触发导出操作: ```html <button onclick="exportToExcel()">导出到Excel</button> ``` 然后,在JavaScript中,我们需要编写一个函数来导出HTML表格到Excel。可以使用第三方库如`tableexport.js`来实现这个功能。首先,我们需要在HTML文件中引入该库: ```html <script src="tableexport/tableexport.js"></script> ``` 接下来,在JavaScript代码中定义一个`exportToExcel`函数并在该函数中使用`TableExport`库来导出表格为Excel文件,并支持Excel的函数: ```javascript function exportToExcel() { var table = document.getElementById("myTable"); // 获取表格元素 // 创建 TableExport 对象 var tableExport = new TableExport(table, { exportButtons: false, // 不显示导出按钮 filename: 'Excel文件名', // 设置导出文件名 sheetname: '工作表名', // 设置工作表名 exportFunction: 'xlsx' // 设置导出格式为Excel }); var exportData = tableExport.getExportData(); // 获取导出数据 var xlsxData = exportData.myTable.xlsx; // 获取Excel文件的二进制数据 saveXlsxToDisk(xlsxData); // 将二进制数据保存为Excel文件 } function saveXlsxToDisk(xlsxData) { var blob = new Blob([xlsxData], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}); var url = URL.createObjectURL(blob); var a = document.createElement("a"); a.href = url; a.download = 'Excel文件名.xlsx'; // 设置下载文件名 a.click(); URL.revokeObjectURL(url); } ``` 以上代码中,`myTable`是要导出的HTML表格的id,可以根据实际情况进行修改。`filename`和`sheetname`分别是导出的Excel文件名和工作表名,可以根据需要自行设置。 当用户点击"导出到Excel"按钮时,就会触发`exportToExcel`函数,该函数会将表格导出为Excel文件并保存到磁盘上。 需要注意的是,由于涉及到文件下载,所以该方法需要在浏览器环境中才能使用,而不能在服务器端运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值