在使用apache poi API阅读excel文档时,我遇到了以下异常:
Exception in thread "main" java.lang.IllegalStateException: A sheet hyperlink must either have a location, or a relationship. Found:
<xml-fragment ref="C1271" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"/>
at org.apache.poi.xssf.usermodel.XSSFHyperlink.<init>(XSSFHyperlink.java:72)
at org.apache.poi.xssf.usermodel.XSSFSheet.initHyperlinks(XSSFSheet.java:182)
at org.apache.poi.xssf.usermodel.XSSFSheet.read(XSSFSheet.java:139)
at org.apache.poi.xssf.usermodel.XSSFSheet.onDocumentRead(XSSFSheet.java:119)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:222)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:200)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:172)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:63)
at com.gepower.mdcatalog.test.Test.main(Test.java:34)
如果我从excel表中手动删除那个超链接,它工作正常......所以我的问题是可以读取带有超链接的excel表吗?或者有没有办法使用java代码本身删除该超链接.. 谢谢!
答案 0 :(得分:0)
加载并获取该单元格的getHyperlink
所需的任何单元格的超链接地址。您可以使用以下内容:
Workbook wb = WorkbookFactory.create(new File(FilePath));
Sheet TestSheet = wb.getSheetAt(0);
Cell cell = TestSheet.getRow(0).getCell(0);
Hyperlink linkAddress = cell.getHyperlink();
if(linkAddress != null){
System.out.println(linkAddress .getAddress());
}