管理图片

添加图片

将图片添加到电子表格非常容易。它只需要几行代码。

只需调用[添加](https://reference.aspose.com/cells/java/com.aspose.cells/picturecollection#add(int,%20int,%20java.lang.String) 的方法图片集合(封装在工作表目的)。这[添加](https://reference.aspose.com/cells/java/com.aspose.cells/picturecollection#add(int,%20int,%20java.lang.String)方法采用以下参数:

  • 左上行索引左上行的索引。
  • 左上列索引左上列的索引。
  • 图像文件名,图像文件的名称,完整的路径。
// 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定位如下:

绝对定位

开发者可以通过使用设置上增量X设置上增量的方法图片目的。

// 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");

推进主题