获取批注方式有两种,一种是获取所有,一种是获取每一行的批注,具体看情况而定。
//构建文件流。。。
InputStream inputstream = file.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputstream.read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
InputStream userIs = new ByteArrayInputStream(baos.toByteArray());
Workbook wb = WorkbookFactory.create(userIs);
//读取excel表中的sheet, 参数为sheet的索引值(从0开始)
Sheet sheet = wb.getSheetAt(0);
//方法一: 获取所有批注
Map<CellAddress, ? extends Comment> cellComments = sheet.getCellComments();
for (CellAddress cellAddress : cellComments.keySet()) {
Comment cellComment = sheet.getCellComment(cellAddress);
System.out.println("批注是:" + cellComment.getString().toString());
}
//方法二: 获取每一行的批注
for (int i = 1; i <= sheet.getLastRowNum(); i++) { //外循环是循环行,内循环是循环每行的单元格
//根据行索引获取行对象(单元格集合)
row = sheet.getRow(i);
//遍历行的单元格,并解析
for (Cell cell : row) {
if (cell.getCellComment() != null) {
Comment cellComment = cell.getCellComment();
//将批注转为 String
String commentStr = cellComment.getString().toString();
}
}
}