本文将介绍如何使用 spire.xls for java给excel文档添加批注以及如何读取excel文档的现有批注的内容。
添加批注
import com.spire.xls.*;
public class insertcomments {
public static void main(string[] args){
//新建excel文档
workbook workbook = new workbook();
//获取第一张工作表
worksheet sheet = workbook.getworksheets().get(0);
//创建字体
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);
//给指定的excel单元格添加普通批注
cellrange range = sheet.getcellrange("a1");
range.settext("spire.xls");
range.getcomment().settext("excel文档处理组件");
range.autofitcolumns();
//给指定的excel单元格添加富文本批注
range = sheet.getcellrange("a2");
range.settext("spire.xls");
range.getrichtext().setfont(0, 8, font);
range.autofitcolumns();
range.getcomment().getrichtext().settext("excel文档处理组件");
range.getcomment().getrichtext().setfont(0, 4, fontgreen);
range.getcomment().getrichtext().setfont(5, 6, fontblue);
//保存结果文档
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){
//加载excel文档
workbook workbook = new workbook();
workbook.loadfromfile("addcomments.xlsx");
//获取第一张工作表
worksheet sheet = workbook.getworksheets().get(0);
//打印单元格a1、a2的批注内容
system.out.println("a1 comment = " sheet.getcellrange("a1").getcomment().gettext());
system.out.println("a2 comment = " sheet.getcellrange("a2").getcomment().getrichtext().getrtftext());
}
}