由于后端返回了一些不必要的字段,如果直接用Layui第三方扩展导出,那些字段就会自己显示出来,有点影响美观所以前端需要过滤一下
html代码一个导出的按钮
<button class="layui-btn" id="ExportExcel">导出Excel</button>
js代码
//导出Excel按钮
var excel=LAY_EXCEL;//就是怎么写不要多想
$('#ExportExcel').on('click', function() {
$.ajax({
url: 'http://localhost:65290/api/ProduceTransfer/GetAllProduceTransferExcel',
type: "post",
headers: {
'access_token': layui.data('layuiAdmin').access_token
},
success: function(data) {
data = excel.filterExportData(data, {//这里写你要显示的字段
id:"id"
,code:"code"
,produce_code:"produce_code"
,po:"po"
,supplier_fast_code:"supplier_fast_code"
,supplier_short_name:"supplier_short_name"
,produce_name:"produce_name"
,valuation_count:"valuation_count"
,delivery_quantity_count:"delivery_quantity_count"
,acceptance_quantity_count:"acceptance_quantity_count"
,gram_weight:"gram_weight"
,material_name:"material_name"
,flute:"flute"
,material_category:"material_category"
,produce_length:"produce_length"
,produce_width:"produce_width"
,valuation_price:"valuation_price"
,valuation_amount:"valuation_amount"
,valuation_unit:"valuation_unit"
,purchase_count:"purchase_count"
,purchase_unit:"purchase_unit"
,remark:"remark"
});
data.unshift({//相对应的列名
"id": "编号",
"code": "原材料编号",
"produce_code": "生产编号",
"po": "PO编号",
"supplier_fast_code":"供应商代码",
"supplier_short_name":"供应商简称",
"produce_name": "订单名称",
"valuation_count": "数量",
"delivery_quantity_count": "已发货数量",
"acceptance_quantity_count": "验收数量",
"gram_weight": "克重",
"material_name":"原料名称",
"flute": "楞型",
"material_category": "原料类别",
"produce_length": "长",
"produce_width": "宽",
"valuation_price": "单价",
"valuation_amount": "金额",
"valuation_unit": "计价单位",
"purchase_count": "采购数量",
"purchase_unit": "采购单位",
"remark": "备注"
});
LAY_EXCEL.exportExcel(data, '导出Excel数据.xlsx', 'xlsx');
}
})
})