开发者

How to handle HYPERLINK function using POI SS?

开发者 https://www.devze.com 2023-03-08 12:33 出处:网络
I\'m trying to use POI SS to convert an Excel spreadsheet to CSV.Unfortunately I can\'t seem to handle HYPERLINK functions properly:

I'm trying to use POI SS to convert an Excel spreadsheet to CSV. Unfortunately I can't seem to handle HYPERLINK functions properly:

if (cell.getCellType() == Cell.CELL_TYPE_FORMUL开发者_运维知识库A) {
  System.out.println("formula cell: " + cell.toString());
  switch (cell.getCachedFormulaResultType()) {
    case Cell.CELL_TYPE_BOOLEAN:
      return Boolean.toString(cell.getBooleanCellValue());
    case Cell.CELL_TYPE_NUMERIC:
      return NUMERIC_FORMAT.format(cell.getNumericCellValue());
    case Cell.CELL_TYPE_BLANK:
      return "";
    default:
      // Special case handling for HYPERLINK cells
      if (cell.getHyperlink() != null) {
        System.out.println("found hyperlink: " + cell.getHyperlink().getAddress());
        return cell.getHyperlink().getAddress();
      }
      System.out.println("something else: " + cell.toString());
      return cell.toString();
  }
}

Unfortunately, even though it interprets this cell as being a FORMULA cell, cell.getHyperlink() returns null so cell.toString() just returns the full formula like:

=HYPERLINK("http:...","...")

How do I get this to work correctly?


A formula of =HYPERLINK("http://something/","label") is not the same thing as a normal hyperlink in Excel.

If you call get.getHyperlink(), then it'll return the hyperlink (if set) for the cell. That's the hyperlink you get from something like Insert -> Hyperlink

For the formula case, just get the formula string back, and if it's a hyperlink formula grab the bit of interest from it.

(Even though they might look quite similar in Excel, in the file format they're very very different, which is what you're seeing)

0

精彩评论

暂无评论...
验证码 换一张
取 消