需求:將如下網頁中的所有表格一次導入到Excel文件中。
方法:使用jQuery的tableExport.js插件,可以將網頁中指定的table表格數據導出到Excel文件,而不需要經過后台。
操作步驟:
1. 在需要導出表格的頁面引入以下兩個js文件:
<script type="text/javascript" src="js/excel/FileSaver.min.js"></script> <script type="text/javascript" src="js/excel/tableExport.min.js"></script>
2. 點擊導出按鈕時,調用以下代碼即可將多個table表格數據導出:
$('table').tableExport({ type:'excel', ignoreColumn:[5],//從1開始,忽略第5列 mso: {//若table表格中使用了以下指定的樣式屬性,則將該樣式同步到Excel中(可以保留表格原有的樣式到Excel中) styles:['background-color', 'border-top-color', 'border-left-color', 'border-right-color', 'border-bottom-color', 'border-top-width', 'border-left-width', 'border-right-width', 'border-bottom-width', 'border-top-style', 'border-left-style', 'border-right-style', 'border-bottom-style', 'color'] } });
若需要導出指定的某個table表格,將$('table')改為$('#表格ID')即可。
導出的數據在Excel中的效果如下圖:
插件的下載及其它功能的使用方法請看:
https://github.com/hhurz/tableExport.jquery.plugin