生成区划脚本

    @RequestMapping(value = "/getExcel", method = RequestMethod.POST)
    public void getExcel(@RequestBody TreeDto dto) throws IOException {
        int startRow = 0;// 开始读取行数
        InputStream impIs = null;

        Resource resource = SpringContextUtils.getApplicationContext()
                .getResource("classpath:/excel/a.xls");
        InputStream inputStream = resource.getInputStream();
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet = workbook.getSheetAt(0); // 获取第一个工作表

        Object _temp = null;
        List<AreaJson> alllist = new ArrayList<>();
        // 你可以在这里添加代码来操作工作表
        int i = 0;
        while (true) {
            HSSFRow row = sheet.getRow(startRow);
            if (row == null) {
                break;
            }
            HSSFCell xh = row.getCell(0);// 区划
            // 序号
            _temp = Tools.getObjFormCell(xh);
            String xhStr = _temp.toString().trim();

            if(StringUtils.isBlank(xhStr)) {
                break;
            } else {
                xhStr += "000000";
            }

            HSSFCell mc = row.getCell(1);// 名称
            _temp = Tools.getObjFormCell(mc);
            String xhStr1 = _temp.toString().trim();
//            System.out.println(i++ + ":" +xhStr+":"+ xhStr1);
            startRow++;

            AreaJson areaJson = new AreaJson();
            areaJson.setCode(xhStr);
            areaJson.setName(xhStr1);

            alllist.add(areaJson);

        }

        List<AreaJson> jsonList = new ArrayList<>();
        for(int j = 0; j < alllist.size(); j++) {
            if(alllist.get(j).getCode().substring(2,6).equals("0000")) {
                AreaJson temp = alllist.get(j);
                jsonList.add(temp);
            }
        }

        for(int j = 0; j < jsonList.size(); j++) {
            for(int m = 0; m < alllist.size(); m++) {
                if(!alllist.get(m).getCode().substring(2,6).equals("0000") &&
                        alllist.get(m).getCode().substring(4,6).equals("00") &&
                        jsonList.get(j).getCode().substring(0,2).equals(alllist.get(m).getCode().substring(0,2))) {
                    if(jsonList.get(j).getSub() == null) {
                        jsonList.get(j).setSub(new ArrayList<>());
                    }
                    List tempList = jsonList.get(j).getSub();
                    AreaJson x = alllist.get(m);
                    x.setSub(new ArrayList<>());
//                    AreaChildren areaChildren = new AreaChildren();
//                    areaChildren.setLeaf(true);
//                    areaChildren.setName(x.getName());
//                    areaChildren.setValue(x.getValue());
                    tempList.add(x);
                }
            }
        }


        for(int j = 0; j < jsonList.size(); j++) {
            if(jsonList.get(j).getSub() != null) {
                List<AreaJson> listm = jsonList.get(j).getSub();
                for(int m = 0; m < listm.size(); m++) {
                    for(int n = 0; n < alllist.size(); n++) {
                        if(!alllist.get(n).getCode().substring(4,6).equals("00") &&
                                alllist.get(n).getCode().substring(0,4).equals(listm.get(m).getCode().substring(0,4))){
                            List x = listm.get(m).getSub();
                            AreaChildren areaChildren = new AreaChildren();
                            areaChildren.setName(alllist.get(n).getName());
                            areaChildren.setCode(alllist.get(n).getCode());
                            x.add(areaChildren);
                        }
                    }
                }
            }
        }


        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(jsonList); // objArray是对象数组

        System.out.println(json);
        inputStream.close();

    }
package com.neusoft.ehrss.nn.sl.unemployedGraduateSign.domain;

public class AreaChildren {
    private String name;
    private String code;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}
package com.neusoft.ehrss.nn.sl.unemployedGraduateSign.domain;

import java.util.List;

public class AreaJson {

    private String name;
    private String code;
    private List<AreaJson> sub;
//    private Boolean isLeaf;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public List<AreaJson> getSub() {
        return sub;
    }

    public void setSub(List<AreaJson> sub) {
        this.sub = sub;
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值