解决如何使用layui导出Excel表数据

解决如何使用layui导出Excel表数据

1.前端页面html代码展示

    <button id="importData" class="layui-btn">
            <i class="layui-icon"></i>导入Excel
    </button>

2.前端页面js代码展示

<script type="text/javascript" src="${pageContext.request.contextPath }/statics/js/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/statics/frame/layui/layui.js"></script>
  <script type="text/javascript" src="${pageContext.request.contextPath }/statics/js/index.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/statics/js/excel.js"></script>
 <script type="text/javascript" src="${pageContext.request.contextPath }/statics/js/data.js"></script>

<script type="text/javascript">
 

 // layui方法
    layui.use(['table', 'form', 'layer', 'vip_table',"element", "laypage", "upload"], function () {

        // 操作对象
        var form = layui.form
                , table = layui.table
                , layer = layui.layer
                , vipTable = layui.vip_table
                , $ = layui.jquery;
                var element = layui.element;
                var laypage = layui.laypage;
                var upload = layui.upload;//主要是这个
         
  //导出excel
        form.on('submit(uploadImg)', function(data){
            loading = layer.load(1, {shade: [0.3, '#fff']});
            var $ = layui.jquery;
            var excel = layui.excel;
            $.ajax({
                url: '${pageContext.request.contextPath }/infoSheet/getTreeList',
                dataType: 'json',
                data: {
                    datas:JSON.stringify(data.field)
                },
                success: function(res) {
                    layer.close(loading);
                    layer.msg(res.msg);
                    layer.msg(res.msg,{icon:6,time:1000});
                    // 假如返回的 res.data 是需要导出的列表数据
                    console.log(res.data);//
                    // 1. 数组头部新增表头
                    res.data.unshift({infoName: '企业名称',industryTypeName:'行业类型',infoTypeName:'企业性质',areaName:'企业区域',infoGround:'企业地址',registCapital:'注册资本',
                    	infoPhone:'联系电话',infoPostbox:'公司邮箱',infoTime:'成立时间'});
                    // 3. 执行导出函数,系统会弹出弹框
                    excel.exportExcel({
                        sheet1: res.data
                    }, '企业信息.xlsx', 'xlsx');
                },
                error:function(res){
                    layer.close(loading);
                    layer.msg(res.msg);
                    layer.msg(res.msg,{icon:5,time:1000});
                }
            });
        });
    }); 
  })
 
</script>
       

3.java后台代码

  //导出excel表
	     @ResponseBody
	     @RequestMapping("/getTreeList")
	     public Object getList(HttpServletRequest request, Model model) {

	  
	         HashMap<Object, Object> map = new HashMap<Object, Object>();

	         try {
	             List<InfoSheet> mList = new ArrayList<InfoSheet>();
	             Integer count = infoSheetService.getCount(map);

	             List<InfoSheetEntity> list = new ArrayList<InfoSheetEntity>();
	             if (count > 0) {
	                 //查询所有数据
	                 mList = infoSheetService.getSelective(map);
	                 for (InfoSheet infoSheet : mList) {
	                     //自定义一个新实体类,定义好要导出来的字段,把遍历出来的数据存放到一个新的list,因为会出现关联表的数据
	                	 InfoSheetEntity infoSheetEntity = new InfoSheetEntity();
	                	// infoSheetEntity.setInfoId(infoSheet.getInfoId());//企业ID
	                	 infoSheetEntity.setInfoName(infoSheet.getInfoName());//企业名称
	                	 //判断如果行业类型是null,就set " - "
	                	 if (infoSheet.getIndustryType()==null){
	                		 infoSheetEntity.setIndustryTypeName("-");
	                     }else {
		                	 infoSheetEntity.setIndustryTypeName(infoSheet.getIndustryType().getIndustryTypeName());//行业类型

	                     }
	                	 //判断如果企业性质是null,就set " - "
	                	 if (infoSheet.getInfoType()==null){
	                		 infoSheetEntity.setInfoTypeName("-");
	                     }else {
		                	 infoSheetEntity.setInfoTypeName(infoSheet.getInfoType().getInfoTypeName());//企业性质

	                     }
	                	 //判断如果企业性质是null,就set " - "
	                	 if (infoSheet.getArea()==null){
	                		
	                		 infoSheetEntity.setAreaName("-");
	                     }else {
		                	 //企业地区
                            infoSheetEntity.setAreaName(infoSheet.getArea().getAreaName());
	                     }
	                	 

	                	 infoSheetEntity.setInfoGround(infoSheet.getInfoGround());//企业地址
	                	 infoSheetEntity.setRegistCapital(infoSheet.getRegistCapital());//注册资本
	                	 infoSheetEntity.setInfoPhone(infoSheet.getInfoPhone());//联系电话
	                	 infoSheetEntity.setInfoPostbox(infoSheet.getInfoPostbox());//公司邮箱
	                	
	                	 infoSheetEntity.setInfoTime(infoSheet.getInfoTime());//成立时间
	                	 
	                	
	                	 
	                	 list.add(infoSheetEntity);
	                	 
	                	
	                     //时间格式可能不对,备用
//	                 DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//	                 String time = format.format(sellDealEntity.getSdTime());
	                     //根据类型 set 对应的值
	                     //switchType(sellDealEntity,sellDeal);
	                     //sellDeal.setSdWater(sellDealEntity.getSdWater());
	                     //判断如果订单是null,就set " - "
	                    
	                 }
	             }

	             map.put("code", 0);
	             map.put("msg", "导出成功");
	             map.put("count", count);
	             map.put("data", list);
	         }catch (Exception e){
	             map.put("code", 1);
	             map.put("msg", "导出失败,请稍后重试!");
	         }


	         return JSONObject.toJSON(map);
	     }
	     

