Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Fitting a picture exactly to a single cell is a common requirement when designing spreadsheets that act as visual reports, product catalogs, employee directories, dashboards, or inventory lists. Rather than stretching an image across many cells or placing it loosely on a worksheet, you may want a clean, cell-bound image that stays aligned with the cell that owns it. Aspose.Cells supports this scenario in two complementary ways:
Picture to the worksheet, set its Placement to MoveAndSize, and adjust its anchor cells (UpperLeftRow, UpperLeftColumn, LowerRightRow, LowerRightColumn) so the picture covers exactly one cell.EmbeddedImage property. The image automatically scales to fit the cell’s display area and travels with the cell.
The rest of this article walks through both approaches, explains the relevant APIs, and shows how to use them in code.A floating picture is a Picture object that lives on the worksheet drawing layer. Although it is not part of any single cell, it is anchored to a cell range. The picture’s anchor cells — its upper-left and lower-right corners — determine its visual extent on the worksheet. By default, a freshly added picture spans several cells.
To make a floating picture cover exactly one cell, you need to:
worksheet.getPictures().add(int row, int column, InputStream stream), which anchors the new picture to the given cell.picture.setPlacement(PlacementType.MOVE_AND_SIZE) so the picture moves and resizes with the underlying cell when the user changes the column width or row height.The picture’s anchor is defined by four zero-based index properties:
picture.setUpperLeftRow(int) — the row index of the picture’s top edge.picture.setUpperLeftColumn(int) — the column index of the picture’s left edge.picture.setLowerRightRow(int) — the row index of the picture’s bottom edge. To make the picture’s bottom edge sit at the bottom of row r, set this to r + 1.picture.setLowerRightColumn(int) — the column index of the picture’s right edge. To make the picture’s right edge sit at the right of column c, set this to c + 1.Row and column indices in Aspose.Cells are zero-based. Cell C6 has row index 5 and column index 2. Off-by-one errors on the lower-right anchor are the most common source of pictures that appear to overlap into an adjacent cell.
Picture.Placement is an enum of type PlacementType that controls how the picture behaves when the user resizes the row or column beneath it. The recommended value for a single-cell picture is PlacementType.MoveAndSize, which causes the picture to move and resize together with its underlying cell, preserving the exact fit.
Workbook (or open an existing one).Worksheet from workbook.getWorksheets().get(0).InputStream (for example, by using FileInputStream) so the stream is closed properly.worksheet.getPictures().add(5, 2, stream) to add a picture anchored to cell C6. Capture the returned Picture reference.UpperLeftRow = 5, UpperLeftColumn = 2, LowerRightRow = 6, LowerRightColumn = 3.picture.setPlacement(PlacementType.MOVE_AND_SIZE) to keep the picture aligned with C6 when the column or row is resized..xlsx file.
The following code demonstrates the complete approach.const AsposeCells = require("aspose.cells-node");
var workbook = new AsposeCells.Workbook();
var worksheet = workbook.getWorksheets().get(0);
var picIndex = worksheet.getPictures().add(5, 2, "logo.png");
var picture = worksheet.getPictures().get(picIndex);
picture.setUpperLeftRow(5);
picture.setUpperLeftColumn(2);
picture.setLowerRightRow(6);
picture.setLowerRightColumn(3);
picture.setPlacement(AsposeCells.PlacementType.MoveAndSize);
workbook.save("output.xlsx", AsposeCells.SaveFormat.Xlsx);
Aspose.Cells also exposes a simpler mechanism for cell-bound images: the Cell.EmbeddedImage property. Assigning image bytes to this property attaches the image to the cell itself, as if it were inline content.
Cell.EmbeddedImage the most concise option when your goal is simply “an image that lives inside this cell.”Workbook (or open an existing one).Worksheet from workbook.getWorksheets().get(0).Files.readAllBytes from java.nio.file.Files).worksheet.getCells().get("C6") or worksheet.getCells().get(5, 2).EmbeddedImage property via cell.setEmbeddedImage(bytes)..xlsx file.
The following code demonstrates the complete approach.const AsposeCells = require("aspose.cells-node");
const fs = require("fs");
var workbook = new AsposeCells.Workbook();
var worksheet = workbook.getWorksheets().get(0);
// Get the target cell C6
var cell = worksheet.getCells().get("C6");
// Read the image file into a byte array
var imageData = fs.readFileSync("logo.png");
// Embed the image directly into the cell
cell.setEmbeddedImage(imageData);
// Optionally adjust row height and column width so the embedded image is more visible
worksheet.getCells().setColumnWidth(2, 30); // Column C (index 2)
worksheet.getCells().setRowHeight(5, 100); // Row 6 (index 5)
// Save the resulting workbook as an .xlsx file
workbook.save("output.xlsx", AsposeCells.SaveFormat.Xlsx);
Both approaches produce a picture that fits inside a single cell, but they differ in how the picture is stored and how it behaves:
PictureCollection.Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.