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

📄 hyperlinkrecord.java

📁 A Java API to read, write and modify Excel spreadsheets
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*********************************************************************
*
*      Copyright (C) 2002 Andrew Khan
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/

package jxl.write.biff;

import java.io.File;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.ArrayList;

import common.Assert;
import common.Logger;

import jxl.CellType;
import jxl.Hyperlink;
import jxl.Range;
import jxl.WorkbookSettings;
import jxl.biff.CellReferenceHelper;
import jxl.biff.IntegerHelper;
import jxl.biff.SheetRangeImpl;
import jxl.biff.StringHelper;
import jxl.biff.Type;
import jxl.biff.WritableRecordData;
import jxl.write.Label;
import jxl.write.WritableHyperlink;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;


/**
 * A hyperlink
 */
public class HyperlinkRecord extends WritableRecordData
{
  /**
   * The logger
   */
  private static Logger logger = Logger.getLogger(HyperlinkRecord.class);

  /**
   * The first row
   */
  private int firstRow;
  /**
   * The last row
   */
  private int lastRow;
  /**
   * The first column
   */
  private int firstColumn;
  /**
   * The last column
   */
  private int lastColumn;

  /**
   * The URL referred to by this hyperlink
   */
  private URL url;

  /**
   * The local file referred to by this hyperlink
   */
  private File file;

  /**
   * The location in this workbook referred to by this hyperlink
   */
  private String location;

  /**
   * The cell contents of the cell which activate this hyperlink
   */
  private String contents;

  /**
   * The type of this hyperlink
   */
  private LinkType linkType;

  /**
   * The data for this hyperlink
   */
  private byte[] data;

  /**
   * The range of this hyperlink.  When creating a hyperlink, this will
   * be null until the hyperlink is added to the sheet
   */
  private Range range;

  /**
   * The sheet containing this hyperlink
   */
  private WritableSheet sheet;

  /**
   * Indicates whether this record has been modified since it was copied
   */
  private boolean modified;

  /**
   * The excel type of hyperlink
   */
  private static class LinkType {};

  private static final LinkType urlLink      = new LinkType();
  private static final LinkType fileLink     = new LinkType();
  private static final LinkType uncLink      = new LinkType();
  private static final LinkType workbookLink = new LinkType();
  private static final LinkType unknown      = new LinkType();

  /**
   * Constructs this object from the readable spreadsheet
   *
   * @param hl the hyperlink from the read spreadsheet
   */
  protected HyperlinkRecord(Hyperlink h, WritableSheet s)
  {
    super(Type.HLINK);

    if (h instanceof jxl.read.biff.HyperlinkRecord)
    {
      copyReadHyperlink(h, s);
    }
    else
    {
      copyWritableHyperlink(h, s);
    }
  }

  /**
   * Copies a hyperlink read in from a read only sheet
   */
  private void copyReadHyperlink(Hyperlink h, WritableSheet s)
  {
    jxl.read.biff.HyperlinkRecord hl = (jxl.read.biff.HyperlinkRecord) h;

    data = hl.getRecord().getData();
    sheet = s;

    // Populate this hyperlink with the copied data
    firstRow    = hl.getRow();
    firstColumn = hl.getColumn();
    lastRow     = hl.getLastRow();
    lastColumn  = hl.getLastColumn();
    range       = new SheetRangeImpl(s, 
                                     firstColumn, firstRow,
                                     lastColumn, lastRow);

    linkType = unknown;

    if (hl.isFile())
    {
      linkType = fileLink;
      file = hl.getFile();
    }
    else if (hl.isURL())
    {
      linkType = urlLink;
      url = hl.getURL();
    }
    else if (hl.isLocation())
    {
      linkType = workbookLink;
      location = hl.getLocation();
    }

    modified = false;
  }

  /**
   * Copies a hyperlink read in from a writable sheet.
   * Used when copying writable sheets
   *
   * @param hl the hyperlink from the read spreadsheet
   */
  private void copyWritableHyperlink(Hyperlink hl, WritableSheet s)
  {
    HyperlinkRecord h = (HyperlinkRecord) hl;

    firstRow = h.firstRow;
    lastRow = h.lastRow;
    firstColumn = h.firstColumn;
    lastColumn = h.lastColumn;

    if (h.url != null)
    {
      try
      {
        url = new URL(h.url.toString());
      }
      catch (MalformedURLException e)
      {
        // should never get a malformed url as a result url.toString()
        Assert.verify(false);
      }
    }
    
    if (h.file != null)
    {
      file = new File(h.file.getPath());
    }

    location = h.location;
    contents = h.contents;
    linkType = h.linkType;
    modified = true;

    sheet = s;
    range = new SheetRangeImpl(s, 
                               firstColumn, firstRow,
                               lastColumn, lastRow);
  }

