下面是部分代码,address的值是 [2024]报告.docx
private static void setCellHyperlink(Workbook wb, HyperlinkType hyperlinkType, String address, Cell cell, CellStyle cellStyle) {
CreationHelper creationHelper = wb.getCreationHelper();
Hyperlink hyperlink = creationHelper.createHyperlink(hyperlinkType);
if(hyperlinkType==HyperlinkType.FILE){
address=replaceLinkCharStr(address);
}
hyperlink.setAddress(address);
cell.setHyperlink(hyperlink);
cell.setCellStyle(cellStyle);
}
调试了一下,原因是找到了,在源码里new URI(address);抛了个异常,我也尝试通过反射绕过validate方法的验证,直接设置_location的值,setAddress不报错了,但是将文件写到本地时又报错了。字符转义后也会报错
#poi源码
public void setAddress(String address) {
validate(address);//验证地址,就是这个方法内报错,验证没通过抛了个异常出来
_location = address;
//we must set location for internal hyperlinks
if (_type == HyperlinkType.DOCUMENT) {
setLocation(address);
}
}
#验证地址
private void validate(String address) {
switch (_type) {
// email, path to file and url must be valid URIs
case EMAIL:
case FILE:
case URL:
try {
new URI(address);
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Address of hyperlink must be a valid URI", e);
}
break;
case DOCUMENT:
// currently not evaluating anything.
break;
default:
throw new IllegalStateException("Invalid Hyperlink type: " + _type);
}
}