preface

Recently, this project involves the function of exporting Excel tables. The background management uses the framework of Layui, which is generally very convenient and saves a lot of development time.

Looking at the community forum on Layui’s official website, many people say that the export function of Layui can only export the data of the current page. In addition, part of the data is through the data correlation table query out, using layui’s own export data will display (Object), let people very headache.

So to use the plug-in, very simple, the following url, download the file.

This is the file that the plug-in must use. Pay attention to the path of the imported file.

Here to provide you with a website, you can download here, which also has a tutorial: github.com/wangerzi/la… To show you the project background: the order number, for example, is queried from the associated order table

Go directly to the code:

The JSP:

This is an export action button:

<button type="button" lay-submit="" class="layui-btn layui-btn-warm" lay-filter="uploadImg"> <i Layui - class = "icon" >  < / I > export Excel < / button > layui. Use ([' layer ', 'form', 'table', 'laydate', 'jquery']. function () { var $ = layui.jquery, layer = layui.layer, form = layui.form, laydate = layui.laydate, table = layui.table; Var loading; Loading = layer.load(1, {shade: [0.3, '# FFF ']}); Form. On ('submit(uploadImg)', function(data){loading = layer.load(1, {shade: [0.3, '# FFF ']}); var $ = layui.jquery; var excel = layui.excel; $.ajax({ url: '${WEB_URL}sellDeal/getTreeList', dataType: 'json', data: { datas:JSON.stringify(data.field) }, success: function(res) { layer.close(loading); layer.msg(res.msg); Console. log(res.data); // If the res.data returned is the list of data to export; // // 1. Add res.data.unshift({sdId: 'ID',sdMoney: 'transaction' sdTime: 'trading hours, type:' transaction type, sdWater: 'transaction flow number, the order: "order number"}); Excel. ExportExcel ({sheet1: res.data}, 'sheet.xlsx ',' XLSX ') is displayed. }, error:function(res){ layer.close(loading); layer.msg(res.msg); }}); }); });Copy the code