セル参照に基づいて画像を挿入する
Contents
[
Hide
]
時々、空の画像があり、Formula Barでセル参照を設定して画像内のデータや内容を表示する必要があります。 Aspose.Cellsはこの機能(Microsoft Excel 2010)をサポートしています。
セル参照に基づいて画像を挿入する
Aspose.Cellsはワークシートセルの内容を画像形状で表示することをサポートしています。表示したいデータを含むセルに画像をリンクすることができます。セルまたはセル範囲がグラフィックオブジェクトにリンクされているため、データの変更が自動的にグラフィックオブジェクトに反映されます。ワークシートに画像を追加するには、Worksheetオブジェクトでカプセル化されたShapeCollectionコレクションのaddPictureメソッドを呼び出します。PictureオブジェクトのsetFormulaメソッドを使用してセル範囲を指定します。
以下は、下記のコードによって生成されるファイルのスクリーンショットです。
セル参照に基づいて画像を挿入する
サンプルコード
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"); |