org.apache.poi.hssf.usermodel.HSSFPicture Java Examples

The following examples show how to use org.apache.poi.hssf.usermodel.HSSFPicture. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PoiPublicUtil.java    From autopoi with Apache License 2.0 6 votes vote down vote up
/**
 * 获取Excel2003图片
 * 
 * @param sheet
 *            当前sheet对象
 * @param workbook
 *            工作簿对象
 * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData
 */
public static Map<String, PictureData> getSheetPictrues03(HSSFSheet sheet, HSSFWorkbook workbook) {
	Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
	List<HSSFPictureData> pictures = workbook.getAllPictures();
	if (!pictures.isEmpty()) {
		for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
			HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
			if (shape instanceof HSSFPicture) {
				HSSFPicture pic = (HSSFPicture) shape;
				int pictureIndex = pic.getPictureIndex() - 1;
				HSSFPictureData picData = pictures.get(pictureIndex);
				String picIndex = String.valueOf(anchor.getRow1()) + "_" + String.valueOf(anchor.getCol1());
				sheetIndexPicMap.put(picIndex, picData);
			}
		}
		return sheetIndexPicMap;
	} else {
		return null;
	}
}
 
Example #2
Source File: PoiPublicUtil.java    From jeasypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 获取Excel2003图片
 * 
 * @param sheet
 *            当前sheet对象
 * @param workbook
 *            工作簿对象
 * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData
 */
public static Map<String, PictureData> getSheetPictrues03(HSSFSheet sheet, HSSFWorkbook workbook) {
	Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
	List<HSSFPictureData> pictures = workbook.getAllPictures();
	if (!pictures.isEmpty()) {
		for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
			HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
			if (shape instanceof HSSFPicture) {
				HSSFPicture pic = (HSSFPicture) shape;
				int pictureIndex = pic.getPictureIndex() - 1;
				HSSFPictureData picData = pictures.get(pictureIndex);
				String picIndex = String.valueOf(anchor.getRow1()) + "_" + String.valueOf(anchor.getCol1());
				sheetIndexPicMap.put(picIndex, picData);
			}
		}
		return sheetIndexPicMap;
	} else {
		return null;
	}
}
 
Example #3
Source File: DefaultExcelReader.java    From myexcel with Apache License 2.0 6 votes vote down vote up
private void convertPicture(Row row, T obj, Integer index, Field field) {
    byte[] pictureData;
    if (isXSSFSheet) {
        XSSFPicture xssfPicture = xssfPicturesMap.get(row.getRowNum() + "_" + index);
        if (xssfPicture == null) {
            return;
        }
        pictureData = xssfPicture.getPictureData().getData();
    } else {
        HSSFPicture hssfPicture = hssfPictureMap.get(row.getRowNum() + "_" + index);
        if (hssfPicture == null) {
            return;
        }
        pictureData = hssfPicture.getPictureData().getData();
    }
    try {
        field.set(obj, new ByteArrayInputStream(pictureData));
    } catch (IllegalAccessException e) {
        throw new ExcelReadException("Failed to read picture.", e);
    }
}
 
Example #4
Source File: PoiPublicUtil.java    From easypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 获取Excel2003图片
 * 
 * @param sheet
 *            当前sheet对象
 * @param workbook
 *            工作簿对象
 * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData
 */
public static Map<String, PictureData> getSheetPictrues03(HSSFSheet sheet, HSSFWorkbook workbook) {
    Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
    List<HSSFPictureData> pictures = workbook.getAllPictures();
    if (!pictures.isEmpty()) {
        for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
            HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
            if (shape instanceof HSSFPicture) {
                HSSFPicture pic = (HSSFPicture) shape;
                int pictureIndex = pic.getPictureIndex() - 1;
                HSSFPictureData picData = pictures.get(pictureIndex);
                String picIndex = String.valueOf(anchor.getRow1()) + "_"
                                  + String.valueOf(anchor.getCol1());
                sheetIndexPicMap.put(picIndex, picData);
            }
        }
        return sheetIndexPicMap;
    } else {
        return null;
    }
}
 
Example #5
Source File: ExcelPublicUtil.java    From jeewx with Apache License 2.0 6 votes vote down vote up
/** 
    * 获取Excel2003图片 
    * @param sheet 当前sheet对象 
    * @param workbook 工作簿对象 
    * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData 
    */  
   @SuppressWarnings("unchecked")
