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

📄 hyperlinkrecord.java~

📁 jxtl API Java中Excel的生成与导入解析参考文档
💻 JAVA~
📖 第 1 页 / 共 2 页
字号:
/*********************************************************************
*
*      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.util.ArrayList;

import common.Assert;
import common.Logger;

import jxl.CellType;
import jxl.Hyperlink;
import jxl.Range;
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.WritableCell;
import jxl.write.WritableSheet;


/**
 * A number record.  This is stored as 8 bytes, as opposed to the 
 * 4 byte RK record
 */
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);

    Assert.verify(h instanceof jxl.read.biff.HyperlinkRecord);

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

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

  /**
   * 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))
      {
        logger.debug("equal");
        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

⌨️ 快捷键说明

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