package com.whyc.service; import com.whyc.dto.FileUrlDTO; import com.whyc.dto.Response; import com.whyc.mapper.ProductBomApprovingMapper; import com.whyc.pojo.DocUser; import com.whyc.pojo.ProductBomApproving; import com.whyc.pojo.WorksheetMain; import com.whyc.util.ActionUtil; import com.whyc.util.CommonUtil; import com.whyc.util.FileUtil; import com.whyc.util.Zip4jUtil; import io.swagger.models.auth.In; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.*; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.xml.crypto.Data; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.LinkedList; import java.util.List; @Service public class ProductBomApprovingService { @Resource private ProductBomApprovingMapper mapper; public List excelParse(InputStream inputStream) throws IOException, InvalidFormatException { List list = new LinkedList<>(); Workbook workbook = null; workbook = WorkbookFactory.create(inputStream); List allPictures = workbook.getAllPictures(); inputStream.close(); //取第一个sheet表 Sheet sheet = workbook.getSheetAt(0); int lastRowNum = sheet.getLastRowNum(); //取第三行,并以第三行开始 Row row2 = sheet.getRow(1); short lastCellNum = row2.getLastCellNum(); for (int i = 2; i < lastRowNum+1; i++) { ProductBomApproving bomApproving = new ProductBomApproving(); for (int j = 1; j < lastCellNum; j++) { Row row = sheet.getRow(i); Cell cell = row.getCell(j); String cellValue = null; Double cellValueInt = 0d; if(j == 9){ cellValueInt = cell.getNumericCellValue(); }else if(j!=15){ cellValue = cell.getStringCellValue(); } switch (j){ 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(cellValueInt.intValue());}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 bomList) { mapper.insertBatchSomeColumn(bomList); } public Response zipParse(MultipartFile file) throws IOException, InvalidFormatException { List list = new LinkedList<>(); Response response = new Response(); //检查是否为zip boolean isZip = Zip4jUtil.checkZipFileParam(file); if(!isZip){ return response.set(1,false,"上传的文件格式不是zip"); } //zip存储路径:doc_file/product_approving/${username}/{dateFormat}/${timeStamp} String rootFile = CommonUtil.getRootFile(); DocUser user = ActionUtil.getUser(); String dateFormat = new SimpleDateFormat("YYYY-MM").format(new Date()); long timeStamp = System.currentTimeMillis(); String filePath = rootFile + File.separator + "product_approving" + File.separator + user.getName() + File.separator + dateFormat+ File.separator + timeStamp; File parentFile = new File(filePath); File zipFile = new File(filePath+File.separator+file.getOriginalFilename()); if(!zipFile.exists()){ zipFile.mkdirs(); } file.transferTo(zipFile); //解压文件夹 Zip4jUtil.unPackZip(zipFile,null,filePath); //遍历解压后的文件夹路径,解析excel System.out.println(filePath); List fileList = new ArrayList<>(); //图纸筛选 List dwgExistsList = getDwgList(fileList); fileList = FileUtil.getStaticFilePath(parentFile,fileList); for (int i = 0; i < fileList.size(); i++) { String fileTempUrl = fileList.get(i); if(fileTempUrl.contains(".xls")){ File fileTemp = new File(fileTempUrl); Workbook workbook = null; workbook = WorkbookFactory.create(fileTemp); List allPictures = workbook.getAllPictures(); //取第一个sheet表 Sheet sheet = workbook.getSheetAt(0); int lastRowNum = sheet.getLastRowNum(); //取第三行,并以第三行开始 Row rowTemp = sheet.getRow(1); short lastCellNum = rowTemp.getLastCellNum(); //先单独获取产品型号和版本号/分类 Row row = sheet.getRow(1); String type = row.getCell(1).getStringCellValue(); Row row2 = sheet.getRow(2); String productModel = row2.getCell(5).getStringCellValue(); Row row3 = sheet.getRow(3); String productVersion = row3.getCell(5).getStringCellValue(); for (int l = 9; l < lastRowNum-1; l++) { ProductBomApproving bomApproving = new ProductBomApproving(); bomApproving.setParentModel(productModel); bomApproving.setParentVersion(productVersion); bomApproving.setType(type); for (int m = 2; m < lastCellNum; m++) { row = sheet.getRow(l); Cell cell = row.getCell(m); String cellValue = null; int cellValueInt = 0; Double cellValueDouble = null; if(m == 3){ cellValueDouble = cell.getNumericCellValue(); DecimalFormat decimalFormat = new DecimalFormat("0"); cellValue = decimalFormat.format(cellValueDouble); } else if(m ==7){ cellValueDouble = cell.getNumericCellValue(); if(cellValueDouble.equals(0d)){ cellValue = ""; }else{ cellValue = cellValueDouble.toString(); } } else if(m==8){ cellValueDouble = cell.getNumericCellValue(); } else { cellValue = cell.getStringCellValue(); } switch (m){ case 2:{bomApproving.setCategory(cellValue);}break; case 3:{bomApproving.setSubCode(cellValue);}break; case 4:{bomApproving.setSubName(cellValue);}break; case 5:{ bomApproving.setSubModel(cellValue); //判断图纸查看是否存在 bomApproving.setDwgExist(0); String dwgFileName = cellValue+".dwg"; for (int n = 0; n < dwgExistsList.size(); n++) { FileUrlDTO fileUrlDTO = dwgExistsList.get(n); if(dwgFileName.equals(fileUrlDTO.getFileName())){ //存在 bomApproving.setDwgExist(1); bomApproving.setDwgUrl(fileUrlDTO.getHttpFileUrl()); break; } } }break; case 6:{bomApproving.setMaterial(cellValue);}break; case 7:{bomApproving.setThickness(cellValue);}break; case 8:{bomApproving.setQuantity(cellValueDouble.intValue());}break; case 9:{bomApproving.setSurfaceDetail(cellValue);}break; case 10:{bomApproving.setNotes(cellValue);}break; case 11:{ //图片,从0开始,到图片size为止 int k = l-9; if(k getDwgList(List fileList) { List list = new LinkedList<>(); fileList.forEach(fileUrl->{ if(fileUrl.substring(fileUrl.lastIndexOf(".")+1).equals("dwg")) { FileUrlDTO dto = new FileUrlDTO(); dto.setFileName(fileUrl.substring(fileUrl.lastIndexOf(File.separator) + 1)); dto.setHttpFileUrl(fileUrl.substring(fileUrl.lastIndexOf("doc_file" + File.separator + "product_approving"))); list.add(dto); } }); return list; } public static void main(String[] args) throws IOException, InvalidFormatException { String a = "C:\\Users\\29550\\Desktop\\当前项目\\202207泰州平台\\49泰州数据库备份-服务器从windows切换到Ubuntu时的数据库备份\\db_alarm.sql"; String substring = a.substring(a.lastIndexOf(File.separator)+1); String s2 = (a.substring(a.lastIndexOf("当前项目"+File.separator+"202207泰州平台"))); System.out.println(substring); System.out.println(s2); } }