4.感谢你的浏览访问

工作几年,略有心得,然很少整理成文,平常工作中浏览了不少道友的好文章,受益良多。
顾也开始写一些文章, 希望能与各位道友分享进步 !!!!
如果有用,欢迎各位点赞评论,若有不足,亦可指正!!!

已标记关键词 清除标记
![图片说明](https://img-ask.csdn.net/upload/202002/11/1581428152_503071.png) JSP代码 ``` %> <!DOCTYPE HTML> <html> <style> .layui-input { width: 120px; height: 25px; } .layui-form-select { width: 120px; height: 25px; } </style> </head> <body> <input type="hidden" id="orgId" value="${orgId}"> <input type="hidden" id="bool" value="${bool}"> <input type="hidden" id="currentYear" value="${year}"> <div class="layui-form" style="padding: 5px 2px 5px 0px;background-color: #D4D4D4;margin-top: 0px;margin-bottom: -10px"> <div class="layui-form-item" style="margin-bottom: 0px"> <div class="layui-inline">年份: </div> <div class="layui-inline"> <input class="layui-input" name="year" id="year" autocomplete="off" style="height: 25px;width: 80px"> </div> <div class="layui-inline">调度员角色: </div> <div class="layui-inline"> <select id="dispatchRole" name="dispatchRole" xm-select="dispatchRole" xm-select-create="" xm-select-skin="normal" xm-select-search="" xm-select-max="30"> <option value="">所有</option> <option value="1">操作人</option> <option value="2">审核人</option> <option value="3">值班负责人</option> </select> </div> <div class="layui-inline"> <button class="layui-btn layui-btn-xs" data-type="reload" id="search">搜索</button> <button type="button" lay-submit="" class="layui-btn layui-btn-xs" id="exportWord" name="exportWord"> <i class="layui-icon"></i>导出Word</button> </div> </div> </div> <div id="docx"> <div class="WordSection1"> </div> </div> <script type="text/html" id="statisticsTool"> <div class="layui-btn-container" id="layerDemo"> <button data-method="offset" data-type="auto" class="layui-btn layui-btn-xs" lay-event="addUser" id="dddd">配置人员 </button> </div> </script> <table id="statisticsTable" lay-filter="statisticsTable" class="layui-hide" style="width: 500px;padding-top: -20px"></table> </body> </html> ``` 我要把layui自带的三个按钮隐藏,帮配置人员移到上面一排
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页