public void vendorImportExcel(MultipartFile file) throws Exception {
int suffixIndex = file.getOriginalFilename().lastIndexOf(".");
String suffix = file.getOriginalFilename().substring(suffixIndex);
if (".xlsx".equals(suffix)) {
throw new RuntimeException("请上传xlsx格式的excel");
}
ImportParams params = new ImportParams();
params.setHeadRows(1);
List<ImportPackingEntity> list = ExcelImportUtil.importExcel(file.getInputStream(), ImportPackingEntity.class, params);
if (CollectionUtils.isEmpty(list)) {
throw new RuntimeException("表格数据为空");
}
//读取表格中的图片
XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFDrawing dp = sheet.createDrawingPatriarch();
List<XSSFShape> pics = dp.getShapes();
for (XSSFShape shape : pics) {
XSSFPicture inpPic = (XSSFPicture) shape;
XSSFClientAnchor clientAnchor = inpPic.getClientAnchor();
PictureData pict = inpPic.getPictureData();
//图片必须要严格定位到一个具体的单元格子内
int col = clientAnchor.getCol1();
int row = clientAnchor.getRow1();
// System.out.println("col1: " + clientAnchor.getCol1() + ", col2: " + clientAnchor.getCol2() + ", row1: " + clientAnchor.getRow1() + ", row2: " + clientAnchor.getRow2());
ImportPackingEntity entity = list.get(row - 1);
String processNo = getNextProcessNo();
entity.setPackingNo(processNo);
String fileName = processNo;
if (col == 23) {//零件图片
fileName = fileName + "零件图片";
} else if (col == 24) {//内包装图片
fileName = fileName + "内包装图片";
} else if (col == 25) {//外包装图片
fileName = fileName + "外包装图片";
}
String fileExtension = pict.suggestFileExtension();
fileName = fileName + "." + fileExtension;
byte[] data = pict.getData();
FastDFSFile file1 = new FastDFSFile(data, fileExtension);
NameValuePair[] metaList = new NameValuePair[4];
metaList[0] = new NameValuePair("fileName", fileName);
metaList[1] = new NameValuePair("fileLength", String.valueOf(data.length));
metaList[2] = new NameValuePair("fileExt", fileExtension);
metaList[3] = new NameValuePair("fileAuthor", "");
String filePathJson = FileManager.upload(file1, metaList);
cn.hutool.json.JSONObject json = new cn.hutool.json.JSONObject(filePathJson);
String uri = json.toString();
String filepath = Base64Util.getBase64(uri);
System.out.println(filepath);
if (col == 23) {//零件图片
entity.setImageName1(fileName);
entity.setImageUrl1(filepath);
} else if (col == 24) {//内包装图片
entity.setImageName2(fileName);
entity.setImageUrl2(filepath);
} else if (col == 25) {//外包装图片
entity.setImageName3(fileName);
entity.setImageUrl3(filepath);
}
}
List<PackingEntity> packingEntityList = new ArrayList<>();
PackingEntity entity = null;
for (ImportPackingEntity importPackingEntity : list) {
entity = new PackingEntity();
BeanUtils.copyProperties(importPackingEntity, entity);
packingEntityList.add(entity);
}
this.saveBatch(packingEntityList);
}
java poi导入文字和图片
最新推荐文章于 2023-12-19 11:50:19 发布