whycxzp
2022-07-19 dc974ec2faed53d789abbe49c70bf0443ed29703
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
package com.whyc.service;
 
import com.whyc.pojo.ProductBomApproving;
import com.whyc.pojo.WorksheetMain;
import com.whyc.util.CommonUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
 
@Service
public class ProductBomApprovingService {
    public List<ProductBomApproving> excelParse(InputStream inputStream) throws IOException, InvalidFormatException {
        List<ProductBomApproving> list = new LinkedList<>();
        Workbook workbook = null;
        workbook = WorkbookFactory.create(inputStream);
        List<? extends PictureData> allPictures = workbook.getAllPictures();
        inputStream.close();
        //取第一个sheet表
        Sheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        //取第三行,并以第三行开始
        Row row2 = sheet.getRow(2);
        short lastCellNum = row2.getLastCellNum();
        for (int i = 2; i < lastRowNum; i++) {
            ProductBomApproving bomApproving = new ProductBomApproving();
            for (int j = 0; j < lastCellNum; j++) {
                Row row = sheet.getRow(i);
                Cell cell = row.getCell(j);
                String cellValue = cell.getStringCellValue();
                switch (j){
                    case 0:{}break;
                    case 1:{bomApproving.setParentCode(cellValue);}break;
                    case 2:{bomApproving.setParentName(cellValue);}break;
                    case 3:{bomApproving.setParentModel(cellValue);}break;
                    case 4:{bomApproving.setCategory(cellValue);}break;
                    case 5:{bomApproving.setSubCode(cellValue);}break;
                    case 6:{bomApproving.setSubName(cellValue);}break;
                    case 7:{bomApproving.setSubModel(cellValue);}break;
                    case 8:{bomApproving.setUnit(cellValue);}break;
                    case 9:{bomApproving.setQuantity(Integer.parseInt(cellValue));}break;
                    case 10:{bomApproving.setProducer(cellValue);}break;
                    case 11:{bomApproving.setMaterial(cellValue);}break;
                    case 12:{bomApproving.setThickness(cellValue);}break;
                    case 13:{bomApproving.setSurfaceDetail(cellValue);}break;
                    case 14:{bomApproving.setNotes(cellValue);}break;
                    case 15:{
                        //图片,从0开始,到图片size为止
                        int k = i-2;
                        if(k<allPictures.size()){
                            PictureData pictureData = allPictures.get(k);
                            //图片存储
                            CommonUtil.getRootFile();
                            bomApproving.setPictureUrl(bomApproving.getSubModel()+"."+pictureData.suggestFileExtension());
                        }
                    }break;
                }
            }
            list.add(bomApproving);
        }
 
        return list;
    }
}