Gestionar comentarios y notas

Introducción

Los comentarios se utilizan para agregar información adicional a las celdas. Aspose.Cells proporciona dos métodos para agregar comentarios a las celdas. El primero es crear comentarios en un archivo de diseño manualmente. Estos comentarios luego se importan utilizando Aspose.Cells. El segundo es agregar comentarios usando la API de Aspose.Cells en tiempo de ejecución. Este tema trata sobre cómo agregar comentarios a las celdas mediante la API de Aspose.Cells. También se explicará el formato de comentarios.

Agregar un comentario

Agregar un comentario a una celda llamando al método Add de la colección Comments (encapsulado en el objeto Worksheet). El nuevo objeto Comment se puede acceder desde la colección Comments pasando el índice del comentario. Después de acceder al objeto Comment, personalice la nota del comentario utilizando la propiedad Note del objeto Comment.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int sheetIndex = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[sheetIndex];
// Adding a comment to "F5" cell
int commentIndex = worksheet.Comments.Add("F5");
// Accessing the newly added comment
Comment comment = worksheet.Comments[commentIndex];
// Setting the comment note
comment.Note = "Hello Aspose!";
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Formato de comentario

También es posible dar formato a la apariencia de los comentarios configurando su altura, ancho y ajustes de fuente.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int sheetIndex = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[sheetIndex];
// Adding a comment to "F5" cell
int commentIndex = worksheet.Comments.Add("F5");
// Accessing the newly added comment
Comment comment = worksheet.Comments[commentIndex];
// Setting the comment note
comment.Note = "Hello Aspose!";
// Setting the font size of a comment to 14
comment.Font.Size = 14;
// Setting the font of a comment to bold
comment.Font.IsBold = true;
// Setting the height of the font to 10
comment.HeightCM = 10;
// Setting the width of the font to 2
comment.WidthCM = 2;
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Agregar una imagen al comentario

Con Microsoft Excel 2007, también es posible tener una imagen como fondo de un comentario de celda. En Excel 2007, esto se logra siguiendo los siguientes pasos. (Se supone que ya ha agregado un comentario de celda)

  1. Haga clic con el botón derecho en la celda que contiene el comentario.
  2. Seleccione Mostrar/Ocultar comentarios, y elimine cualquier texto del comentario.
  3. Haga clic en el borde del comentario para seleccionarlo.
  4. Seleccione Formato, luego Comentario.
  5. En la pestaña Colores y líneas, expanda la lista de Color.
  6. Haga clic en Efectos de relleno.
  7. En la pestaña Imagen, haga clic en Seleccionar imagen.
  8. Ubique y seleccione la imagen.
  9. Haga clic en Aceptar hasta que se cierren todos los diálogos.

Aspose.Cells también proporciona esta función. A continuación se muestra un ejemplo de código que crea un archivo XLSX desde cero, agregando un comentario a la celda “A1” con una imagen como fondo.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiate a Workbook
Workbook workbook = new Workbook();
// Get a reference of comments collection with the first sheet
CommentCollection comments = workbook.Worksheets[0].Comments;
// Add a comment to cell A1
int commentIndex = comments.Add(0, 0);
Comment comment = comments[commentIndex];
comment.Note = "First note.";
comment.Font.Name = "Times New Roman";
// Load an image into stream
Bitmap bmp = new Bitmap(dataDir + "logo.jpg");
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
// Set image data to the shape associated with the comment
comment.CommentShape.Fill.ImageData = ms.ToArray();
// Save the workbook
workbook.Save(dataDir + "book1.out.xlsx", Aspose.Cells.SaveFormat.Xlsx);

Temas avanzados