  /**
   * Constructs a URL hyperlink to a range of cells
   *
   * @param col the column containing this hyperlink
   * @param row the row containing this hyperlink
   * @param lastcol the last column which activates this hyperlink
   * @param lastrow the last row which activates this hyperlink
   * @param url the hyperlink
   * @param desc the description
   */
  protected HyperlinkRecord(int col, int row, 
                            int lastcol, int lastrow, 
                            URL url,
                            String desc)
  {
    super(Type.HLINK);

    firstColumn = col;
    firstRow = row;

    lastColumn = Math.max(firstColumn, lastcol);
    lastRow = Math.max(firstRow, lastrow);
    
    this.url = url;
    contents = desc;

    linkType = urlLink;

    modified = true;
  }

  /**
   * Constructs a File hyperlink to a range of cells
   *
   * @param col the column containing this hyperlink
   * @param row the row containing this hyperlink
   * @param lastcol the last column which activates this hyperlink
   * @param lastrow the last row which activates this hyperlink
   * @param file the hyperlink
   * @param desc the description
   */
  protected HyperlinkRecord(int col, int row, int lastcol, int lastrow, 
                            File file, String desc)
  {
    super(Type.HLINK);

    firstColumn = col;
    firstRow = row;

    lastColumn = Math.max(firstColumn, lastcol);
    lastRow = Math.max(firstRow, lastrow);
    contents = desc;

    this.file = file;

    if (file.getPath().startsWith("\\\\"))
    {
      linkType = uncLink;
    }
    else
    {
      linkType = fileLink;
    }

    modified = true;
  }

  /**
   * Constructs a hyperlink to some cells within this workbook
   *
   * @param col the column containing this hyperlink
   * @param row the row containing this hyperlink
   * @param lastcol the last column which activates this hyperlink
   * @param lastrow the last row which activates this hyperlink
   * @param desc the contents of the cell which describe this hyperlink
   * @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 HyperlinkRecord(int col, int row,
                            int lastcol, int lastrow, 
                            String desc,
                            WritableSheet s,
                            int destcol, int destrow,
                            int lastdestcol, int lastdestrow)
  {
    super(Type.HLINK);

    firstColumn = col;
    firstRow = row;

    lastColumn = Math.max(firstColumn, lastcol);
    lastRow = Math.max(firstRow, lastrow);
    
    setLocation(s, destcol, destrow, lastdestcol, lastdestrow);
    contents = desc;

    linkType = workbookLink;

    modified = true;
  }

  /**
   * Determines whether this is a hyperlink to a file
   * 
   * @return TRUE if this is a hyperlink to a file, FALSE otherwise
   */
  public boolean isFile()
  {
    return linkType == fileLink;
  }

  /**
   * Determines whether this is a hyperlink to a UNC
   * 
   * @return TRUE if this is a hyperlink to a UNC, FALSE otherwise
   */
  public boolean isUNC()
  {
    return linkType == uncLink;
  }

  /**
   * Determines whether this is a hyperlink to a web resource
   *
   * @return TRUE if this is a URL
   */
  public boolean isURL()
  {
    return linkType == urlLink;
  }

  /**
   * Determines whether this is a hyperlink to a location in this workbook
   *
   * @return TRUE if this is a link to an internal location
   */
  public boolean isLocation()
  {
    return linkType == workbookLink;
  }

  /**
   * Returns the row number of the top left cell
   * 
   * @return the row number of this cell
   */
  public int getRow()
  {
    return firstRow;
  }

  /**
   * Returns the column number of the top left cell
   * 
   * @return the column number of this cell
   */
  public int getColumn()
  {
    return firstColumn;
  }

  /**
   * Returns the row number of the bottom right cell
   * 
   * @return the row number of this cell
   */
  public int getLastRow()
  {
    return lastRow;
  }

  /**
   * Returns the column number of the bottom right cell
   * 
   * @return the column number of this cell
   */
  public int getLastColumn()
  {
    return lastColumn;
  }

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

⌨️ 快捷键说明

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