Excelのコメントは、任意のセルに挿入できるメモです。例えば、数式の説明、提案、他の人のためのメモなど、さまざまな目的に使用することができます。コメントを挿入すると、ユーザーは必要に応じてそれを編集したり削除したりすることができます。この記事では、無料のFree Spire.XLS for Java APIを使用して、JavaでExcelにコメントを挿入、編集、削除する方法を説明します。
【依存関係の追加】
この方法は、無料のFree Spire.XLS for Javaが必要ですので、先にjarファイルをインポートしてください。
1. Maven
Maven を使用している場合、プロジェクトの pom.xml ファイルに以下のコードを追加することで、簡単にアプリケーションに JAR ファイルをインポートすることができます。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
2. 公式サイトよりJarファイルをダウンロード
まず、Free Spire.XLS for Java の公式サイトよりzipファイルをダウンロードします。zipファイルを解凍し、libフォルダの下にあるSpire.Xls.jarファイルを依存関係としてプロジェクトにインポートしてください。
Excelにコメントを挿入する
Free Spire.XLS for Java は、特定のセルにコメントを追加するための Worksheet.getCellRange("rangeName").addComment() メソッドを提供します。コメントを追加したら、ExcelComment クラスが提供するメソッドを使用して、コメントのテキストや書式を設定したり、コメント内に画像を充填したりすることができます。
次の例では、Free Spire.XLS for Javaを使用して、Javaで通常のテキストコメントを追加する方法、書式付きコメントを追加する方法、画像付きコメントを追加する方法を説明します。
Java
import com.spire.xls.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class addComment {
public static void main(String[] args) throws IOException {
//Workbook クラスのインスタンスを作成する
Workbook workbook = new Workbook();
//Excelファイルを読み込む
workbook.loadFromFile("C:/ジャイアントパンダ.xlsx");
//最初のワークシートを取得する
Worksheet sheet = workbook.getWorksheets().get(0);
//セルC6にコメントを追加する
ExcelComment textComment = sheet.getCellRange("B6").addComment();
//コメントのテキストを設定する
textComment.setText("ジャイアントパンダは、パンダ熊、または単にパンダとも呼ばれています。");
//コメントの高さと幅を設定する
textComment.setHeight(100);
textComment.setWidth(200);
//コメントの位置を設定する
textComment.setCommentLocation(true, false);
//コメントを表示するように設定する
textComment.setVisible(true);
//フォントを作成する
ExcelFont font = workbook.createFont();
font.setFontName("Yu Gothic Medium");
font.setSize(12);
font.setColor(Color.orange);
font.isBold(true);
//セルF6にコメントを追加する
ExcelComment richTextComment = sheet.getCellRange("F6").addComment();
//コメントの高さと幅を設定する
richTextComment.setHeight(100);
richTextComment.setWidth(200);
//コメントのテキストを設定する
richTextComment.getRichText().setText("ジャイアントパンダは中国固有種の熊です。");
//コメントのフォントを設定する
richTextComment.getRichText().setFont(0, 50, font);
richTextComment.setTextRotation(TextRotationType.LeftToRight);
//コメントのテキストの配置を設定する
richTextComment.setVAlignment(CommentVAlignType.Center);
richTextComment.setHAlignment(CommentHAlignType.Justified);
//コメントの位置を設定する
richTextComment.setCommentLocation(true, false);
//コメントを表示するように設定する
richTextComment.setVisible(true);
//セルB12にコメントを追加する
ExcelComment imageComment = sheet.getCellRange("B12").addComment();
//画像を読み込む
BufferedImage bufferedImage = ImageIO.read(new File("C:/ジャイアントパンダ.png"));
//画像を使用してコメントを充填する
imageComment.getFill().customPicture(bufferedImage, "C:/ジャイアントパンダ.png");
//コメントの高さと幅を設定する
imageComment.setHeight(bufferedImage.getHeight());
imageComment.setWidth(bufferedImage.getWidth());
//コメントの位置を設定する
imageComment.setCommentLocation(true, false);
//コメントを表示するように設定する
imageComment.setVisible(true);
//結果ファイルを保存する
workbook.saveToFile("コメントの追加.xlsx", ExcelVersion.Version2016);
}
}
【結果のExcelファイル】
Excelでのコメントを編集する
特定のセルのコメントを編集したい場合は、Worksheet.getCellRange("rangeName").getComment() メソッドを使用してセル内のコメントを取得し、ExcelComment クラスのメソッドを使用してコメントのテキスト、高さ、幅などの属性を編集することが可能です。
次の例は、Excelワークシートの指定されたセルのコメントを編集する方法を示しています。
Java
import com.spire.xls.ExcelComment;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class editComment {
public static void main(String[] args){
//Workbook クラスのインスタンスを作成する
Workbook workbook = new Workbook();
//Excelファイルを読み込む
workbook.loadFromFile("コメントの追加.xlsx");
//最初のワークシートを取得する
Worksheet sheet = workbook.getWorksheets().get(0);
//セルB6のコメントを取得する
ExcelComment comment1 = sheet.getCellRange("B6").getComment();
//コメントのテキストを変更する
comment1.setText("私は野生のパンダを見たことがありません。");
//コメントの高さと幅を変更する
comment1.setHeight(100);
comment1.setWidth(150);
comment1.setVisible(true);
//セルF6のコメントを取得する
ExcelComment comment2 = sheet.getCellRange("F6").getComment();
//コメントの大きさを変更する
comment2.setAutoSize(true);
//結果ファイルを保存する
workbook.saveToFile("コメントの編集.xlsx", ExcelVersion.Version2013);
}
}
【結果のExcelファイル】
Excelからコメントを削除する
Worksheet.getCellRange("rangeName").getComment().remove() メソッドを使用して特定のセルからコメントを削除したり、Worksheet.getComments().Clear() メソッドを使用して特定のワークシートからすべてのコメントを削除したりすることができます。
次の例は、Excelのワークシートからすべてのコメントを削除する方法を示しています。
Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class deleteComments {
public static void main(String[] args){
//Workbook クラスのインスタンスを作成する
Workbook workbook = new Workbook();
//Excelファイルを読み込む
workbook.loadFromFile("コメントの追加.xlsx");
//最初のワークシートを取得する
Worksheet sheet = workbook.getWorksheets().get(0);
//セルF6からコメントを削除する
//sheet.getCellRange("F6").getComment().remove();
//ワークシートのコメントをすべて削除する
sheet.getComments().clear();
//結果ファイルを保存する
workbook.saveToFile("コメントの削除.xlsx", ExcelVersion.Version2013);
}
}
【結果のExcelファイル】
この記事から、Excelでコメントを扱う基本的なスキルを学ぶことができます。もし、まだ理解できないことがあれば、Spire.XLSフォーラムでより多くの情報を入手してください。