How can I remove hyperlink in poi 3.8?

2019-06-27 09:34发布

问题:

I have some problem with reading an excel file contains hyperlink text in poi.

Data is like that(excel file):
|1| type | category | job_type | position | name | email
|2| Test | developer | parttime | manager | hong | asdf##@dsaf.com(hyperlink)
|3| Test | developer | parttime | manager | asde | test@mail.com(hyperlink)
|4| Test | developer | parttime | manager | asde | aaaaaaa(non-hyperlink)

To create workbook object, I use WorkbookFactory.create(InputStream inp) method.
Codes are here:

public POIExcelImport(String name, InputStream inputStream) throws ExcelImportException {
    super(name, null);
    try {

        logger.debug("before work : {}", this.workbook);

        this.workbook = WorkbookFactory.create(inputStream);// exception 

    } catch (InvalidFormatException e) {
        throw new ExcelImportException(e);
    } catch (IOException e) {
        throw new ExcelImportException(e);
    } 

    if(XSSFWorkbook.class.isAssignableFrom(workbook.getClass()))
        this.type = ExcelFileType.XLSX;
    else
        this.type = ExcelFileType.XLS;
}

When I call create method, Exception was thrown.

java.lang.IllegalStateException: The hyperlink for cell F2 references relation rId1, but that didn't exist!
at org.apache.poi.xssf.usermodel.XSSFHyperlink.<init>(XSSFHyperlink.java:71) ~[poi-ooxml-3.8.jar:3.8]
at org.apache.poi.xssf.usermodel.XSSFSheet.initHyperlinks(XSSFSheet.java:204) ~[poi-ooxml-3.8.jar:3.8]
at org.apache.poi.xssf.usermodel.XSSFSheet.read(XSSFSheet.java:157) ~[poi-ooxml-3.8.jar:3.8]
at org.apache.poi.xssf.usermodel.XSSFSheet.onDocumentRead(XSSFSheet.java:129) ~[poi-ooxml-3.8.jar:3.8]
at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:269) ~[poi-ooxml-3.8.jar:3.8]
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159) ~[poi-ooxml-3.8.jar:3.8]
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:183) ~[poi-ooxml-3.8.jar:3.8]
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:73) ~[poi-ooxml-3.8.jar:3.8]
at dreaminfra.ipams.common.excel.poi.POIExcelImport.<init>(POIExcelImport.java:49) ~[ipams-core-1.0.0.jar:na]

I want to remove hyperlink, but there are only issues about to create hyperlink.
I don't have idea, is there any idea?

回答1:

I found this to work on my system:

ICell oCell = workbook.GetSheetAt(0).GetRow(0).GetCell(0);
IHyperlink oLink = oCell.Hyperlink;
oLink.setFirstRow(0);
oLink.setLastRow(0);
oLink.setFirstColumn(0);
oLink.setLastColumn(0);
oLink.setLabel(null);
oLink.setAddress("");

Note: I'm primarily working with NPOI (C# version) not POI (Java version), but it seems to work the same. Also, be sure to keep the .setAddress() last.

EDIT: Unfortunately this seems to cause documents that cannot be saved by Excel for some reason. (excel detects corruption on save) So it doesn't actually work in practice, looks like someone will have to patch the NPOI repository for this one.



标签: apache-poi