1. 程式人生 > 其它 >Layui 資料表格將特定列的資料匯出為Excel檔案

Layui 資料表格將特定列的資料匯出為Excel檔案

技術標籤:Layuilayui

此方法參照官方文件,如下圖:
官方文件

一、JS table工具欄加入匯出標誌

,defaultToolbar:['filter',{
    title: '匯出全部資料' //滑鼠放在上面顯示的提示
    ,layEvent: 'exportData' //事件名,用於toolbar事件中使用
    ,icon: 'layui-icon-export' //圖示類名
}]//顯示導航欄

二、JS 新增監聽事件

//監聽頭工具欄事件
table.on('toolbar(test)', function(obj){
    switch(obj.event){
        case
'exportData': //匯出資料 $.ajax({ url: '/xxx/exportData'//查詢全部資料 ,type: 'post' ,async: false ,dataType: 'json' ,success: function (res) { table.exportFile(['產品名稱','產品類別','試驗產品型號'], res,
'xls'); //三個引數:表頭行各列名稱、資料、副檔名 } }); break; }; });

三、Controller方法

@RequestMapping("/exportData")
@ResponseBody
public List<List<String>> exportData(){
    List<List<String>> listList = new ArrayList<List<
String>>(); List<ProductAndCertInfo> proAndCert = productInfoService.getProAndCertByType(cert_type); //資料填充 //將物件取出拼接成list for (ProductAndCertInfo pro: proAndCert){ List<String> list = new ArrayList<String>(); list.add(pro.getProduct_name());//產品名稱 list.add(pro.getProduct_category());//產品類別 list.add(pro.getProduct_type());//試驗產品型號 listList.add(list); } return listList; }

匯出效果:
在這裡插入圖片描述