Hantera kommentarer och noteringar
Introduktion
Kommentarer används för att lägga till ytterligare information i celler. Aspose.Cells tillhandahåller två metoder för att lägga till kommentarer i celler. Den första är att skapa kommentarer i en designerfil manuellt. Dessa kommentarer importeras sedan med hjälp av Aspose.Cells. Den andra är att lägga till kommentarer med hjälp av Aspose.Cells API vid körning. Detta ämne diskuterar att lägga till kommentarer i celler med hjälp av Aspose.Cells API. Formatering av kommentarer kommer också att förklaras.
Lägga till en kommentar
Lägg till en kommentar i en cell genom att anropa Comments samlingen Add metod (inkapslad i Worksheet objektet). Det nya Comment objektet kan nås från Comments samlingen genom att skicka kommentarindexet. Efter att ha fått tillgång till Comment objektet, anpassa kommentarnoteringen genom att använda Comment objektets Note egenskap.
// 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"); |
Kommentarformatering
Det är också möjligt att formatera kommentarers utseende genom att konfigurera deras höjd, bredd och teckensnittsinställningar.
// 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"); |
Lägg till en bild till en kommentar
Med Microsoft Excel 2007 är det också möjligt att ha en bild som bakgrund till en cellkommentar. I Excel 2007 uppnås detta genom följande steg. (De förutsätter att du redan har lagt till en cellkommentar.)
- Högerklicka på cellen som innehåller kommentaren.
- Välj ** Visa/dölj kommentarer ** och ta bort all text från kommentaren.
- Klicka på kommentarens kant för att markera den.
- Välj ** Formatera **, sedan ** Kommentar **.
- På fliken ** Färg och linjer **, expandera Färg listan.
- Klicka på Fyllnings effekter.
- På fliken Bild, klicka på Välj bild.
- Hitta och välj bilden.
- Klicka OK tills alla dialogrutor har stängts.
Aspose.Cells tillhandahåller också den här funktionen. Nedan finns ett kodexempel som skapar en XLSX-fil från grunden och lägger till en kommentar i cellen “A1” med en bild som bakgrund.
// 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); |