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")
使用IronXL簡單明了的API,管理這些註解變得像其他單元格操作一樣簡單,使其成為在您的C#應用程式中建立專業且注釋詳細的Excel文件的重要工具。 無論您正在構建ASP.NET Web 應用程式還是桌面解決方案,IronXL的註解功能都可以無縫整合至您的工作流程中,而不需要COM Interop的複雜性。
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.