public void addImage(AreaRef areaRef, byte[] imageBytes, ImageType imageType) { int poiPictureType = findPoiPictureTypeByImageType(imageType); int pictureIdx = workbook.addPicture(imageBytes, poiPictureType); addImage(areaRef, pictureIdx); }
int pictureureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG);
private int clipAndEncodeImage( final Image image, final long width, final long height, final double deviceScaleFactor ) throws UnsupportedEncoderException, IOException { final int imageWidth = (int) StrictGeomUtility.toExternalValue( width ); final int imageHeight = (int) StrictGeomUtility.toExternalValue( height ); // first clip. final BufferedImage bi = ImageUtils.createTransparentImage( imageWidth, imageHeight ); final Graphics2D graphics = (Graphics2D) bi.getGraphics(); graphics.scale( deviceScaleFactor, deviceScaleFactor ); if ( image instanceof BufferedImage ) { if ( graphics.drawImage( image, null, null ) == false ) { logger.debug( "Failed to render the image. This should not happen for BufferedImages" ); // NON-NLS } } else { final WaitingImageObserver obs = new WaitingImageObserver( image ); obs.waitImageLoaded(); while ( graphics.drawImage( image, null, obs ) == false ) { obs.waitImageLoaded(); if ( obs.isError() ) { logger.warn( "Error while loading the image during the rendering." ); // NON-NLS break; } } } graphics.dispose(); final byte[] data = RenderUtility.encodeImage( bi ); return printerBase.getWorkbook().addPicture( data, Workbook.PICTURE_TYPE_PNG ); }
Workbook resultWorkbook = transformer.transformXLS(is, beans); Iterator rowIter = resultWorkbook.getSheetAt(0).rowIterator(); while (rowIter.hasNext()){ HSSFRow row = (HSSFRow) rowIter.next(); Iterator cellIter = row.cellIterator(); while (cellIter.hasNext()){ HSSFCell cell = (HSSFCell) cellIter.next(); final String IMG_PREFIX = "#IMG#"; if(cell.toString().startsWith(IMG_PREFIX)){ String cellValue = cell.toString(); cell.setCellValue(""); String imagePath = cellValue.replaceFirst(IMG_PREFIX, ""); File imageFile = new File(imagePath); if(imageFile.exists()){ FileInputStream isi = new FileInputStream(imageFile); ImageTools imageTools = new ImageTools(); byte[] imgBytes = imageTools.resizeImage(isi, 100); int pictureIdx = resultWorkbook.addPicture(imgBytes, Workbook.PICTURE_TYPE_JPEG); CreationHelper helper = resultWorkbook.getCreationHelper(); Drawing drawing = resultWorkbook.getSheetAt(0).createDrawingPatriarch(); ClientAnchor anchor = helper.createClientAnchor(); anchor.setCol1(cell.getColumnIndex()); anchor.setRow1(cell.getRowIndex()); Picture pict = drawing.createPicture(anchor, pictureIdx); pict.resize(); isi.close(); } } } }
ImageIO.write(bufferImg, imagePath.substring(imagePath.indexOf(".") + 1, imagePath.length()), byteArrayOut); byte[] value = byteArrayOut.toByteArray(); patriarch.createPicture(anchor, row.getSheet().getWorkbook().addPicture(value, getImageType(value))); } catch (IOException e) { LOGGER.error(e.getMessage(), e); byte[] value = (byte[]) (entity.getMethods() != null ? getFieldBySomeMethod(entity.getMethods(), obj) : entity.getMethod().invoke(obj, new Object[] {})); if (value != null) { patriarch.createPicture(anchor, row.getSheet().getWorkbook().addPicture(value, getImageType(value)));
byte[] value = byteArrayOut.toByteArray(); patriarch.createPicture(anchor, row.getSheet().getWorkbook().addPicture(value, getImageType(value))); } catch (IOException e) { LOGGER.error(e.getMessage(), e); if (value != null) { patriarch.createPicture(anchor, row.getSheet().getWorkbook().addPicture(value, getImageType(value)));
byte[] bytes = IOUtils.toByteArray(is); is.close(); int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
final ResourceData data = resourceManager.load( url ); return workbook.addPicture( data.getResource( resourceManager ), format ); } catch ( final ResourceException re ) { logger.info( "Failed to load image from URL " + url, re ); // NON-NLS return workbook.addPicture( data, Workbook.PICTURE_TYPE_PNG );
/** * 在指定工作表的指定位置插入图片 * @param sheet 工作表 * @param imagePath 图片路径 * @param beginRow 图片开始的行 * @param beginCol 图片开始的列 */ public void insertPicture(Sheet sheet, String imagePath, int beginRow, int beginCol) { Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor clientAnchor = workbook.getCreationHelper().createClientAnchor(); clientAnchor.setRow1(beginRow); clientAnchor.setCol1(beginCol); Picture picture = drawing.createPicture(clientAnchor, workbook.addPicture(ImageUtils.getImageData(imagePath), getImageType(imagePath))); picture.resize(); }
/** * 在指定工作表的指定位置插入图片 * @param sheet 工作表 * @param imageInputStream 图片输入流 * @param imageType 图片文件类型枚举,推荐使用png或jpg图片 * @param beginRow 图片开始的行 * @param beginCol 图片开始的列 */ public void insertPicture(Sheet sheet, InputStream imageInputStream, MIMETypeEnum imageType, int beginRow, int beginCol) { Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor clientAnchor = workbook.getCreationHelper().createClientAnchor(); clientAnchor.setRow1(beginRow); clientAnchor.setCol1(beginCol); Picture picture = drawing.createPicture(clientAnchor, workbook.addPicture(ImageUtils.getImageData(imageInputStream, imageType), getImageType(imageType))); picture.resize(); }
row.getSheet().getWorkbook().addPicture(value, getImageType(value)));
sURL); int index = sheet.getWorkbook().addPicture( IOUtils.toByteArray(imageFile.openStream()), imageType); drawing.createPicture(anchor, index);
/** * 在指定工作表的指定位置插入图片 * @param sheet 工作表 * @param imagePath 图片路径 * @param leftDX 图片在单元格中离左上角的x距离 * @param topDY 图片在单元格中离左上角的y距离 * @param widthDX 图片的宽度 * @param heightDY 图片的高度 * @param beginRow 图片开始的行 * @param beginCol 图片开始的列 * @param endRow 图片结束的行 * @param endCol 图片结束的列 */ public void insertPicture(Sheet sheet, String imagePath, int leftDX, int topDY, int widthDX ,int heightDY, int beginRow, int beginCol, int endRow, int endCol) { Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor clientAnchor = workbook.getCreationHelper().createClientAnchor(); clientAnchor.setDx1(leftDX); clientAnchor.setDy1(topDY); clientAnchor.setDx2(widthDX); clientAnchor.setDy2(heightDY); clientAnchor.setRow1(beginRow); clientAnchor.setCol1(beginCol); clientAnchor.setRow2(endRow); clientAnchor.setCol2(endCol); drawing.createPicture(clientAnchor, workbook.addPicture(ImageUtils.getImageData(imagePath), getImageType(imagePath))); }
/** * 在指定工作表的指定位置插入图片 * @param sheet 工作表 * @param imageInputStream 图片输入流 * @param imageType 图片文件类型枚举,推荐使用png或jpg图片 * @param leftDX 图片在单元格中离左上角的x距离 * @param topDY 图片在单元格中离左上角的y距离 * @param widthDX 图片的宽度 * @param heightDY 图片的高度 * @param beginRow 图片开始的行 * @param beginCol 图片开始的列 * @param endRow 图片结束的行 * @param endCol 图片结束的列 */ public void insertPicture(Sheet sheet, InputStream imageInputStream, MIMETypeEnum imageType, int leftDX, int topDY, int widthDX ,int heightDY, int beginRow, int beginCol, int endRow, int endCol) { Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor clientAnchor = workbook.getCreationHelper().createClientAnchor(); clientAnchor.setDx1(leftDX); clientAnchor.setDy1(topDY); clientAnchor.setDx2(widthDX); clientAnchor.setDy2(heightDY); clientAnchor.setRow1(beginRow); clientAnchor.setCol1(beginCol); clientAnchor.setRow2(endRow); clientAnchor.setCol2(endCol); drawing.createPicture(clientAnchor, workbook.addPicture(ImageUtils.getImageData(imageInputStream, imageType), getImageType(imageType))); }
/** * 图片类型的Cell */ public void createImageCell(Cell cell, double height, String imagePath, byte[] data) throws Exception { if (height > cell.getRow().getHeight()) { cell.getRow().setHeight((short) height); } ClientAnchor anchor; if (type.equals(ExcelType.HSSF)) { anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), cell.getRow().getRowNum() + 1); } else { anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), cell.getRow().getRowNum() + 1); } if (StringUtils.isNotEmpty(imagePath)) { data = ImageCache.getImage(imagePath); } if (data != null) { PoiExcelGraphDataUtil.getDrawingPatriarch(cell.getSheet()).createPicture(anchor, cell.getSheet().getWorkbook().addPicture(data, getImageType(data))); } }