| | |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.poi.openxml4j.opc.PackagePartName; |
| | | import org.apache.poi.ss.usermodel.Sheet; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | import org.apache.poi.ss.usermodel.WorkbookFactory; |
| | | import org.apache.poi.ss.usermodel.*; |
| | | import org.apache.poi.xssf.usermodel.*; |
| | | |
| | | import java.io.*; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipInputStream; |
| | | |
| | |
| | | |
| | | public static Map<String, XSSFPictureData> getExcelPictures(InputStream is) { |
| | | try { |
| | | byte[] fileData = getFileStream(is); |
| | | byte[] fileData = toByteArray(is); |
| | | Map<String, XSSFPictureData> pictures = getPictures(fileData); |
| | | pictures.forEach((id, xssfPictureData) -> { |
| | | System.out.println("id:" + id); |
| | |
| | | return null; |
| | | } |
| | | |
| | | } |
| | | public static byte[] toByteArray(InputStream input) throws IOException { |
| | | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| | | int bytesRead; |
| | | byte[] buffer = new byte[1024]; // 可以根据需要调整缓冲区大小 |
| | | while ((bytesRead = input.read(buffer)) != -1) { |
| | | byteArrayOutputStream.write(buffer, 0, bytesRead); |
| | | } |
| | | return byteArrayOutputStream.toByteArray(); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取单元格中的图片 |
| | | */ |
| | | |
| | | public static List<XSSFPictureData> getPicturesFromCell(Sheet sheet, Cell cell) { |
| | | try { |
| | | List<XSSFPictureData> cellPictures = new ArrayList<>(); |
| | | XSSFDrawing drawing = ((XSSFSheet) sheet).createDrawingPatriarch(); |
| | | if (drawing != null) { |
| | | List<XSSFPicture> pictures = drawing.getShapes().stream() |
| | | .filter(shape -> shape instanceof XSSFPicture) |
| | | .map(shape -> (XSSFPicture) shape).collect(Collectors.toList()); |
| | | for (XSSFPicture picture : pictures) { |
| | | int rowIndex = picture.getPreferredSize().getRow1(); |
| | | int colIndex = picture.getPreferredSize().getCol1(); |
| | | if (colIndex == cell.getColumnIndex() && rowIndex == cell.getRowIndex()) { |
| | | cellPictures.add(picture.getPictureData()); |
| | | } |
| | | } |
| | | } |
| | | return cellPictures; |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private static Map<String, List<XSSFPicture>> getImgPathMap(Sheet sheet) { |
| | | Map<String, List<XSSFPicture>> imgPathMap = new HashMap<>(); |
| | | XSSFDrawing drawing = ((XSSFSheet) sheet).createDrawingPatriarch(); |
| | | if (drawing != null) { |
| | | List<XSSFPicture> pictures = drawing.getShapes().stream() |
| | | .filter(shape -> shape instanceof XSSFPicture) |
| | | .map(shape -> (XSSFPicture) shape).collect(Collectors.toList()); |
| | | for (XSSFPicture picture : pictures) { |
| | | int rowIndex = picture.getPreferredSize().getRow1(); |
| | | int colIndex = picture.getPreferredSize().getCol1(); |
| | | String key = colIndex + "-" + rowIndex; |
| | | List<XSSFPicture> list = null; |
| | | if (imgPathMap.containsKey(key)) { |
| | | list = imgPathMap.get(key); |
| | | } else { |
| | | list = new ArrayList<>(); |
| | | imgPathMap.put(key, list); |
| | | } |
| | | list.add(picture); |
| | | } |
| | | } |
| | | return imgPathMap; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取图片的扩展名 |
| | | */ |
| | | |
| | | private static String getImageExtension(PictureData picture) { |
| | | String mimeType = picture.getMimeType(); |
| | | switch (mimeType) { |
| | | case "image/jpeg": |
| | | return "jpg"; |
| | | case "image/png": |
| | | return "png"; |
| | | default: |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | } |