logo Spire.XLS 系列教程 我也要发布文档

在Java中以编程方式在Excel中添加和阅读注释


Spire.XLS for Java是专业的Java Excel API,使开发人员无需使用Microsoft Office或Microsoft Excel即可创建,管理,操作,转换和打印Excel工作表。

本文演示了如何将常规注释和富文本注释添加到Excel文件,以及如何使用Spire.XLS for Java阅读注释。

免费下载Spire.XLS最新版


添加评论

import com.spire.xls.*;

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

        //Create fonts
        ExcelFont font = workbook.createFont();
        font.setFontName("Arial");
        font.setSize(11);
        font.setKnownColor(ExcelColors.Orange);
        ExcelFont fontBlue = workbook.createFont();
        fontBlue.setKnownColor(ExcelColors.LightBlue);
        ExcelFont fontGreen = workbook.createFont();
        fontGreen.setKnownColor(ExcelColors.LightGreen);

        //Add regular comment to specific cell range
        CellRange range = sheet.getCellRange("A1");
        range.setText("Regular comment");
        range.getComment().setText("Regular comment");
        range.autoFitColumns();

        //Add rich text comment to specific cell range
        range = sheet.getCellRange("A2");
        range.setText("Rich text comment");
        range.getRichText().setFont(0, 16, font);
        range.autoFitColumns();
        range.getComment().getRichText().setText("Rich text comment");
        range.getComment().getRichText().setFont(0, 4, fontGreen);
        range.getComment().getRichText().setFont(5, 9, fontBlue);

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

常规评论

Excel开发组件Spire.XLS教程:在Java中以编程方式在Excel中添加和阅读注释

富文本评论

Excel开发组件Spire.XLS教程:在Java中以编程方式在Excel中添加和阅读注释

阅读评论

import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

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 the comment
        System.out.println("A1 Comment = " + sheet.getCellRange("A1").getComment().getText());
        System.out.println("A2 Comment = " + sheet.getCellRange("A2").getComment().getRichText().getRtfText());
    }
}
Excel开发组件Spire.XLS教程:在Java中以编程方式在Excel中添加和阅读注释


慧都是E-iceblue官方友好合作伙伴,如果您对spire.xls,可以联系在线客服了解具体授权价格和使用机制