标签:double service format apt evel 分区 ++ eric 成熟
解决方案: 因为是树形结构,那么表单的数据分区读取,先读取父级的数据存到数据库,再存入子类数据。
String originalFileName = file.getOriginalFilename();
InputStream is = file.getInputStream();
int version = 0;
if (originalFileName.endsWith(".xls")) {
version = 2003;
}else if (originalFileName.endsWith(".xlsx")) {
version = 2007;
}else {
throw new Exception("Incorrect file format,Only allowed ‘.xls,.xlsx‘ extension");
}
Workbook workbook = null;
switch (version) {
case 2003:
POIFSFileSystem fs = new POIFSFileSystem(new BufferedInputStream(is));
workbook = new HSSFWorkbook(fs);
break;
case 2007:
workbook = new XSSFWorkbook(new BufferedInputStream(is));
break;
}
int sheetIndex = workbook.getSheetIndex("云运维成熟度评估要素");
Sheet sheet = workbook.getSheetAt(sheetIndex);
List<OcScope> ocScopeListOne = new ArrayList<OcScope>();
String b = ""; String c = ""; int e =0; double w = 0D;
for (Row row : sheet) {
OcScope ocScope = new OcScope();
int rowNum = row.getRowNum();
if(e==1){break;} //当e==1时跳出循环
if(rowNum <= 1){//跳出第一行 一般第一行都是表头没有数据意义
continue;
}
int startCell = 1; //
if(row.getCell(startCell)!=null ){//第1列数据
row.getCell(startCell).setCellType(Cell.CELL_TYPE_STRING);
if(!row.getCell(startCell).getStringCellValue().equals("")){
//判空赋值的好处是解决跨行数据 如果下一行没有读到就继续将上一行的数据写入实体
if(row.getCell(startCell).getStringCellValue().equals("0")){ //如果数据为0跳出循环
e=1;
}else{
b = row.getCell(startCell).getStringCellValue(); //不是就赋值
}
}
}
startCell++;
if(row.getCell(startCell) == null){
e=1;
}
if(row.getCell(startCell)!=null){//第15列
row.getCell(startCell).setCellType(Cell.CELL_TYPE_NUMERIC);
w = row.getCell(startCell).getNumericCellValue();
}
//解决跨行问题
ocScope.setScopeName(b); //只有不为空的时候b才会被从新赋值
ocScope.setParentName(null);
ocScope.setLevel("1");
ocScope.setWeights(w);//只有不为空的时候W才会被从新赋值
ocScopeListOne.add(ocScope);
}
Map<String, OcScope> ocScopeMapOne = Maps.newHashMap();
Map<String, OcScope> ocScopeMapTwo = Maps.newHashMap();
for (OcScope ocScope : ocScopeListOne) {
ocScopeMapOne.put(ocScope.getScopeName(), ocScope);
}
OcScope ocScopeOne = new OcScope();
ocScopeOne.setLevel("1");
List<OcScope> ocScopelistOne= ocScopeService.findList(ocScopeOne); //查询出一级的所有数据
for (String key : ocScopeMapOne.keySet()) { //遍历map
for (OcScope ocScope1 : ocScopelistOne) {
if(!ocScopeMapOne.get(key).getScopeName().equals(ocScope1.getScopeName())){ //与一级的list数据进行对比没有相同的就存入新数据
// dao.insert(ocScopeMapOne.get(key));
}
}
}
b=""; c="";w=0; e =0; //重新赋值
for (Row row : sheet) {
OcScope ocScope = new OcScope();
int rowNum = row.getRowNum();
if(e==1){break;}
if(rowNum <= 1){//跳出第一行 一般第一行都是表头没有数据意义
continue;
}
int startCell = 1; //
if(row.getCell(startCell)!=null ){//第1列数据
row.getCell(startCell).setCellType(Cell.CELL_TYPE_STRING);
if(!row.getCell(startCell).getStringCellValue().equals("")){
b = row.getCell(startCell).getStringCellValue();
}
}
startCell++;
startCell++;
if(row.getCell(startCell) == null){
e=1;
}
if(row.getCell(startCell)!=null){//第3列数据
row.getCell(startCell).setCellType(Cell.CELL_TYPE_STRING);
if(!row.getCell(startCell).getStringCellValue().equals("")){
if(row.getCell(startCell).getStringCellValue().equals("0")){
e=1;
}else{
c = row.getCell(startCell).getStringCellValue();
}
}
}
startCell++;
if(row.getCell(startCell)!=null){//第4列
row.getCell(startCell).setCellType(Cell.CELL_TYPE_NUMERIC);
if(row.getCell(startCell).getNumericCellValue()!=0.0){
w = row.getCell(startCell).getNumericCellValue();
}
}
// 转换为Integer类型
//解决跨行问题
ocScope.setParentName(b); //这里需要存一个父级的名子
ocScope.setScopeName(c);
ocScope.setLevel("2");
ocScope.setWeights(w);
ocScopeListTwo.add(ocScope);
}
for (OcScope ocScope : ocScopeListTwo) { //去重处理
ocScopeMapTwo.put(ocScope.getScopeName(), ocScope);
}
OcScope ocScopeTwo = new OcScope();
ocScopeTwo.setLevel("2");
List<OcScope> ocScopelistTwo= ocScopeService.findList(ocScopeTwo); //取出二级的List数据
for (String key : ocScopeMapTwo.keySet()) { //遍历map
for (OcScope ocScope1 : ocScopelistOne) {
if(ocScopeMapTwo.get(key).getParentName().equals(ocScope1.getScopeName())){ //给对象赋值父ID根据值判断
ocScopeMapTwo.get(key).setParentId(ocScope1.getId());
}
}
}
System.err.println("ocScopeMapTwo+Pid"+ocScopeMapTwo);
for (String key : ocScopeMapTwo.keySet()) { //遍历map
for (OcScope ocScope1 : ocScopelistTwo) {
if(!ocScopeMapTwo.get(key).getScopeName().equals(ocScope1.getScopeName())){//数据库没有相关数据就存入进去
// dao.insert(ocScopeMapOne.get(key));
}
}
}
PoiExcel导入实现(解决跨行问题以及03和07版本问题)
标签:double service format apt evel 分区 ++ eric 成熟
原文地址:https://www.cnblogs.com/jiajialeps/p/10182645.html