Insert a Picture based on Cell Reference
Inserting a Picture Based on Cell Reference
Aspose.Cells supports displaying the contents of a worksheet cell in an image shape. You can link the picture to the cell that contains the data that you want to display. Since the cell or cell range is linked to the graphic object, changes to the data automatically appear in the graphic object. Add a picture to the worksheet by calling the addPicture method of the ShapeCollection collection (encapsulated in the Worksheet object). Specify the cell range by using the setFormula method of the Picture object.
Below is a screenshot of the file the code below generates.
Inserting a picture based on cell reference
Sample Code
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getDataDir(InsertPictureCellReference.class); | |
// Instantiate a new Workbook | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet's cells collection | |
Cells cells = workbook.getWorksheets().get(0).getCells(); | |
// Add string values to the cells | |
cells.get("A1").putValue("A1"); | |
cells.get("C10").putValue("C10"); | |
// Add a blank picture to the D1 cell | |
Picture pic = (Picture) workbook.getWorksheets().get(0).getShapes().addPicture(0, 3, null, 10, 10); | |
// Set the size of the picture. | |
pic.setHeightCM(4.48); | |
pic.setWidthCM(5.28); | |
// Specify the formula that refers to the source range of cells | |
pic.setFormula("A1:C10"); | |
// Update the shapes selected value in the worksheet | |
workbook.getWorksheets().get(0).getShapes().updateSelectedValue(); | |
// Save the Excel file. | |
workbook.save(dataDir + "referencedpicture.xlsx"); |