Java 添加、删除Excel 批注
1、可在e-iceblue中文官网下载包,解压后,将lib文件夹中的jar文件导入Java程序;或者通过maven仓库下载导入jar,maven仓库路径配置及依赖添加可以参考官网教程。导入效果如下图:

1、import com.spire.xls.*;
import java.awt.*;
public class AddComment {
public static void main(String[] args) {
//加载Excel文档
Workbook wb = new Workbook();
wb.loadFromFile("test.xlsx");
//获取工作表
Worksheet sheet = wb.getWorksheets().get(0);
//创建字体
ExcelFont font = wb.createFont();
font.setFontName("Arial");
font.setSize(11);
font.setKnownColor(ExcelColors.Orange);
ExcelFont fontBlue = wb.createFont();
fontBlue.setKnownColor(ExcelColors.LightBlue);
ExcelFont fontGreen = wb.createFont();
fontGreen.setKnownColor(ExcelColors.LightGreen);
//给指定的Excel单元格添加普通批注
CellRange range = sheet.getCellRange("H2");
range.getComment().setText("税前价格");
range.getComment().setName("批注1");
range.getComment().setTextRotation(TextRotationType.TopToBottom);
range.autoFitColumns();
range.getComment().setVisible(true);//设置批注隐藏或显示
range.getComment().getFill().customPicture("logo.png");//背景图片填充
//range.getComment().getFill().setForeColor(new Color(255,228,225));//颜色填充
//给指定的Excel单元格添加富文本批注
range = sheet.getCellRange("F2");
range.getRichText().setFont(0, 8, font);
range.autoFitColumns();
range.getComment().setName("批注2");
range.getComment().setVisible(true);//设置批注隐藏或显示
range.getComment().getRichText().setText("已入库数量");
range.getComment().getRichText().setFont(0, 4, fontGreen);
range.getComment().getRichText().setFont(3, 4, fontBlue);
//保存结果文档
wb.saveToFile("AddComments.xlsx", ExcelVersion.Version2013);
wb.dispose();
}
}
2、批注添加效果:

1、import com.spire.xls.*;
public class DeleteComment {
public static void main(String[] args) {
//加载Excel文档
Workbook wb = new Workbook();
wb.loadFromFile("AddComments.xlsx");
//获取工作表
Worksheet sheet = wb.getWorksheets().get(0);
//获取指定单元格中的批注,并删除
sheet.getRange().get("H2").getComment().remove();
//保存文档
wb.saveToFile("DeleteComment.xlsx", ExcelVersion.Version2013);
wb.dispose();
}
}
2、批注删除结果:

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。