Chủ Nhật, 17 tháng 2, 2019

Export excel với jqueryTable To Excel (XSL) Converter

Đây là bộ thư viện jquery hỗ trợ việc xuất dữ liệu website ra file excel.Với bộ thư viện này thì việc xuất dữ liệu ra file excel sẽ trở lên đơn giản hơn bao giờ hết. 

Sau đây là các bước sử dụng jquery Table To Excel (XSL) Converter.

Bước 1: Tải bộ source tại link sau https://www.jqueryscript.net/table/table-excel-xsl-converter.html

Bước 2: Khai báo 2 file script sau ở thẻ <head></head>:

2        integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
3        crossorigin="anonymous">
4</script>
5<script src="jquery.tableToExcel.js"></script>

Bước 3: Tạo danh sách dữ liệu thông qua thẻ <table></table>. Ví dụ như dưới:

01<table class="table table-bordered table-striped">
02  <thead>
03    <tr>
04      <th>Firstname</th>
05      <th>Lastname</th>
06      <th>Email</th>
07    </tr>
08  </thead>
09  <tbody>
10    <tr>
11      <td>John</td>
12      <td>Doe</td>
13      <td>john@example.com</td>
14    </tr>
15    <tr>
16      <td>Mary</td>
17      <td>Moe</td>
18      <td>mary@example.com</td>
19    </tr>
20    <tr>
21      <td>July</td>
22      <td>Dooley</td>
23      <td>july@example.com</td>
24    </tr>
25  </tbody>
26</table>

Bước 4: Kích hoạt hàm thực thi export excel bằng lệnh sau:
$('table').tblToExcel();
Bạn có thể đặt lệnh này trong event onclick của button để khi click vào button đó thì file excel sẽ được tải xuống.
Ví dụ:
<button type="button" onclick="$('table').tblToExcel();">Export To Excel</button>
Chúc các bạn thành công.


EmoticonEmoticon