public static Map<String, PictureData> getSheetPictrues03(HSSFSheet sheet, 
   		HSSFWorkbook workbook) {  
       Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>(); 
       List<HSSFPictureData> pictures = workbook.getAllPictures();  
       if (pictures.size() != 0) {  
           for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {  
               HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();  
               if (shape instanceof HSSFPicture) {  
                   HSSFPicture pic = (HSSFPicture) shape;  
                   int pictureIndex = pic.getPictureIndex() - 1;  
                   HSSFPictureData picData = pictures.get(pictureIndex);  
                   String picIndex = String.valueOf(anchor.getRow1()) + "_"  
                           + String.valueOf(anchor.getCol1());  
                   sheetIndexPicMap.put(picIndex, picData);  
               }
           }  
           return sheetIndexPicMap;  
       } else {  
           return (Map<String, PictureData>) sheetIndexPicMap.put(null, null);  
       }  
   }
 
Example #6
Source File: POIImage.java    From excel2pdf with Apache License 2.0 5 votes vote down vote up
public POIImage getCellImage(Cell cell) {
    Sheet sheet = cell.getSheet();
    if (sheet instanceof HSSFSheet) {
        HSSFSheet hssfSheet = (HSSFSheet) sheet;
        if (hssfSheet.getDrawingPatriarch() != null) {
            List<HSSFShape> shapes = hssfSheet.getDrawingPatriarch().getChildren();
            for (HSSFShape shape : shapes) {
                HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
                if (shape instanceof HSSFPicture) {
                    HSSFPicture pic = (HSSFPicture) shape;
                    PictureData data = pic.getPictureData();
                    String extension = data.suggestFileExtension();
                    int row1 = anchor.getRow1();
                    int row2 = anchor.getRow2();
                    int col1 = anchor.getCol1();
                    int col2 = anchor.getCol2();
                    if (row1 == cell.getRowIndex() && col1 == cell.getColumnIndex()) {
                        dimension = pic.getImageDimension();
                        this.anchor = anchor;
                        this.bytes = data.getData();
                    }
                }
            }
        }
    }
    return this;
}
 
Example #7
Source File: PictureSheetGenerator.java    From ermasterr with Apache License 2.0 4 votes vote down vote up
private void setImage(final HSSFWorkbook workbook, final HSSFSheet sheet, final CellLocation cellLocation, int width, int height) {
    POIUtils.setCellValue(sheet, cellLocation, "");

    if (imageBuffer != null) {
        final HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

        final HSSFPicture picture = patriarch.createPicture(new HSSFClientAnchor(), pictureIndex);

        final Dimension dimension = picture.getImageDimension();
        final float rate = (float) dimension.width / (float) dimension.height;
        final float specifiedRate = (float) width / (float) height;

        if (width == -1 || height == -1) {
            width = dimension.width;
            height = dimension.height;

        } else {
            if (rate > specifiedRate) {
                if (dimension.width > width) {
                    height = (int) (width / rate);

                } else {
                    width = dimension.width;
                    height = dimension.height;
                }

            } else {
                if (dimension.height > height) {
                    width = (int) (height * rate);

                } else {
                    width = dimension.width;
                    height = dimension.height;
                }
            }
        }

        final HSSFClientAnchor preferredSize = getPreferredSize(sheet, new HSSFClientAnchor(0, 0, 0, 0, (short) cellLocation.c, cellLocation.r, (short) 0, 0), width, height);
        picture.setAnchor(preferredSize);
    }
}
 
Example #8
Source File: PictureSheetGenerator.java    From ermaster-b with Apache License 2.0 4 votes vote down vote up
private void setImage(HSSFWorkbook workbook, HSSFSheet sheet,
			CellLocation cellLocation, int width, int height) {
		POIUtils.setCellValue(sheet, cellLocation, "");
System.out.println("this.imageBuffer:" + this.imageBuffer);
		if (this.imageBuffer != null) {
			HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

			HSSFPicture picture = patriarch.createPicture(
					new HSSFClientAnchor(), this.pictureIndex);

			Dimension dimension = picture.getImageDimension();
			float rate = dimension.width / dimension.height;
			float specifiedRate = width / height;

			if (width == -1 || height == -1) {
				width = dimension.width;
				height = dimension.height;
				
			} else {
				if (rate > specifiedRate) {
					if (dimension.width > width) {
						height = (int) (width / rate);

					} else {
						width = dimension.width;
						height = dimension.height;
					}

				} else {
					if (dimension.height > height) {
						width = (int) (height * rate);

					} else {
						width = dimension.width;
						height = dimension.height;
					}
				}
			}

			HSSFClientAnchor preferredSize = this.getPreferredSize(sheet,
					new HSSFClientAnchor(0, 0, 0, 0, (short) cellLocation.c,
							cellLocation.r, (short) 0, 0), width, height);
			picture.setAnchor(preferredSize);
		}
	}