⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hyperlinkrecord.java

📁 java操作Excel表格的api
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
  }

  /**
   * Gets the URL referenced by this Hyperlink
   *
   * @return the URL, or NULL if this hyperlink is not a URL
   */
  public URL getURL()
  {
    return url;
  }

  /**
   * Returns the local file eferenced by this Hyperlink
   *
   * @return the file, or NULL if this hyperlink is not a file
   */
  public File getFile()
  {
    return file;
  }

  /**
   * Gets the binary data to be written to the output file
   * 
   * @return the data to write to file
   */
  public byte[] getData()
  {
    if (!modified)
    {
      return data;
    }

    // Build up the common data
    byte[] commonData = new byte[32];

    // Set the range of cells this hyperlink applies to
    IntegerHelper.getTwoBytes(firstRow, commonData, 0);
    IntegerHelper.getTwoBytes(lastRow, commonData, 2);
    IntegerHelper.getTwoBytes(firstColumn, commonData, 4);
    IntegerHelper.getTwoBytes(lastColumn, commonData, 6);

    // Some inexplicable byte sequence
    commonData[8]  = (byte) 0xd0;
    commonData[9]  = (byte) 0xc9;
    commonData[10] = (byte) 0xea;
    commonData[11] = (byte) 0x79;
    commonData[12] = (byte) 0xf9;
    commonData[13] = (byte) 0xba;
    commonData[14] = (byte) 0xce;
    commonData[15] = (byte) 0x11;
    commonData[16] = (byte) 0x8c;
    commonData[17] = (byte) 0x82;
    commonData[18] = (byte) 0x0;
    commonData[19] = (byte) 0xaa;
    commonData[20] = (byte) 0x0;
    commonData[21] = (byte) 0x4b;
    commonData[22] = (byte) 0xa9;
    commonData[23] = (byte) 0x0b;
    commonData[24] = (byte) 0x2;
    commonData[25] = (byte) 0x0;
    commonData[26] = (byte) 0x0;
    commonData[27] = (byte) 0x0;

    // Set up the option flags to indicate the type of this URL.  There
    // is no description
    int optionFlags = 0;
    if (isURL())
    {
      optionFlags = 3;
      
      if (contents != null)
      {
        optionFlags |= 0x14;
      }
    }
    else if (isFile())
    {
      optionFlags = 3;

      if (contents == null)
      {
        optionFlags |= 0x14;
      }
    }
    else if (isLocation())
    {
      optionFlags = 8;
    }
    else if (isUNC())
    {
      optionFlags = 259;
    }

    IntegerHelper.getFourBytes(optionFlags, commonData, 28);

    if (isURL())
    {
      data = getURLData(commonData);
    }
    else if (isFile())
    {
      data = getFileData(commonData);
    }
    else if (isLocation())
    {
      data = getLocationData(commonData);
    }
    else if (isUNC())
    {
      data = getUNCData(commonData);
    }

    return data;
  }

  /**
   * A standard toString method
   * 
   * @return the contents of this object as a string
   */
  public String toString()
  {
    if (isFile())
    {
      return file.toString();
    }
    else if (isURL())
    {
      return url.toString();
    }
    else if (isUNC())
    {
      return file.toString();
    }
    else
    {
      return "";
    }
  }

  /**
   * Gets the range of cells which activate this hyperlink
   * The get sheet index methods will all return -1, because the
   * cells will all be present on the same sheet
   *
   * @return the range of cells which activate the hyperlink or NULL
   * if this hyperlink has not been added to the sheet
   */
  public Range getRange()
  {
    return range;
  }

  /**
   * Sets the URL of this hyperlink
   *
   * @param url the url
   */
  public void setURL(URL url)
  {
    URL prevurl = this.url;
    linkType = urlLink;
    file = null;
    location = null;
    contents = null;
    this.url = url;
    modified = true;

    if (sheet == null)
    {
      // hyperlink has not been added to the sheet yet, so simply return
      return;
    }

    // Change the label on the sheet if it was a string representation of the 
    // URL
    WritableCell wc = sheet.getWritableCell(firstColumn, firstRow);
    
    if (wc.getType() == CellType.LABEL)
    {
      Label l = (Label) wc;
      String prevurlString = prevurl.toString();
      String prevurlString2 = "";
      if (prevurlString.charAt(prevurlString.length() - 1) == '/' ||
          prevurlString.charAt(prevurlString.length() - 1) == '\\')
      {
        prevurlString2 = prevurlString.substring(0, 
                                                 prevurlString.length() - 1);
      }

      if (l.getString().equals(prevurlString) ||
          l.getString().equals(prevurlString2))
      {
        l.setString(url.toString());
      }
    }   
  }

  /**
   * Sets the file activated by this hyperlink
   * 
   * @param file the file
   */
  public void setFile(File file)
  {
    linkType = fileLink;
    url = null;
    location = null;
    contents = null;
    this.file = file;
    modified = true;

    if (sheet == null)
    {
      // hyperlink has not been added to the sheet yet, so simply return
      return;
    }

    // Change the label on the sheet
    WritableCell wc = sheet.getWritableCell(firstColumn, firstRow);
    
    Assert.verify(wc.getType() == CellType.LABEL);

    Label l = (Label) wc;
    l.setString(file.toString());
  }

  /**
   * Sets the location of the cells to be linked to within this workbook
   *
   * @param desc the label describing the link
   * @param sheet the sheet containing the cells to be linked to
   * @param destcol the column number of the first destination linked cell
   * @param destrow the row number of the first destination linked cell
   * @param lastdestcol the column number of the last destination linked cell
   * @param lastdestrow the row number of the last destination linked cell
   */
  protected void setLocation(String desc,
                             WritableSheet sheet,
                             int destcol, int destrow,
                             int lastdestcol, int lastdestrow)
  {
    linkType = workbookLink;
    url = null;
    file = null;
    modified = true;
    contents = desc;

    setLocation(sheet, destcol, destrow, lastdestcol, lastdestrow);

    if (sheet == null)
    {
      // hyperlink has not been added to the sheet yet, so simply return
      return;
    }

    // Change the label on the sheet
    WritableCell wc = sheet.getWritableCell(firstColumn, firstRow);
    
    Assert.verify(wc.getType() == CellType.LABEL);

    Label l = (Label) wc;
    l.setString(desc);
  }

  /**
    * Initializes the location from the data passed in
   *
   * @param sheet the sheet containing the cells to be linked to
   * @param destcol the column number of the first destination linked cell
   * @param destrow the row number of the first destination linked cell
   * @param lastdestcol the column number of the last destination linked cell
   * @param lastdestrow the row number of the last destination linked cell
   */
  private void setLocation(WritableSheet sheet,
                           int destcol, int destrow,
                           int lastdestcol, int lastdestrow)
  {
    StringBuffer sb = new StringBuffer();
    sb.append('\'');
    
    if (sheet.getName().indexOf('\'') == -1)
    {
      sb.append(sheet.getName());
    }
    else 
    {
      // sb.append(sheet.getName().replaceAll("'", "''"));

      // Can't use replaceAll as it is only 1.4 compatible, so have to
      // do this the tedious way
      String sheetName = sheet.getName();
      int pos = 0 ;
      int nextPos = sheetName.indexOf('\'', pos);

      while (nextPos != -1 && pos < sheetName.length())
      {
        sb.append(sheetName.substring(pos, nextPos));
        sb.append("''");
        pos = nextPos + 1;
        nextPos = sheetName.indexOf('\'', pos);
      }
      sb.append(sheetName.substring(pos));
    }

    sb.append('\'');    
    sb.append('!');
    
    lastdestcol = Math.max(destcol, lastdestcol);
    lastdestrow = Math.max(destrow, lastdestrow);

    CellReferenceHelper.getCellReference(destcol, destrow, sb);
    sb.append(':');
    CellReferenceHelper.getCellReference(lastdestcol, lastdestrow, sb);

    location = sb.toString();
  }

  /** 
   * A row has been inserted, so adjust the range objects accordingly
   *
   * @param r the row which has been inserted
   */
  void insertRow(int r)
  {
    // This will not be called unless the hyperlink has been added to the
    // sheet
    Assert.verify(sheet != null && range != null);

    if (r > lastRow)
    {
      return;
    }

    if (r <= firstRow)
    {
      firstRow++;
      modified = true;
    }

    if (r <= lastRow)
    {
      lastRow++;
      modified = true;
    }

    if (modified)
    {
      range  = new SheetRangeImpl(sheet, 
                                  firstColumn, firstRow,
                                  lastColumn, lastRow);
    }
  }

  /** 
   * A column has been inserted, so adjust the range objects accordingly
   *
   * @param c the column which has been inserted
   */
  void insertColumn(int c)
  {
    // This will not be called unless the hyperlink has been added to the
    // sheet
    Assert.verify(sheet != null && range != null);

    if (c > lastColumn)
    {
      return;
    }

    if (c <= firstColumn)
    {
      firstColumn++;
      modified = true;
    }

    if (c <= lastColumn)
    {
      lastColumn++;
      modified = true;
    }

    if (modified)
    {
      range  = new SheetRangeImpl(sheet, 
                                  firstColumn, firstRow,
                                  lastColumn, lastRow);
    }
  }

  /** 
   * A row has been removed, so adjust the range objects accordingly
   *
   * @param r the row which has been inserted
   */
  void removeRow(int r)
  {
    // This will not be called unless the hyperlink has been added to the
    // sheet
    Assert.verify(sheet != null && range != null);

    if (r > lastRow)
    {
      return;
    }

    if (r < firstRow)
    {
      firstRow--;
      modified = true;
    }

    if (r < lastRow)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -