在Java中的Excel中添加,读取和删除注释

A comment is a rich text note which we often use to add tips or additional information to a particular Excel cell. Free Spire.XLS for Java library provides Java developers the ability to add and manipulate comments in Excel files within Java applications. In this article, we will introduce how to add, read and delete comments in Excel using Free Spire.XLS for Java library.

Installation

First of all, you need to download spire.xls.jar and add it to your project as dependency. If you use maven, you need to add the following dependencies to your pom.xml file.

<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>

Add Comments

下面的示例演示如何使用免费的Java Spire.XLS将注释添加到Excel文件,并以不同的颜色设置注释文本中各个字符的格式。

import com.spire.xls.*;

public class AddComments {
    public static void main(String[] args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);
        //Set worksheet name
        sheet.setName("Comments");

        //Add text to cell [1,1]
        CellRange range = sheet.getCellRange(1,1);
        range.setText("Working with comment:");
        //Add text to cell [5,1]
        CellRange range1 = sheet.getCellRange(5, 1);
        range1.setText("Comment");
        //Add comment to cell [5,1]
        range1.getComment().setText("It's a comment.\nmulti-line,\nvisible and\nformatted.");
        //Show comment
        range1.getComment().setVisible(true);
        //Set comment hight
        range1.getComment().setHeight(100);
        //Create fonts and set font color
        ExcelFont fontBlue = workbook.createFont();
        fontBlue.setKnownColor(ExcelColors.LightBlue);
        ExcelFont fontGreen = workbook.createFont();
        fontGreen.setKnownColor(ExcelColors.LightGreen);
        //Set fonts for individual characters within the comment text
        range1.getComment().getRichText().setFont(0, 4, fontGreen);
        range1.getComment().getRichText().setFont(5, 6, fontBlue);
        range1.getComment().getRichText().setFont(7, 15, fontGreen);

        //Save the file
        workbook.saveToFile("AddComments.xlsx", ExcelVersion.Version2013);
    }
}

Alt Text

Read Comments

Java的免费Spire.XLS支持读取所有注释以及读取与Excel工作表中指定单元格相关的特定注释。

import com.spire.xls.*;

public class ReadComments {
    public static void main(String[] args){
        //Load Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("AddComments.xlsx");
        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Print out all of the comments in the worksheet
        for(int i = 0; i < sheet.getComments().getCount(); i ++){
            String comment = sheet.getComments().get(i).getText();
            System.out.println(comment);
        }

        //Print out comment associated with a specified cell
        //System.out.println(sheet.getCellRange(5,1).getComment().getText());
    }
}

Alt Text

Delete Comments

我们可以删除所有注释,也可以删除与Excel工作表中指定单元格关联的特定注释。

import com.spire.xls.*;

public class DeleteComments {
    public static void main(String[] args){
        //Load Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("AddComments.xlsx");
        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Delete all of the comments
        for(int i = 0; i < sheet.getComments().getCount(); i ++){
            sheet.getComments().get(i).remove();
        }

        //Delete comment associated with a specified cell
        sheet.getCellRange(5,1).getComment().remove();

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

Alt Text

from: https://dev.to//eiceblue/add-read-and-delete-comments-in-excel-in-java-1jof

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值