OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
Quick Start: Cell Comments & Threads

OpenXLSX-NX provides robust, full-featured support for cell annotations in Microsoft Excel, supporting both the classic 97-2016 comment boxes and the modern Office 365 threaded conversations.

  1. Traditional Comments (Notes): The classic floating yellow boxes with a red triangle indicator in the cell corner. These are typically used for static annotations or instructions.
  2. Modern Threaded Comments: The modern conversational UI (introduced in Office 365) with a purple indicator. These allow multiple users to have a back-and-forth discussion directly tied to a cell.

OpenXLSX-NX offers a highly ergonomic Fluent Chaining API and Document-Level Default Authors to make generating these annotations seamless and zero-boilerplate.


1. Fluent Setup & Default Authors

When generating reports programmatically, you often act on behalf of a system or a single user. You can set a global default author to avoid passing the same name repeatedly.

#include <OpenXLSX.hpp>
using namespace OpenXLSX;
int main() {
doc.create("CommentsDemo.xlsx", XLForceOverwrite);
// 1. Set a global default author for all generated comments/notes
doc.setDefaultAuthor("Financial Auditor");
auto wks = doc.workbook().worksheet("Sheet1");
wks.setName("Modern Comments");
This class encapsulates the concept of an excel file. It is different from the XLWorkbook,...
Definition XLDocument.hpp:97
void create(std::string_view fileName, bool forceOverwrite=XLForceOverwrite)
Initialize a new .xlsx package from a built-in template.
Definition XLDocument.cpp:372
void setDefaultAuthor(const std::string &author)
Set the default author for comments and notes.
Definition XLDocument.cpp:847
XLWorkbook workbook() const
Get the underlying workbook object, containing worksheets and global data state. [[nodiscard]] preven...
Definition XLDocument.cpp:828
void setName(const std::string &sheetName)
Method for renaming the sheet.
Definition XLSheetBase.hpp:212
XLWorksheet worksheet(std::string_view sheetName)
Get the worksheet with the given name.
Definition XLWorkbook.cpp:205
Definition IXLCellProvider.hpp:24
constexpr const bool XLForceOverwrite
Definition XLDocument.hpp:60

2. Modern Threaded Comments (Excel 365)

For modern conversational comments, OpenXLSX-NX provides highly streamlined, object-oriented methods directly on the XLCell object. When you create a modern comment, OpenXLSX-NX automatically generates the invisible legacy fallback structures required by the OOXML standard for backward compatibility with older Excel versions.

// 1. Start a new conversation thread on a cell fluently
// Signature: addComment(Text, [Optional AuthorName])
// Returns an XLThreadedComment object which you can chain replies to.
auto thread = wks.cell("A3")
.value() = "Q3 Projections";
wks.cell("A3").addComment("Can we update the Q3 projections?", "Alice Smith")
.addReply("Yes, I will have them ready by tomorrow.", "Bob Johnson")
.addReply("Great, thanks Bob!", "Alice Smith")
.setResolved(true); // Mark the entire thread as resolved (Excel 365 feature)

3. Traditional Comments (Notes)

If you strictly need the classic yellow boxes, you can use the addNote() fluent method. You also have full access to the underlying XLComments system to manipulate the VML drawing shapes (e.g., resizing the box or making it permanently visible).

// 1. Add a classic yellow note to a cell
wks.cell("C2").value() = "Audit Target";
wks.cell("C2").addNote("Please review these numbers."); // Uses the default "Financial Auditor" author
// 2. Advanced: Modify the physical shape of the Note box
// To modify the shape, we must query the worksheet's comments() collection.
auto shape = wks.comments().shape("C2");
// Make the comment box always visible
shape.style().show();
// Resize the comment box
shape.style().setWidth(300);
shape.style().setHeight(150);

4. Removing Comments

Removing comments can be done directly from the worksheet.

// 1. Remove a modern threaded comment and all its replies
wks.deleteComment("A3");
// 2. Remove a traditional note
wks.deleteNote("C2");
doc.save();
doc.close();
return 0;
}
void close()
Close the document and release underlying ZIP resources, ensuring file handles are closed properly.
Definition XLDocument.cpp:412
void save()
Save changes to the original file path.
Definition XLDocument.cpp:460