Java POI-get.hyperlink()未检索单元格的最新超链接

Java POI-get.hyperlink()未检索单元格的最新超链接,java,excel,apache-poi,Java,Excel,Apache Poi,在这里,我在同一单元格中用第二个超链接覆盖第一个超链接,并且get.hyperlink()方法总是提取第一个写入单元格的超链接 这方面的任何帮助都将是巨大的!!提前谢谢 下面是我的代码和输出: public static void main(String[] args) { ExcelUtils2.writeHyperLink("DashboardCopy", 1, 1, "test1", "https://google.com"); ExcelUtils2.writeHyper

在这里,我在同一单元格中用第二个超链接覆盖第一个超链接,并且
get.hyperlink()
方法总是提取第一个写入单元格的超链接

这方面的任何帮助都将是巨大的!!提前谢谢

下面是我的代码和输出:

public static void main(String[] args) {
    ExcelUtils2.writeHyperLink("DashboardCopy", 1, 1, "test1", "https://google.com");
    ExcelUtils2.writeHyperLink("DashboardCopy", 1, 1, "test2", "https://facebook.com");
    String dtr = ExcelUtils2.getCellHyperlink(1, 1);
    System.out.println("Hyperlink of the cell is >> " + dtr);
}


public static synchronized void writeHyperLink(String workBookName, int irow, int icol, String ivalue, String iUrl)
        throws InvalidFormatException, IOException {
    FileInputStream excelFile = new FileInputStream(excelSource);
    excelWBook = new XSSFWorkbook(excelFile);
    excelWSheet = excelWBook.getSheet(workBookName);
    createHelper = excelWBook.getCreationHelper();
    XSSFRow row = excelWSheet.getRow(irow);
    XSSFCell cell = row.createCell(icol);
    cell.setCellValue(ivalue);
    XSSFHyperlink link = (XSSFHyperlink) createHelper.createHyperlink(HyperlinkType.URL);
    link.setAddress(iUrl);
    cell.setHyperlink(link);
    FileOutputStream fos = new FileOutputStream(excelSource);
    excelWBook.write(fos);
    fos.close();

}

public static String getCellHyperlink(int rowNum, int colNum) throws Exception {
    FileInputStream excelFile = new FileInputStream(excelSource);
    excelWBook = new XSSFWorkbook(excelFile);
    excelWSheet = excelWBook.getSheet("DashboardCopy");
    XSSFRow row = excelWSheet.getRow(rowNum);
    XSSFCell cell = row.getCell(colNum);
    XSSFHyperlink link = cell.getHyperlink();
    String cellData = link.getAddress();
    // System.out.println(link.getAddress());
    return cellData;
}
输出: 单元格的超链接为>>

预期产出:
它应该打印

在设置超链接之前,我会尝试删除超链接

 cell.removeHyperlink();
 cell.setHyperlink(link);

在writeHyperLink的末尾执行excelWBook.close()怎么样?不过请注意,如果我们在writeHyperLink方法中使用link.getAddress()在单元格中写入链接后,在writeHyperLink方法中获取地址,它会正确地写入“facebook.com”。只有在使用getHyperlink()方法时才会出现问题。它与removeHyperlink()方法一起工作-在写入新的超链接之前使用它