Free Support Forum - aspose.com

Aspose.Cells如何添加一个形状,并且在形状中填充一张图片

Worksheet sheet = workbook.getWorksheets().get(i);
InputStream fis = new FileInputStream(watermark.getWatermarkText());
//Add Watermark
ShapeCollection shapes = sheet.getShapes();
Shape shape = shapes.addShape(MsoDrawingType.RECTANGLE, watermark.getUpperLeftRow(),1,watermark.getUpperLeftColumn(),1,300, 400);
Shape picture = shapes.addPicture(watermark.getUpperLeftRow(), watermark.getUpperLeftColumn(), fis, 75, 75);
目前可以单独添加一张图片或者一个形状,但是无法关联

@lvyoufang,

请尝试以下示例代码来完成您的任务以供参考:
例如
样例代码:

 Workbook workbook = new Workbook();
 Worksheet sheet = workbook.getWorksheets().get(0);

 // Get the image file.
 File file = new File("e:\\test\\pic.jpg");
 // Get the picture into the streams.
 byte[] imageData = new byte[(int) file.length()];
 FileInputStream fis = new FileInputStream(file);
 fis.read(imageData);

 ShapeCollection shapes = sheet.getShapes();
 com.aspose.cells.Shape shape = shapes.addShape(MsoDrawingType.RECTANGLE, 1,1,2,1,300, 400);
 shape.getFill().setImageData(imageData);

 workbook.save("f:\\files\\out1.xlsx");

希望这个对你有帮助。

It’s OK,Thank you

@lvyoufang,

欢迎您,也很高兴知道您的问题已由建议的代码段解决。