使用 poi 从 excel 读取超链接时获取空



我有一个 excel 文件,其中一行包含很少的单元格作为字符串,其他作为数字、超链接。

我想从 excel 中读取我在下面代码编写的数据

HSSFCell cell =row.getCell(j+1);    
cell.setCellType(CellType.STRING);
String cellValue = cell.getStringCellValue();

上面的代码读取数字单元格和字符串单元格,但是当涉及到包含超链接的单元格时,在这种情况下,它会将这些单元格读取为null。我可以在工作表中的双引号("abc@cd.com"(之间放置我的超链接,但我想在代码级别处理这个问题。有什么办法可以处理这种情况吗?

您应该使用 cell.getHyperlink(( 从单元格获取超链接。

if(cell.getCellTypeEnum() == CellType.STRING){
Hyperlink hyperlink = cell.getHyperlink();
String value = cell.getRichStringCellValue().getString();
if(hyperlink == null) {
return value;
} else {
return value + " " + hyperlink.getAddress(); 
}
}

最新更新