shuishen
2021-09-07 d012d12ca32309ce3867511efc3b33a5790e9753
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package org.springblade.modules.riverway.service.impl;
 
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springblade.modules.riverway.entity.RiverwayR;
import org.springblade.modules.riverway.mapper.RiverwayMapper;
import org.springblade.modules.riverway.service.RiverwayService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.List;
 
@Service
@DS("slaves")
public class RiverwayServiceImpl extends ServiceImpl<RiverwayMapper, RiverwayR> implements RiverwayService {
 
    @Override
    public void upLoadFile(MultipartFile file) {
        try{
            InputStream in = file.getInputStream();
            Workbook workbook = null;
            try{
                workbook = new HSSFWorkbook(in);
            }catch(Exception e){
                workbook = new XSSFWorkbook(in);
            }
 
 
            int numberOfSheets = workbook.getNumberOfSheets();
            //第一个sheet
            for (int j=0;j<numberOfSheets;j++) {
                Sheet sheet1 = workbook.getSheetAt(j);
 
                String sheetName = sheet1.getSheetName();
                String temp[]=sheetName.split("-",1);
 
                /*//获取当前时间戳作为水面线ID
                Long waterID = new Date().getTime();
                //获取sheet名称作为水面线数据录入
                waterLine.setId(waterID);
                waterLine.setWaterline(temp[0]);
                if(temp.length >1){
                    waterLine.setCjtime(temp[1]);
                }*/
 
                //有效行数
                int rowNubms = sheet1.getPhysicalNumberOfRows();
                for (int i = 1; i < rowNubms; i++) {
                    Row rowi = sheet1.getRow(i);
                    if (rowi == null) {
                        continue;
                    }
 
                    RiverwayR riverwayR = new RiverwayR();
 
                    Cell cell1 = rowi.getCell(0);//河段编码
                    if(cell1.getCellType() == Cell.CELL_TYPE_BLANK) {
                        continue;
                    }
                    Cell cell2 = rowi.getCell(1);//河段名称
                    Cell cell3 = rowi.getCell(2);//河名
                    Cell cell4 = rowi.getCell(3);//河段类型
                    Cell cell5 = rowi.getCell(4);//河段长
                    Cell cell6 = rowi.getCell(5);//起点经度
                    Cell cell7 = rowi.getCell(6);//起点纬度
                    Cell cell8 = rowi.getCell(7);//终点经度
                    Cell cell9 = rowi.getCell(8);//终点纬度
                    Cell cell10 = rowi.getCell(9);//关联文站编码
                    Cell cell11 = rowi.getCell(10);//关联水文站起点距
                    Cell cell12 = rowi.getCell(11);//默认水面线
 
                    //此处判断为null,以防get值的时候 报空指针
                    DecimalFormat df = new DecimalFormat("0");
 
                    String hdbm = (cell1 != null) ? getCellValue(cell1) : null;
                    String hdmc = (cell2 != null) ? getCellValue(cell2) : null;
                    String hm = (cell3!= null) ? getCellValue(cell3) : null;
                    String hdlx = (cell4 != null) ? getCellValue(cell4) : null;
                    String hdc = df.format(cell5.getNumericCellValue());
                    String qdjd = (cell6 != null) ? getCellValue(cell6) : null;
                    String qdwd = (cell7 != null) ? getCellValue(cell7) : null;
                    String zdjd = (cell8 != null) ? getCellValue(cell8) : null;
                    String zdwd = (cell9 != null) ? getCellValue(cell9) : null;
                    String glbm = (cell10 != null) ? getCellValue2(cell10) : null;
                    String glqdj = (cell11 != null) ? getCellValue(cell11) : null;
                    String mrsmx = (cell12 != null) ? getCellValue(cell12) : null;
 
                    riverwayR.setRiverCode(hdbm);
                    riverwayR.setRiverway(hdmc);
                    riverwayR.setRiverName(hm);
                    riverwayR.setRiverType(hdlx);
                    riverwayR.setRiverLength(Integer.parseInt(hdc));
                    riverwayR.setRiverOrigin(qdjd);
                    riverwayR.setQdLat(qdwd);
                    riverwayR.setDestination(zdjd);
                    riverwayR.setZdLat(zdwd);
                    riverwayR.setGlCode(glbm);
                    riverwayR.setGlQdj(glqdj);
                    riverwayR.setDefaultSmx(mrsmx);
 
                    //河段新增
                    RiverwayServiceImpl.super.save(riverwayR);
 
                    in.close();  //关闭流
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @Override
    public List selectName() {
        return baseMapper.selectName();
    }
 
    private String getCellValue2(Cell cell){
        String value ="";
        switch (cell.getCellType()){
            case Cell.CELL_TYPE_STRING:
                value = cell.getStringCellValue();
                break;
            case  Cell.CELL_TYPE_NUMERIC:
                DecimalFormat df = new DecimalFormat("0");
                value = df.format(cell.getNumericCellValue());
                break;
            case  Cell.CELL_TYPE_BLANK:
                value = "";
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                value = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_ERROR:
                value = String.valueOf(cell.getErrorCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                value = String.valueOf(cell.getCellFormula());
                break;
        }
        return value;
    }
 
    private String getCellValue(Cell cell){
        String value ="";
        switch (cell.getCellType()){
            case Cell.CELL_TYPE_STRING:
                value = cell.getStringCellValue();
                break;
            case  Cell.CELL_TYPE_NUMERIC:
                value = String.valueOf(cell.getNumericCellValue());
                break;
            case  Cell.CELL_TYPE_BLANK:
                value = "";
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                value = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_ERROR:
                value = String.valueOf(cell.getErrorCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                value = String.valueOf(cell.getCellFormula());
                break;
        }
        return value;
    }
 
}