管理图片
Contents
[
Hide
]
Aspose.Cells允许开发人员在运行时向电子表格添加图片。此外,可以在运行时控制这些图片的定位,更多细节将在接下来的章节中讨论。
本文章介绍了如何添加图片,以及如何插入显示特定单元格内容的图片。
添加图片
向电子表格添加图片非常简单。只需几行代码即可。
只需调用Worksheet对象中封装的Pictures集合的add方法即可。add方法接受以下参数:
- 左上角行索引,左上角行的索引。
- 左上角列索引,左上角列的索引。
- 图像文件名,图像文件的名称,包括完整路径。
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 | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(AddingPictures.class); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Obtaining the reference of first worksheet | |
Worksheet sheet = worksheets.get(0); | |
// Adding a picture at the location of a cell whose row and column indices are 5 in the worksheet. It is "F6" cell | |
int pictureIndex = sheet.getPictures().add(5, 5, dataDir + "logo.jpg"); | |
Picture picture = sheet.getPictures().get(pictureIndex); | |
// Saving the Excel file | |
workbook.save(dataDir + "book1.xls"); |
图片的定位
可使用Aspose.Cells按以下方式定位图片:
- 绝对定位.
绝对定位
开发人员可以通过使用 setUpperDeltaX 和 setUpperDeltaY 方法来绝对定位图片。
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 | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(AbsolutePositioning.class); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the newly added worksheet. | |
int sheetIndex = workbook.getWorksheets().add(); | |
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex); | |
// Adding a picture at the location of a cell whose row and column indices are 5 in the worksheet. It is "F6" cell | |
int pictureIndex = worksheet.getPictures().add(5, 5, dataDir + "logo.jpg"); | |
Picture picture = worksheet.getPictures().get(pictureIndex); | |
// Positioning the picture proportional to row height and colum width | |
picture.setUpperDeltaX(200); | |
picture.setUpperDeltaY(200); | |
// Saving the Excel file | |
workbook.save(dataDir + "test_pictures.xls"); |