Comments (also called notes in newer Excel versions) allow you to attach additional information to cells. They appear as small indicators in the corner of cells and display their content when hovering over or clicking the cell.
use PhpOffice\PhpSpreadsheet\Style\Color;$comment = $spreadsheet->getActiveSheet()->getComment('A1');$comment->setAuthor('System');// Title in bold and red$titleRun = $comment->getText()->createTextRun('Important Notice:');$titleRun->getFont() ->setBold(true) ->setColor(new Color(Color::COLOR_RED)) ->setSize(12);$comment->getText()->createTextRun("\r\n");// Body text$bodyRun = $comment->getText()->createTextRun('This value requires approval.');$bodyRun->getFont() ->setItalic(true) ->setSize(10);
$comment = $spreadsheet->getActiveSheet()->getComment('C5');$comment->setWidth('200px');$comment->setHeight('100px');// Set margins$comment->setMarginLeft('50px');$comment->setMarginTop('50px');
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;$worksheet = $spreadsheet->getActiveSheet();$worksheet->setCellValue('B5', 'Product Name');// Create drawing for background$drawing = new Drawing();$drawing->setName('Product Image');$drawing->setPath('/path/to/product.png');// Add to comment$comment = $worksheet->getComment('B5');$comment->setBackgroundImage($drawing);// Resize comment to match image$comment->setSizeAsBackgroundImage();
$comment = $worksheet->getComment('B2');$comment->setAuthor('Data Validator');$titleRun = $comment->getText()->createTextRun('Valid Range:');$titleRun->getFont()->setBold(true);$comment->getText()->createTextRun("\r\n");$comment->getText()->createTextRun('Please enter a value between 0 and 100');$comment->setWidth('250px');$comment->setHeight('75px');
$comment = $worksheet->getComment('F10');$comment->setAuthor('System');$headerRun = $comment->getText()->createTextRun('Calculation:');$headerRun->getFont()->setBold(true);$comment->getText()->createTextRun("\r\n");$comment->getText()->createTextRun('=SUM(F2:F9)');$comment->getText()->createTextRun("\r\n\r\n");$comment->getText()->createTextRun('This value is automatically calculated.');