kongdeqiang
2023-07-10 aaabd0a9eb8f88705208fc9b0ff441dd0fd7ccbb
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
package com.boying.util;
 
import com.boying.entity.DongHuanBaoJing;
import com.boying.entity.DongHuanZhuangTai;
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.ss.usermodel.WorkbookFactory;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author kdq
 * @version 1.0.0
 * @ClassName ExcelUtils.java
 * @Description TODO
 * @createTime 2023年06月19日 08:58:00
 */
public class ExcelUtils {
    public static List<DongHuanZhuangTai> excelToShopIdList(InputStream inputStream,Integer type) throws IOException {
        Workbook workbook = WorkbookFactory.create(inputStream);
        inputStream.close();
        // 在工作簿获取目标工作表
        Sheet sheet = workbook.getSheetAt(0);
        // 获取到最后一行
        int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
        // 该集合用来储存行对象
        ArrayList<DongHuanZhuangTai> detaileds = new ArrayList<>();
        // 遍历整张表,从第二行开始,第一行的表头不要,循环次数不大于最后一行的值
        for (int i = 1; i < physicalNumberOfRows; i++) {
            // 该对象用来储存行数据
            DongHuanZhuangTai detailed = new DongHuanZhuangTai();
            // 获取当前行数据
            Row row = sheet.getRow(i);
            // 获取目标单元格的值并存进对象中
            detailed.setOid(row.getCell(1).getStringCellValue());
            detailed.setDeviceName(row.getCell(2).getStringCellValue());
            detailed.setBianLiangName(row.getCell(3).getStringCellValue());
            detailed.setValue(row.getCell(4).getStringCellValue());
            detailed.setType(type);
            // 把对象放到集合里
            detaileds.add(detailed);
            System.out.println("获取到的当前行数据:" + detailed);
        }
        return detaileds;
    }
 
    public static List<DongHuanBaoJing> excelToShopIdList2(InputStream inputStream, Integer type) throws IOException {
        Workbook workbook = WorkbookFactory.create(inputStream);
        inputStream.close();
        // 在工作簿获取目标工作表
        Sheet sheet = workbook.getSheetAt(0);
        // 获取到最后一行
        int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
        // 该集合用来储存行对象
        ArrayList<DongHuanBaoJing> detaileds = new ArrayList<>();
        // 遍历整张表,从第二行开始,第一行的表头不要,循环次数不大于最后一行的值
        for (int i = 1; i < physicalNumberOfRows; i++) {
            // 该对象用来储存行数据
            DongHuanBaoJing detailed = new DongHuanBaoJing();
            // 获取当前行数据
            Row row = sheet.getRow(i);
            // 获取目标单元格的值并存进对象中
            detailed.setOid(row.getCell(1).getStringCellValue());
            detailed.setDeviceName(row.getCell(2).getStringCellValue());
            detailed.setBianLiangName(row.getCell(3).getStringCellValue());
            detailed.setValue(row.getCell(4).getStringCellValue());
            detailed.setType(type);
            // 把对象放到集合里
            detaileds.add(detailed);
            System.out.println("获取到的当前行数据:" + detailed);
        }
        return detaileds;
    }
}