| | |
| | | import com.whyc.pojo.*; |
| | | import com.whyc.util.DateUtil; |
| | | import com.whyc.util.*; |
| | | import org.apache.poi.hssf.usermodel.*; |
| | | import org.apache.poi.openxml4j.exceptions.InvalidFormatException; |
| | | 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; |
| | |
| | | String fileTempUrl = fileList.get(i); |
| | | //查询需上传的bom数据 |
| | | //if (fileTempUrl.contains(".xls") && fileTempUrl.contains("(BOM)")) { |
| | | int excelType = 1; //.xls |
| | | if (fileTempUrl.substring(fileTempUrl.lastIndexOf(File.separator)+1).contains("(BOM).xls")) { |
| | | Workbook workbook = null; |
| | | InputStream inputStream = new FileInputStream(new File(fileTempUrl)); |
| | | workbook = WorkbookFactory.create(inputStream); |
| | | inputStream.close(); |
| | | List<? extends PictureData> allPictures = workbook.getAllPictures(); |
| | | //List<? extends PictureData> allPictures = workbook.getAllPictures(); |
| | | if(fileTempUrl.substring(fileTempUrl.lastIndexOf(".")+1).equals("xlsx")) { |
| | | Collections.sort(allPictures, new Comparator<PictureData>() { |
| | | excelType = 2; //.xlsx |
| | | /*Collections.sort(allPictures, new Comparator<PictureData>() { |
| | | @Override |
| | | public int compare(PictureData o1, PictureData o2) { |
| | | // Name: /xl/media/image1.png - Content Type: image/png |
| | |
| | | |
| | | |
| | | } |
| | | }); |
| | | });*/ |
| | | } |
| | | //取第一个sheet表 |
| | | Sheet sheet = workbook.getSheetAt(0); |
| | | //存储图片信息及所在分页 |
| | | Map<Integer,PictureData> pictureDataMap = new HashMap<>(); |
| | | if(excelType == 1){ //.xls |
| | | List<HSSFShape> children = ((HSSFSheet) sheet).getDrawingPatriarch().getChildren(); |
| | | for (HSSFShape child : children) { |
| | | HSSFPicture picture = (HSSFPicture)child; |
| | | HSSFPictureData pictureData = picture.getPictureData(); |
| | | HSSFClientAnchor anchor = (HSSFClientAnchor) child.getAnchor(); |
| | | //行,不能跨行; |
| | | int row1 = anchor.getRow1(); |
| | | int row2 = anchor.getRow2(); |
| | | if(row1 == row2){ |
| | | //不跨行,有效 |
| | | pictureDataMap.put(row1,pictureData); |
| | | } |
| | | } |
| | | } |
| | | else if(excelType == 2){ //.xlsx |
| | | List<XSSFShape> children = ((XSSFSheet) sheet).getDrawingPatriarch().getShapes(); |
| | | for (XSSFShape child : children) { |
| | | XSSFPicture picture = (XSSFPicture)child; |
| | | XSSFPictureData pictureData = picture.getPictureData(); |
| | | XSSFClientAnchor anchor = (XSSFClientAnchor) child.getAnchor(); |
| | | //行,不能跨行; |
| | | int row1 = anchor.getRow1(); |
| | | int row2 = anchor.getRow2(); |
| | | if(row1 == row2){ |
| | | //不跨行,有效 |
| | | pictureDataMap.put(row1,pictureData); |
| | | } |
| | | } |
| | | } |
| | | Set<Integer> pictureRowSet = pictureDataMap.keySet(); |
| | | int lastRowNum = sheet.getLastRowNum(); |
| | | //取第三行,并以第三行开始 |
| | | Row row2 = sheet.getRow(1); |
| | |
| | | } |
| | | break; |
| | | case 15: { |
| | | /* 弃用,不适用于 图片不按顺序摆放情况 |
| | | //图片,从0开始,到图片size为止 |
| | | int m = k - 2; |
| | | if (m < allPictures.size()) { |
| | |
| | | fileOutputStream.write(data); |
| | | |
| | | bom.setPictureUrl(picturePathFront); |
| | | }*/ |
| | | if (pictureRowSet.contains(k)) { |
| | | |
| | | PictureData pictureData = pictureDataMap.get(k); |
| | | //图片存储 product_submit/username/2022-07/ |
| | | String approvingPath = rootFile + File.separator + "product_submit" + File.separator + user.getName() + File.separator + dateFormat + File.separator + timeStamp; |
| | | File provingFile = new File(approvingPath); |
| | | if (!provingFile.exists()) { |
| | | provingFile.mkdirs(); |
| | | } |
| | | String suffix = pictureData.suggestFileExtension(); |
| | | String picturePath = approvingPath + File.separator + bom.getSubModel() + "-bom." + suffix; |
| | | String picturePathFront = "doc_file" + File.separator + "product_submit" + File.separator + user.getName() + File.separator + dateFormat + File.separator + timeStamp + File.separator + bom.getSubModel() + "-bom." + suffix; |
| | | byte[] data = pictureData.getData(); |
| | | FileOutputStream fileOutputStream = null; |
| | | File pictureFile = new File(picturePath); |
| | | fileOutputStream = new FileOutputStream(pictureFile); |
| | | fileOutputStream.write(data); |
| | | |
| | | bom.setPictureUrl(picturePathFront); |
| | | } |
| | | } |
| | | break; |