Add a Picture to an Excel Comment

Add picture to Excel Comment with Microsoft Excel

With Microsoft Excel 2007, it is possible to have an image as the background to a cell comment. In Excel 2007, this is accomplished (supposing the comment has already been added) this way:

  1. Right-click the cell which contains the comment.
  2. Choose Show/Hide Comments and clear any text from the comment.
  3. Click on the border of the comment to select it.
  4. Choose Format, then Comment.
  5. On the Colors and Lines tab, click the arrow for Color.
  6. Click Fill Effects.
  7. On the Picture tab, click Select Picture.
  8. Locate and select the picture
  9. Click OK.

Add picture to Excel Comment with Aspose.Cells

Aspose.Cells provides this valuable feature.

The sample code below creates an XLSX file from scratch, and adds a comment with a picture background to cell A1.

After executing the code, A1 has a comment with a background image.

The output file

todo:image_alt_text

Sample Code

// 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.getSharedDataDir(AddPicturetoExcelComment.class) + "articles/";
// Instantiate a Workbook
Workbook workbook = new Workbook();
// Get a reference of comments collection with the first sheet
CommentCollection comments = workbook.getWorksheets().get(0).getComments();
// Add a comment to cell A1
int commentIndex = comments.add(0, 0);
Comment comment = comments.get(commentIndex);
comment.setNote("First note.");
comment.getFont().setName("Times New Roman");
// Load/Read an image into stream
String logo_url = dataDir + "school.jpg";
// Creating the instance of the FileInputStream object to open the logo/picture in the stream
FileInputStream inFile = new FileInputStream(logo_url);
// Setting the logo/picture
byte[] picData = new byte[inFile.available()];
inFile.read(picData);
// Set image data to the shape associated with the comment
comment.getCommentShape().getFill().setImageData(picData);
// Save the workbook
workbook.save(dataDir + "APToExcelComment_out.xlsx");