文档半岛外围网上直营>>Spire.XLS 系列教程>>Spire.XLS教程:在Java中以编程方式在Excel中添加和阅读注释
Spire.XLS教程:在Java中以编程方式在Excel中添加和阅读注释
Spire.XLS for Java是专业的Java Excel API,使开发人员无需使用Microsoft Office或Microsoft Excel即可创建,管理,操作,转换和打印Excel工作表。
本文演示了如何将常规注释和富文本注释添加到Excel文件,以及如何使用Spire.XLS for Java阅读注释。
添加评论
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);
}
}
常规评论
富文本评论
阅读评论
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());
}
}

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线