Gestione dei commenti e delle note

Introduzione

I commenti vengono utilizzati per aggiungere informazioni aggiuntive alle celle. Aspose.Cells fornisce due metodi per aggiungere commenti alle celle. Il primo è creare commenti manualmente in un file di progettazione. Successivamente, questi commenti vengono importati utilizzando Aspose.Cells. Il secondo è aggiungere commenti utilizzando l’API di Aspose.Cells in fase di esecuzione. Questo argomento tratta l’aggiunta di commenti alle celle utilizzando l’API di Aspose.Cells. Verrà inoltre spiegato come formattare i commenti.

Aggiungere un commento

Aggiungi un commento a una cella chiamando il metodo Add della raccolta Comments (incapsulato nell’oggetto Worksheet). Il nuovo oggetto Comment può essere accessibile dalla raccolta Comments passando l’indice del commento. Dopo aver accesso all’oggetto Comment, personalizza la nota del commento utilizzando la proprietà Note dell’oggetto 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");

Formattazione del commento

È anche possibile formattare l’aspetto dei commenti configurando la loro altezza, larghezza e impostazioni del carattere.

// 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");

Aggiungi un’immagine al commento

Con Microsoft Excel 2007, è anche possibile avere un’immagine come sfondo di un commento di cella. In Excel 2007 questo si realizza seguendo i seguenti passaggi. (Si suppone che tu abbia già aggiunto un commento alla cella.)

  1. Fare clic con il pulsante destro del mouse sulla cella che contiene il commento.
  2. Selezionare Mostra/Nascondi commenti, e cancellare eventuali testi dal commento.
  3. Fare clic sul bordo del commento per selezionarlo.
  4. Selezionare Formato, quindi Commento.
  5. Nella scheda Colori e linee, espandere l’elenco Colore.
  6. Fare clic su Effetti di riempimento.
  7. Nella scheda Immagine, fare clic su Seleziona immagine.
  8. Trovare e selezionare l’immagine.
  9. Fare clic su OK finché tutte le finestre di dialogo non si sono chiuse.

Aspose.Cells fornisce anche questa funzionalità. Di seguito è riportato un esempio di codice che crea un file XLSX da zero, aggiungendo un commento alla cella “A1” con un’immagine impostata come sfondo.

// 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);

Argomenti avanzati