画像の管理
Aspose.Cellsを使用すると、開発者は実行時にスプレッドシートに画像を追加できます。さらに、これらの画像の位置を実行時に制御することができます。これについては後のセクションで詳しく説明します。
この記事では、画像の追加方法と特定のセルの内容を示す画像の挿入方法について説明します。
画像の追加
スプレッドシートに画像を追加するのは非常に簡単です。わずかなコード行しか必要ありません。
単にPicturesオブジェクトでカプセル化されたWorksheetコレクションのaddメソッドを呼び出します。addメソッドは以下のパラメータを取ります:
- 左上の行インデックス、左上の行のインデックス。
- 左上の列インデックス、左上の列のインデックス。
- 画像ファイル名、パスを含む画像ファイルの名前。
// 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メソッドとPictureメソッドを使用して画像を絶対位置づけすることができます。
// 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"); |