whycxzp
1 天以前 128f28de7eac9bf585f0909e9bdc8e299670f58c
设备器件excel导入
1个文件已修改
51 ■■■■■ 已修改文件
src/main/java/com/whyc/service/DeviceSpareService.java 51 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/DeviceSpareService.java
@@ -12,7 +12,9 @@
import com.whyc.util.CommonUtil;
import com.whyc.util.ThreadLocalUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -20,6 +22,7 @@
import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
@@ -229,12 +232,50 @@
            spare.setSupplier(supplier);
            spareList.add(spare);
            //第七列为图片 TODO 待处理
            /*Cell cell = row.getCell(7);
            CellType cellType = cell.getCellType();
            System.out.println("-");*/
        }
        addBatch(spareList);
        //第七列为图片-浮动式图片 TODO 待处理 处理已存在的物料
        //获取绘图对象中的所有图形
        XSSFDrawing drawing = (XSSFDrawing) sheet.getDrawingPatriarch();
        if (drawing == null) {
            drawing = (XSSFDrawing) sheet.createDrawingPatriarch();
        }
        List<XSSFShape> shapes = drawing.getShapes();
        for (XSSFShape shape : shapes) {
            if (shape instanceof XSSFPicture) {
                XSSFPicture pic = (XSSFPicture) shape;
                XSSFClientAnchor anchor = pic.getClientAnchor();
                //获取图片所在的起始行
                int rowIndex = anchor.getRow1();
                byte[] data = pic.getPictureData().getData();
                DeviceSpare spare = spareList.get(rowIndex - 1);
                //对存储路径进行定义
                Date now = new Date();
                String timeFormat = ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM_DD_HH_MM_SS_UNION, now);
                String dirMonth = ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM, now);
                String fileDirPath = CommonUtil.getRootFile() + "deviceSpare" + File.separator + dirMonth;
                File fileDir = new File(fileDirPath);
                //如果文件夹不存在则创建
                if (!fileDir.exists()) {
                    fileDir.mkdirs();
                }
                String filePath = fileDirPath + File.separator + timeFormat+"_"+spare.getName()+"_"+spare.getModel()+"_"+spare.getVersion()+".png";
                // 保存图片到本地
                try (FileOutputStream fos = new FileOutputStream(filePath)) {
                    fos.write(data);
                }
                String split = "pis_file"+File.separator+"deviceSpare";
                String picUrl = File.separator + filePath.substring(filePath.indexOf(split));
                spare.setPictureUrl(picUrl);
            }
        }
        //addBatch(spareList);
        return null;
    }