How can I remove hyperlink in poi 3.8?

别等时光非礼了梦想. 提交于 2020-01-14 12:13:11

问题


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.



来源:https://stackoverflow.com/questions/15699049/how-can-i-remove-hyperlink-in-poi-3-8

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!