セル参照に基づいて画像を挿入する

セル参照に基づいて画像を挿入する

Aspose.Cellsはワークシートセルの内容を画像形状で表示することをサポートしています。表示したいデータを含むセルに画像をリンクすることができます。セルまたはセル範囲がグラフィックオブジェクトにリンクされているため、データの変更が自動的にグラフィックオブジェクトに反映されます。ワークシートに画像を追加するには、Worksheetオブジェクトでカプセル化されたShapeCollectionコレクションのaddPictureメソッドを呼び出します。PictureオブジェクトのsetFormulaメソッドを使用してセル範囲を指定します。

以下は、下記のコードによって生成されるファイルのスクリーンショットです。

セル参照に基づいて画像を挿入する

todo:image_alt_text

サンプルコード

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