删除excel中批注信息。
删除批注信息是通过cell的removeComment()方法。值得注意的是:当调用完removeComment()方法时,记得一定要对操作过的excel进行保存。
以下是实例:
// 读取文件
FileInputStream fileInputStream =new FileInputStream("D:\\12.xls");
// 创建工作簿
Workbook workbook = new HSSFWorkbook(fileInputStream);
// 获取当前工作簿sheet数量
int numberSheet = workbook.getNumberOfSheets();
for (int i = 0; i < numberSheet; i++) {
//根据sheet number 获取一个sheet对象
Sheet sheet = workbook.getSheetAt(i);
//获取一个sheet里的物理row number(1——...)
int numberRow = sheet.getPhysicalNumberOfRows();
System.out.println(numberRow);
for (Row row : sheet) {
//获取一个sheet里的物理row number(1——...)
int cellNumber=row.getPhysicalNumberOfCells();
System.out.println(cellNumber);
for (Cell cell : row) {
//获取批注
Comment comment = cell.getCellComment();
if (comment!=null) {
//获取批注作者
System.out.println("comment another :"+ comment.getAuthor());
//获取批注内容
System.out.println("comment context :"+comment.getString().getString());
//获取批注信息是否可见
System.out.println("comment is visible :"+comment.isVisible());
}
//删除批注
cell.removeCellComment();
}
}
}
//因为上文有做删除excel中的批注,必须要做一次保存才可以删除批注。
FileOutputStream fileOutputStream=new FileOutputStream("D:\\12.xls");
//写一个excel文件
workbook.write(fileOutputStream);
fileOutputStream.close();
fileInputStream.close();