基于单元格引用插入图片
Contents
[
Hide
]
有时您会有一张空白图片,并且需要通过在公式栏中设置单元格引用来显示图片中的数据或内容。Aspose.Cells支持此功能(Microsoft Excel 2010)。
根据单元格引用插入图片
Aspose.Cells支持在图像形状中显示工作表单元格的内容。您可以将图片链接到包含您希望显示的数据的单元格。由于单元格或单元格范围已链接到图形对象,因此对数据的更改会自动显示在图形对象中。通过调用 addPicture 方法向工作表添加图片,该方法属于 ShapeCollection 集合(封装在 Worksheet 对象中)。使用 setFormula 方法指定单元格范围,该方法是 Picture 对象的方法。
以下是下面的代码生成的文件的屏幕截图。
基于单元格引用插入图片
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |