LoginSignup
0
0

More than 3 years have passed since last update.

JavaによるExcelコメントの追加、読み取り、削除

Posted at

注釈は、指定されたExcelセルにプロンプ​​トまたは追加情報を追加するために一般的に使用されるリッチテキスト注釈です。Free Spire.XLS for Javaは、JavaアプリケーションでExcelファイルにコメントを無料で追加および操作する機能を開発者に提供します。この記事では、Free Spire.XLS for Javaを使用してExcelドキュメントのコメントを追加、読み取り、削除する方法を紹介します。

基本的な手順:
1. Free Spire.XLS for Javaパッケージをダウンロードして解凍します。
2. libフォルダーのSpire.Xls.jarパッケージを依存関係としてJavaアプリケーションにインポートするか、MavenリポジトリーからJARパッケージをインストールします(pom.xmlファイルを構成するコードについては、以下を参照してください)。
3. Javaアプリケーションで、新しいJava Class(ここではAddComments / ReadComments / DeleteCommentsという名前を付けました)を作成し、対応するJavaコードを入力して実行します。

pom.xmlファイルを構成します:

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls.free</artifactId>
        <version>2.2.0</version>
    </dependency>
</dependencies>

Excelコメントを追加する
次の例は、Free Spire.XLS for Javaを使用してコメントをExcelファイルに追加する方法を示しています、また、注釈テキストの各文字に異なるフォント色を設定します。

import com.spire.xls.*;
public class AddComments {
    public static void main(String[] args){
        //新しいExcelドキュメント
        Workbook workbook = new Workbook();

        //最初のワークシートを入手する
        Worksheet sheet = workbook.getWorksheets().get(0);

        //ワークシート名を設定
        sheet.setName("注釈");

        //セル[1,1]にテキストを追加
        CellRange range = sheet.getCellRange(1,1);
        range.setText("コメントを追加:");

        //セルにテキストを追加[5,1]
        CellRange range1 = sheet.getCellRange(5, 1);
        range1.setText("注釈");

        //セルにコメントを追加[5,1]
        range1.getComment().setText("これはコメントです\n複数行にすることができます");

        //注釈を表示する
        range1.getComment().setVisible(true);

        //注釈の高さを設定する
        range1.getComment().setHeight(100);

        //フォントを作成してフォントの色を設定する
        ExcelFont fontBlue = workbook.createFont();
        fontBlue.setKnownColor(ExcelColors.LightBlue);
        ExcelFont fontGreen = workbook.createFont();
        fontGreen.setKnownColor(ExcelColors.LightGreen);

        //注釈テキストの各文字のフォントを設定します
        range1.getComment().getRichText().setFont(0, 3, fontGreen);
        range1.getComment().getRichText().setFont(4, 6, fontBlue);
        range1.getComment().getRichText().setFont(7, 9, fontGreen);

        //結果ファイルを保存
        workbook.saveToFile("AddComments.xlsx", ExcelVersion.Version2013);
    }
}

AddComments.jpg

Excelのコメントを読む
Free Spire.XLS for Javaは、すべての注釈の読み取りをサポートしますまた、Excelワークシートの指定したセルに関連付けられた特定のコメント。

import com.spire.xls.*;
public class ReadComments {
    public static void main(String[] args){
        //Excelドキュメントを読み込む
        Workbook workbook = new Workbook();
        workbook.loadFromFile("AddComments.xlsx");

        //最初のワークシートを入手する
        Worksheet sheet = workbook.getWorksheets().get(0);

        //ワークシートに含まれるすべてのコメントを印刷する
        for(int i = 0; i < sheet.getComments().getCount(); i ++){
            String comment = sheet.getComments().get(i).getText();
            System.out.println(comment);
        }

        //指定したセルに関連付けられたコメントを印刷します
        //System.out.println(sheet.getCellRange(5,1).getComment().getText());
    }
}

ReadComments.jpg

Excelのコメントを削除する
Free Spire.XLS for Javaを使用すると、すべてのコメントを削除でき、Excelワークシートの指定したセルに関連付けられている特定のコメントも削除できます。

import com.spire.xls.*;
public class DeleteComments {
    public static void main(String[] args){
        //Excelドキュメントを読み込む
        Workbook workbook = new Workbook();
        workbook.loadFromFile("AddComments.xlsx");

        //最初のワークシートを入手する
        Worksheet sheet = workbook.getWorksheets().get(0);

        //ワークシートのすべてのコメントを削除する
        for(int i = 0; i < sheet.getComments().getCount(); i ++){
            sheet.getComments().get(i).remove();
        }

        //指定したセルに関連付けられているコメントを削除します
        sheet.getCellRange(5,1).getComment().remove();

        workbook.saveToFile("DeleteComments.xlsx", ExcelVersion.Version2013);
    }
}

DeleteComments.jpg

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0