如何删除 poi 3.8 中的超链接? [英] How can I remove hyperlink in poi 3.8?

查看:72
本文介绍了如何删除 poi 3.8 中的超链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读取 poi 中包含超链接文本的 excel 文件时遇到了一些问题.

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

数据就是这样(excel文件):
|1|类型 |类别 |job_type |位置 |姓名 |电子邮件
|2|测试 |开发商|兼职|经理 |洪 |asdf##@dsaf.com(超链接)
|3|测试 |开发商|兼职|经理 |阿德|test@mail.com(超链接)
|4|测试 |开发商|兼职|经理 |阿德|aaaaaaa(非超链接)

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)

要创建工作簿对象,我使用 WorkbookFactory.create(InputStream inp) 方法.
代码在这里:

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;
}

当我调用 create 方法时,抛出异常.

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?

推荐答案

我发现这适用于我的系统:

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("");

注意:我主要使用 NPOI(C# 版本)而不是 POI(Java 版本),但它似乎工作相同.另外,请确保将 .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.

不幸的是,这似乎会导致 Excel 由于某种原因无法保存文档.(excel 在保存时检测到损坏)所以它实际上并没有在实践中工作,看起来有人必须为此修补 NPOI 存储库.

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.

这篇关于如何删除 poi 3.8 中的超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