严智鑫
2024-03-18 d812f6f23b3f0e4ad6cd140341b494275b628ee3
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
package com.example.springboot.service;
 
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
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 org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.example.springboot.entity.GlassInfo;
import com.example.springboot.entity.MeasureSetting;
import com.example.springboot.mapper.GlassInfoMapper;
import com.example.springboot.mapper.MeasureSettingMapper;
import com.example.springboot.mapper.QueueMapper;
 
@Service
public class HomeService {
 
    @Autowired
    MeasureSettingMapper MeasureSettingMapper;
    @Autowired
    GlassInfoMapper GlassInfoMapper;
    @Autowired
    QueueMapper QueueMapper;
 
    // 匹配玻璃 宽,高,线路
    public List<GlassInfo> NormalGlassInfo(double width, double height, String line) {
        //
        List<GlassInfo> Results = new ArrayList<GlassInfo>();
        // 获取匹配设置
        MeasureSetting MeasureSetting = MeasureSettingMapper.SelectMeasureSetting(line);
        if (height > 0 && width > 0 && MeasureSetting != null) {
            // 有效参数 请求=1 长/宽>0
            double maxheight = height + MeasureSetting.getErrorHeight();
            double minheight = height - MeasureSetting.getErrorHeight();
            double maxwidth = width + MeasureSetting.getErrorWidth();
            double minwidth = width - MeasureSetting.getErrorWidth();
            // 查询当前测量数据
            List<GlassInfo> GlassInfos = GlassInfoMapper.selectGlassInfos(maxwidth, minwidth, maxheight, minheight);
            List types = new ArrayList<>();
            for (int i = 0; i < GlassInfos.size(); i++) {
                Integer glasstype = GlassInfos.get(i).getGlasstype();
                if (!types.contains(glasstype)) {
                    types.add(glasstype);
                    Results.add(GlassInfos.get(i));
                }
            }
        }
        return Results;
    }
 
    //Execl表格 传入文件路径  
    public List ReadExecl(String filename) {
        try {
             // 创建文件对象
        File file = new File(filename);
        // 创建文件输入流对象
        FileInputStream inputStream = new FileInputStream(file);
        // 创建工作簿对象
        Workbook workbook = WorkbookFactory.create(inputStream);
        // 获取第一个工作表对象
        Sheet sheet = workbook.getSheetAt(0);
        // 创建一个实体类集合,用于存储Excel数据
        
        List Results=new ArrayList();  
        int i=0;  
        // 遍历行
        for (Row row : sheet) {
            // 遍历单元格
            List ResultRow=new ArrayList(); 
            //System.out.println();
            for (Cell cell : row) {
                String LastCellvalue="";
                // 判断单元格类型是否为公式类型
                if (cell.getCellType() == CellType.FORMULA) {
                    // 使用公式计算器计算单元格的值
                    FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
                    CellValue cellValue = evaluator.evaluate(cell);
                    // 计算后的单元格值
                    LastCellvalue=cellValue.getNumberValue()+"";
                }else if(cell.getCellType() == CellType.NUMERIC){
                    double value = cell.getNumericCellValue();
                    if (value%1==0) {
                        LastCellvalue=Math.round(value)+"";
                    }else{
                        LastCellvalue=value+"";
                    }
                } else {
                    // 单元格值
                    LastCellvalue=cell.toString(); 
                }
                ResultRow.add(LastCellvalue);
                //System.out.print(LastCellvalue+"  ");
            }
            Results.add(ResultRow);
            i++;
        }
        System.out.println(i);
        // 关闭工作簿和输入流对象
        workbook.close();
        inputStream.close();
        return Results;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("异常");
            return new ArrayList();
        }
       
    }
 
 
}