コメントとノートの管理
紹介
コメントはセルに追加情報を追加するために使用されます。Aspose.Cellsでは、セルにコメントを追加するための2つの方法が提供されます。最初の方法は、デザイナーファイルで手動でコメントを作成することです。これらのコメントはその後、Aspose.Cellsを使用してインポートされます。2番目は、Aspose.Cells APIを使用してランタイムでコメントを追加することです。このトピックでは、Aspose.Cells APIを使用してセルにコメントを追加する方法について説明します。コメントの書式設定についても説明します。
コメントの追加
CommentsコレクションのAddメソッド(Worksheetオブジェクト内にカプセル化)を呼び出すことによって、セルにコメントを追加します。新しいCommentオブジェクトは、コメントインデックスを渡してCommentsコレクションからアクセスできます。Commentオブジェクトにアクセスした後、CommentオブジェクトのNoteプロパティを使用してコメントノートをカスタマイズします。
// 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"); |
コメントの書式設定
コメントの高さ、幅、フォント設定を構成することで、コメントの外観を書式設定することも可能です。
// 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"); |
コメントに画像を追加する
Microsoft Excel 2007では、セルコメントの背景として画像を使用することもできます。Excel 2007では、次の手順を実行することでこれが実現されます。(すでにセルにコメントを追加していることを前提とします。)
- コメントを含むセルを右クリックします。
- 表示/非表示を選択し、コメント内のテキストをクリアします。
- コメントの境界線をクリックして選択します。
- 書式、次にコメントを選択します。
- 色と線タブで、色リストを展開します。
- 塗りつぶしの効果をクリックします。
- 図タブで、ピクチャを選択をクリックします。
- 画像を探し、選択します。
- すべてのダイアログが閉じるまでOKをクリックします。
Aspose.Cellsもこの機能を提供します。以下は、セル"A1"に画像を背景として設定したコメントを追加し、からXLSXファイルをスクラッチから作成するコードサンプルです。
// 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); |