其他分享
首页 > 其他分享> > 浏览器请求后端导出excle

浏览器请求后端导出excle

作者:互联网

使用的是网友的的gitHub封装好的代码

    <dependency>
      <groupId>com.github.andyczy</groupId>
      <artifactId>java-excel-utils</artifactId>
      <version>4.0</version>
    </dependency>

说明地址:https://github.com/andyczy/czy-nexus-commons-utils/blob/master/README-Andyczy.md

 /**
     * 获取数据导出excle
     *
     * @param response
     * @param dateYearMonth
     * @throws Exception
     */
    @Override
    public void getExcle(HttpServletResponse response, String dateYearMonth) throws Exception {
        List<ClaimTagDTO> list = getClaimTag(dateYearMonth);
        ExcelUtils excelUtils = ExcelUtils.initialization();
        // 数据列表
        List<List<String[]>> dataList = Lists.newArrayList();
        //数据列表  因为需要定义表头信息 所以需要先定义 然后将表头信息添加进去 然后将数据追加拼接
        List<String[]> stringList = Lists.newArrayList();
        // 单行数据内容
        String[] valueString = new String[]{};
        // excel表头
        String[] headerString = new String[]{
                "XX", "分管XX", "职能XXXX人", "南京XXX车间", "动XXXXX",
                "南京XX车间", "蚌埠XX车间", "南京北XX车间", "南京东XX车间", "南京整XX车间", "蚌埠XX备车间",
                "南京东整XX车间", "XX车间", "救援车间", "生活服务中心"};
        stringList.add(headerString);
        //循环真正需要导出的数据
        for (ClaimTagDTO claimTagDTO : list) {
            // 根据riskid查询出riskName
            QRisk qRisk = QRisk.risk;
            String name = jpaQueryFactory.select(qRisk.name)
                    .from(qRisk)
                    .where(qRisk.id.eq(claimTagDTO.getRiskId()))
                    .fetchOne();
            valueString = new String[]{
                    name,
                    claimTagDTO.getHeadOfEachDivision(),
                    claimTagDTO.getClaimByfunctionalSection(),
                    claimTagDTO.getUsingTheWorkshopOfNJD(),
                    claimTagDTO.getUsingTheWorkshopOfBulletTrain(),
                    claimTagDTO.getUsingTheWorkshopOfNJ(),
                    claimTagDTO.getUsingTheWorkshopOfBB(),
                    claimTagDTO.getUsingTheWorkshopOfNJB(),
                    claimTagDTO.getMaintenanceWorkshopOfNJD(),
                    claimTagDTO.getRepairStandbyWorkshopOfNJ(),
                    claimTagDTO.getRepairStandbyWorkshopOfBB(),
                    claimTagDTO.getRepairStandbyWorkshopOfNJD(),
                    claimTagDTO.getEquipmentWorkshop(),
                    claimTagDTO.getRescueWorkshop(),
                    claimTagDTO.getOfficeServiceCenter()
            };
            stringList.add(valueString);
        }
        // 将所有的信息哦存进dataList中
        dataList.add(stringList);

        response.reset();
        excelUtils.setResponse(response);
        excelUtils.setDataLists(dataList);
        excelUtils.setSheetName(new String[]{"安全风险认领"});
        excelUtils.setFileName("安全风险认领");
        excelUtils.exportForExcelsOptimize();

    }

前端:由于Ajax回调函数不能处理  所以在前端写了一个隐藏起来的form表单当作 条件载体 

<a class="btn size-M btn-primary radius" href="javascript:void(0);" id="export"><i class="Hui-iconfont">&#xe644;</i> 导出</a>

<form id='downloadR' class='hidden' method='get' action="../../xxxx/getExcle">
    <input id="dateYearMonthR" type="text" name="dateYearMonth"/>
</form>

然后js

$('#export').click(function () {
        var dateYearMonth = $('#dateYearMonth1').val();
        if (dateYearMonth) {
            //给表单里的内容填充   传到后台当作条件进行数据库查询
            $('#dateYearMonthR').val(dateYearMonth);
            $('#downloadR').submit();
        } else {
            layer.msg('请选择已经添加过的认领单!',{icon:0, time: 3000});
        }
    });

 

标签:浏览器,String,excelUtils,claimTagDTO,dateYearMonth,导出,XX,车间,excle
来源: https://blog.51cto.com/u_13993537/2990024