using IronXL;using System.Linq;WorkBook workBook = WorkBook.Create();WorkSheet workSheet = workBook.DefaultWorkSheet;Cell cellA1 = workSheet["A1"].First();Cell cellD1 = workSheet["D1"].First();// Add commentscellA1.AddComment("Hello World!", "John Doe"); // Add comment with content and author. The comment is invisible by default.cellD1.AddComment(null, null, true); // Add comment with no content and no author. The comment is set to be visible.workBook.SaveAs("addComment.xlsx");
using IronXL;
using System.Linq;
WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;
Cell cellA1 = workSheet["A1"].First();
Cell cellD1 = workSheet["D1"].First();
// Add comments
cellA1.AddComment("Hello World!", "John Doe"); // Add comment with content and author. The comment is invisible by default.
cellD1.AddComment(null, null, true); // Add comment with no content and no author. The comment is set to be visible.
workBook.SaveAs("addComment.xlsx");
ImportsIronXLImportsSystem.LinqPrivate workBook AsWorkBook = WorkBook.Create()Private workSheet AsWorkSheet = workBook.DefaultWorkSheetPrivate cellA1 AsCell = workSheet("A1").First()Private cellD1 AsCell = workSheet("D1").First()' Add commentscellA1.AddComment("Hello World!", "John Doe") ' Add comment with content and author. The comment is invisible by default.cellD1.AddComment(Nothing, Nothing, True) ' Add comment with no content and no author. The comment is set to be visible.workBook.SaveAs("addComment.xlsx")
Imports IronXL
Imports System.Linq
Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
Private cellA1 As Cell = workSheet("A1").First()
Private cellD1 As Cell = workSheet("D1").First()
' Add comments
cellA1.AddComment("Hello World!", "John Doe") ' Add comment with content and author. The comment is invisible by default.
cellD1.AddComment(Nothing, Nothing, True) ' Add comment with no content and no author. The comment is set to be visible.
workBook.SaveAs("addComment.xlsx")
下面是一个在循环中为多个单元格添加注释的实用示例,可用于添加批量注释或验证注释:
using IronXL;using System;// Load an existing workbookWorkBook workBook = WorkBook.Load("salesData.xlsx");WorkSheet workSheet = workBook.DefaultWorkSheet;// Add comments to cells that meet specific criteriafor (int row = 2; row <= 10; row++){ var cell = workSheet[$"D{row}"].First(); var value = cell.DoubleValue; if (value > 1000) { // Add performance comment for high values cell.AddComment($"Excellent performance! Value: {value:C}", "Sales Manager", true); } else if (value < 500) { // Add improvement comment for low values cell.AddComment($"Needs attention. Current: {value:C}", "Sales Manager", false); }}// Add timestamp comment to track last updatevar updateCell = workSheet["A1"].First();updateCell.AddComment($"Last updated: {DateTime.Now:yyyy-MM-dd HH:mm}", "System");workBook.SaveAs("salesDataWithComments.xlsx");
using IronXL;
using System;
// Load an existing workbook
WorkBook workBook = WorkBook.Load("salesData.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Add comments to cells that meet specific criteria
for (int row = 2; row <= 10; row++)
{
var cell = workSheet[$"D{row}"].First();
var value = cell.DoubleValue;
if (value > 1000)
{
// Add performance comment for high values
cell.AddComment($"Excellent performance! Value: {value:C}", "Sales Manager", true);
}
else if (value < 500)
{
// Add improvement comment for low values
cell.AddComment($"Needs attention. Current: {value:C}", "Sales Manager", false);
}
}
// Add timestamp comment to track last update
var updateCell = workSheet["A1"].First();
updateCell.AddComment($"Last updated: {DateTime.Now:yyyy-MM-dd HH:mm}", "System");
workBook.SaveAs("salesDataWithComments.xlsx");
ImportsIronXLImportsSystem' Load an existing workbookDim workBook AsWorkBook = WorkBook.Load("salesData.xlsx")Dim workSheet AsWorkSheet = workBook.DefaultWorkSheet' Add comments to cells that meet specific criteriaFor row AsInteger = 2 To 10 Dim cell = workSheet($"D{row}").First() Dim value = cell.DoubleValue If value > 1000 Then ' Add performance comment for high values cell.AddComment($"Excellent performance! Value: {value:C}", "Sales Manager", True) ElseIf value < 500 Then ' Add improvement comment for low values cell.AddComment($"Needs attention. Current: {value:C}", "Sales Manager", False) End IfNext' Add timestamp comment to track last updateDim updateCell = workSheet("A1").First()updateCell.AddComment($"Last updated: {DateTime.Now:yyyy-MM-dd HH:mm}", "System")workBook.SaveAs("salesDataWithComments.xlsx")
Imports IronXL
Imports System
' Load an existing workbook
Dim workBook As WorkBook = WorkBook.Load("salesData.xlsx")
Dim workSheet As WorkSheet = workBook.DefaultWorkSheet
' Add comments to cells that meet specific criteria
For row As Integer = 2 To 10
Dim cell = workSheet($"D{row}").First()
Dim value = cell.DoubleValue
If value > 1000 Then
' Add performance comment for high values
cell.AddComment($"Excellent performance! Value: {value:C}", "Sales Manager", True)
ElseIf value < 500 Then
' Add improvement comment for low values
cell.AddComment($"Needs attention. Current: {value:C}", "Sales Manager", False)
End If
Next
' Add timestamp comment to track last update
Dim updateCell = workSheet("A1").First()
updateCell.AddComment($"Last updated: {DateTime.Now:yyyy-MM-dd HH:mm}", "System")
workBook.SaveAs("salesDataWithComments.xlsx")
如何编辑现有评论?
为什么Comment属性有时返回空?
当选定的单元格没有关联的评论时,null。 在以编程方式迭代单元格时,这种情况很常见。 在尝试修改评论属性之前,总是检查NullReferenceException。 这种模式与 IronXL'Excel 综合 API 中的其他单元格属性类似。
using IronXL 添加注释的最小代码只有一行:IronXL.WorkBook.Create().DefaultWorkSheet["B2"].First().AddComment("Quick tip!", "Dev")。这样就创建了一个工作簿,选择了单元格 B2,并添加了一个包含内容和作者的注释,无需额外设置。
Why might the 'Comment' property return null in IronXL?
The 'Comment' property returns null if a cell does not have an associated comment, which is common when iterating through cells without comments.
How do I make changes to comment visibility after creation in IronXL?
Access the cell's 'Comment' object and adjust the 'IsVisible' property to 'true' or 'false' as needed to modify comment visibility programmatically.
What types of applications can benefit from using IronXL's comment functionality?
IronXL’s comment functionality is beneficial for ASP.NET web applications and desktop solutions that require annotated, professional, and well-documented Excel files.