0

While reading excel document using apache poi API, I got following Exception:

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)

It's working fine if I manually remove that hyperlink from excel sheet...so my question is that possible to read excel sheet having hyperlink ?or is there any way to remove that hyperlink using java code itself.. Thanks!

4

1 回答 1

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());
}
于 2013-09-11T10:43:20.177 回答