Gestisci commenti e note
introduzione
I commenti vengono utilizzati per aggiungere ulteriori informazioni alle celle. Aspose.Cells fornisce due metodi per aggiungere commenti alle celle. Il primo consiste nel creare manualmente i commenti in un file di progettazione. Questi commenti vengono quindi importati utilizzando Aspose.Cells. Il secondo consiste nell’aggiungere commenti utilizzando Aspose.Cells API in fase di esecuzione. Questo argomento illustra l’aggiunta di commenti alle celle utilizzando Aspose.Cells API. Verrà inoltre illustrata la formattazione dei commenti.
Aggiunta di un commento
Aggiungi un commento a una cella chiamando il metodoCommenti della collezioneAggiungere metodo (incapsulato nel fileFoglio di lavoro oggetto). Il nuovoCommento è possibile accedere all’oggetto daCommenti raccolta passando l’indice dei commenti. Dopo aver effettuato l’accesso alCommento oggetto, personalizzare la nota di commento utilizzando ilCommento dell’oggettoNotaproprietà.
// 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 dei commenti
È anche possibile formattare l’aspetto dei commenti configurandone l’altezza, la larghezza e le 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 ciò si ottiene eseguendo i seguenti passaggi. (Suppongono che tu abbia già aggiunto un commento di cella.)
- Fare clic con il pulsante destro del mouse sulla cella che contiene il commento.
- SelezionareMostra/Nascondi commentie cancella qualsiasi testo dal commento.
- Fare clic sul bordo del commento per selezionarlo.
- SelezionareFormato , poiCommento.
- SulColori e Linee scheda, espandere il fileColore elenco.
- ClicEffetti di riempimento.
- SulImmagine scheda, fare clicSeleziona Immagine.
- Individua e seleziona l’immagine.
- ClicOK fino alla chiusura di tutte le finestre di dialogo.
Aspose.Cells fornisce anche questa funzione. 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); |