@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;
}
}