table2excel là một plugin của jQuery giúp bạn xuất dữ liệu trong bảng HTML sang file Excel. Các bước cài đặt plugin đơn giản và nhanh chóng giúp bạn chuyển đổi dữ liệu một cách dễ dàng.
Các bước sử dụng:
- Thêm thư viện jQuery, sau đó thêm jQuery table2excel plugin vào file HTML như sau:
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <script src="//cdn.rawgit.com/rainabba/jquery-table2excel/1.1.0/dist/jquery.table2excel.min.js"></script>
- Tạo button Export để xuất dữ liệu. Các bạn có thể dùng cách khác thay cho sử dụng button:
<button>Export</button>
- Gọi function đối với table để tiến hành xuất dữ liệu:
$("button").click(function(){ $("#table2excel").table2excel({ // exclude CSS class exclude: ".noExl", name: "Worksheet Name", filename: "SomeFile" //do not include extension }); });
- Bạn có thể thêm CSS class noExl cho các thẻ tr để bỏ qua việc xuất dữ liệu cho bảng đó:
<tr class="noExl"> <th>#</th> <th>Column heading</th> <th>Column heading</th> <th>Column heading</th> </tr>
Các bạn có thể download và xem hướng dẫn sử dụng ở đây
Nguồn: https://www.jqueryscript.net/table/Export-Html-Table-To-Excel-Spreadsheet-using-jQuery-table2excel.html