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

📄 writablesheetimpl.java~

📁 jxtl API Java中Excel的生成与导入解析参考文档
💻 JAVA~
📖 第 1 页 / 共 5 页
字号:
              // it may have formatting information, so
              // it must be copied
              Blank b = new Blank(cell);
              addCell(b);
            }
          }
        }
        catch (WriteException e)
        {
          Assert.verify(false);
        }
      }
    }
  }

  /** 
   * Copies the cell contents from the specified sheet into this one.  This
   * differs from the previous method in that it uses deep copies to copy
   * each cell
   * Used for creating duplicates of WritableSheets
   *
   * @param s the sheet to copy
   */
  private void copyCells(WritableSheet s)
  {
    // Copy the cells
    int cells = s.getRows();
    Cell[] row = null;
    Cell cell = null;
    for (int i = 0;  i < cells; i++)
    {
      row = s.getRow(i);

      for (int j = 0; j < row.length; j++)
      {
        cell = row[j];

        try
        {
          WritableCell wc = ( (WritableCell) cell).copyTo(cell.getColumn(), 
                                                          cell.getRow());
          addCell(wc);
        }
        catch (WriteException e)
        {
          Assert.verify(false);
        }
      }
    }
  }

  /**
   * Copies the specified sheet, row by row and cell by cell
   * 
   * @param s the sheet to copy
   */
  void copy(Sheet s)
  {
    // Copy the settings
    settings = new SheetSettings(s.getSettings(), this);

    SheetCopier si = new SheetCopier(s, this);
    si.setColumnFormats(columnFormats);
    si.setFormatRecords(formatRecords);
    si.setHyperlinks(hyperlinks);
    si.setMergedCells(mergedCells);
    si.setRowBreaks(rowBreaks);
    si.setColumnBreaks(columnBreaks);
    si.setSheetWriter(sheetWriter);
    si.setDrawings(drawings);
    si.setImages(images);
    si.setConditionalFormats(conditionalFormats);

    si.copySheet();

    dataValidation = si.getDataValidation();
    comboBox = si.getComboBox();
    plsRecord = si.getPLSRecord();
    chartOnly = si.isChartOnly();
    buttonPropertySet = si.getButtonPropertySet();
    numRows = si.getRows();
    autoFilter = si.getAutoFilter();
    maxRowOutlineLevel = si.getMaxRowOutlineLevel();
    maxColumnOutlineLevel = si.getMaxColumnOutlineLevel();
  }

  /**
   * Copies the specified sheet, row by row and cell by cell
   * 
   * @param s the sheet to copy
   */
  /*
  void copy(WritableSheet s)
  {
    settings = new SheetSettings(s.getSettings(), this);

    copyCells(s);

    // Copy the column formats
    Iterator cfit = ( (WritableSheetImpl) s).columnFormats.iterator();
    while (cfit.hasNext())
    {
      ColumnInfoRecord cv = new ColumnInfoRecord
        ((ColumnInfoRecord) cfit.next());
      columnFormats.add(cv);
    }

    // Copy the merged cells
    Range[] merged = s.getMergedCells();

    for (int i = 0; i < merged.length; i++)
    {
      mergedCells.add(new SheetRangeImpl((SheetRangeImpl)merged[i], this));
    }

    // Copy the row properties
    try
    {
      RowRecord[] copyRows = ( (WritableSheetImpl) s).rows;
      RowRecord row = null;
      for (int i = 0; i < copyRows.length ; i++)
      {
        row = copyRows[i];
        
        if (row != null &&
            (!row.isDefaultHeight() ||
             row.isCollapsed()))
        {
          RowRecord rr = getRowRecord(i);
          rr.setRowDetails(row.getRowHeight(), 
                           row.matchesDefaultFontHeight(),
                           row.isCollapsed(), 
                           row.getStyle());
        }
      }
    }
    catch (RowsExceededException e)
    {
      // Handle the rows exceeded exception - this cannot occur since
      // the sheet we are copying from will have a valid number of rows
      Assert.verify(false);
    }

    // Copy the headers and footers
    WritableSheetImpl si = (WritableSheetImpl) s;

    //    sheetWriter.setHeader(si.getHeader());
    //    sheetWriter.setFooter(si.getFooter());

    // Copy the horizontal page breaks
    rowBreaks = new ArrayList(si.rowBreaks);

    // Copy the vertical page breaks
    columnBreaks = new ArrayList(si.columnBreaks);

    // Copy the data validations
    DataValidation rdv = si.dataValidation;
    if (rdv != null)
    {
      dataValidation = new DataValidation(rdv, 
                                          workbook,
                                          workbook, 
                                          workbookSettings);
    }

    // Copy the charts 
    sheetWriter.setCharts(si.getCharts());

    // Copy the drawings
    DrawingGroupObject[] dr = si.getDrawings();
    for (int i = 0 ; i < dr.length ; i++)
    {
      if (dr[i] instanceof jxl.biff.drawing.Drawing)
      {
        WritableImage wi = new WritableImage(dr[i], 
                                             workbook.getDrawingGroup());
        drawings.add(wi);
        images.add(wi);
      }

      // Not necessary to copy the comments, as they will be handled by
      // the deep copy of the individual cells
    }

    // Copy the workspace options
    sheetWriter.setWorkspaceOptions(si.getWorkspaceOptions());

    // Copy the environment specific print record
    if (si.plsRecord != null)
    {
      plsRecord = new PLSRecord(si.plsRecord);
    }

    // Copy the button property set
    if (si.buttonPropertySet != null)
    {
      buttonPropertySet = new ButtonPropertySetRecord(si.buttonPropertySet);
    }
  }
  */

  /**
   * Gets the header.  Called when copying sheets
   *
   * @return the page header
   */
  final HeaderRecord getHeader()
  {
    return sheetWriter.getHeader();
  }

  /**
   * Gets the footer.  Called when copying sheets
   *
   * @return the page footer
   */
  final FooterRecord getFooter()
  {
    return sheetWriter.getFooter();
  }
  /**
   * Determines whether the sheet is protected
   *
   * @return whether or not the sheet is protected
   * @deprecated Use the SheetSettings bean instead
   */
  public boolean isProtected()
  {
    return settings.isProtected();
  }

  /**
   * Gets the hyperlinks on this sheet
   *
   * @return an array of hyperlinks
   */
  public Hyperlink[] getHyperlinks()
  {
    Hyperlink[] hl = new Hyperlink[hyperlinks.size()];

    for (int i = 0; i < hyperlinks.size(); i++)
    {
      hl[i] = (Hyperlink) hyperlinks.get(i);
    }

    return hl;
  }

  /**
   * Gets the cells which have been merged on this sheet
   *
   * @return an array of range objects
   */
  public Range[] getMergedCells()
  {
    return mergedCells.getMergedCells();
  }

  /**
   * Gets the writable  hyperlinks on this sheet
   *
   * @return an array of hyperlinks
   */
  public WritableHyperlink[] getWritableHyperlinks()
  {
    WritableHyperlink[] hl = new WritableHyperlink[hyperlinks.size()];

    for (int i = 0; i < hyperlinks.size(); i++)
    {
      hl[i] = (WritableHyperlink) hyperlinks.get(i);
    }

    return hl;
  }

  /**
   * Removes the specified hyperlink.  Note that if you merely set the
   * cell contents to be an Empty cell, then the cells containing the 
   * hyperlink will still be active.  The contents of the cell which
   * activate the hyperlink are removed.
   * The hyperlink passed in must be a hyperlink retrieved using the 
   * getHyperlinks method
   *
   * @param h the hyperlink to remove.
   * @param preserveLabel if TRUE preserves the label contents, if FALSE
   * removes them
   */
  public void removeHyperlink(WritableHyperlink h)
  {
    removeHyperlink(h, false);
  }

  /**
   * Removes the specified hyperlink.  Note that if you merely set the
   * cell contents to be an Empty cell, then the cells containing the 
   * hyperlink will still be active.
   * If the preserveLabel field is set, the cell contents of the 
   * hyperlink are preserved, although the hyperlink is deactivated.  If
   * this value is FALSE, the cell contents are removed
   * The hyperlink passed in must be a hyperlink retrieved using the 
   * getHyperlinks method
   *
   * @param h the hyperlink to remove.
   * @param preserveLabel if TRUE preserves the label contents, if FALSE
   * removes them
   */
  public void removeHyperlink(WritableHyperlink h, boolean preserveLabel)
  {
    // Remove the hyperlink
    hyperlinks.remove(hyperlinks.indexOf(h));

    if (!preserveLabel)
    {
      // Set the cell contents for the hyperlink - including any formatting
      // information - to be empty
      Assert.verify(rows.length > h.getRow() && rows[h.getRow()] != null);
      rows[h.getRow()].removeCell(h.getColumn());
    }
  }

  /**
   * Adds the specified hyperlink
   * 
   * @param the hyperlink
   * @exception WriteException
   * @exception RowsExceededException
   */
  public void addHyperlink(WritableHyperlink h) 
    throws WriteException, RowsExceededException
  {
    // First set the label on the sheet
    Cell c = getCell(h.getColumn(), h.getRow());

    String contents = null;
    if (h.isFile() || h.isUNC())
    {
      String cnts = ( (HyperlinkRecord) h).getContents();
      if (cnts == null)
      {
        contents = h.getFile().getPath();
      }
      else
      {
        contents = cnts;
      }
    }
    else if (h.isURL())
    {
      String cnts = ( (HyperlinkRecord) h).getContents();
      if (cnts == null)
      {
        contents = h.getURL().toString();
      }
      else
      {
        contents=cnts;
      }
    }
    else if (h.isLocation())
    {
      contents = ( (HyperlinkRecord) h).getContents();
    }

    // If the cell type is a label, then preserve the cell contents
    // and most of the format (apart from the font)
    // otherwise overwrite the cell content and the format with the contents
    // and the standard hyperlink format
    if (c.getType() == CellType.LABEL)
    {
      Label l = (Label) c;
      l.setString(contents);
      WritableCellFormat wcf = new WritableCellFormat(l.getCellFormat());
      ( (XFRecord) wcf).setFont(WritableWorkbook.HYPERLINK_FONT);
      l.setCellFormat(wcf);
    }
    else
    {
      Label l = new Label(h.getColumn(), h.getRow(), contents, 
                          WritableWorkbook.HYPERLINK_STYLE);
      addCell(l);
    }
    
    // Set all other cells within range to be empty
    for (int i = h.getRow(); i <= h.getLastRow(); i++)
    {
      for (int j = h.getColumn(); j <= h.getLastColumn(); j++)
      {
        if (i != h.getRow() && j != h.getColumn())
        {
          // Set the cell to be empty
          if (rows.length < h.getLastColumn() && rows[i] != null)
          {
            rows[i].removeCell(j);
          }
        }
      }
    }

    ((HyperlinkRecord) h).initialize(this);
    hyperlinks.add(h);
  }

  /**
   * Merges the specified cells.  Any clashes or intersections between 
   * merged cells are resolved when the spreadsheet is written out
   *
   * @param col1 the column number of the top left cell
   * @param row1 the row number of the top left cell
   * @param col2 the column number of the bottom right cell
   * @param row2 the row number of the bottom right cell
   * @return the Range object representing the merged cells
   * @exception jxl.write..WriteException
   * @exception jxl.write.biff.RowsExceededException
   */
  public Range mergeCells(int col1, int row1, int col2, int row2)
    throws WriteException, RowsExceededException
  {
    // First check that the cells make sense
    if (col2 < col1 || row2 < row1)
    {
      logger.warn("Cannot merge cells - top left and bottom right "+
                  "incorrectly specified");
    }

    // Make sure the spreadsheet is up to size
    if (col2 >= numColumns || row2 >= numRows)
    {
      addCell(new Blank(col2, row2));
    }

    SheetRangeImpl range = new SheetRangeImpl(this, col1, row1, col2, row2);
    mergedCells.add(range);

    return range;
  }

  /** 
   * Sets a row grouping
   *
   * @param row1 the first row of the group
   * @param row2 the last row of the group
   * @param collapsed should the group be collapsed?
   * @exception WriteException
   * @exception RowsExceededException
   */
  public void setRowGroup(int row1, int row2, 
                          boolean collapsed) 
    throws WriteException, RowsExceededException 
  {
    if (row2 < row1)
    {
      logger.warn("Cannot merge cells - top and bottom rows incorrectly " + 
                  "specified");
    }

    for (int i = row1; i <= row2; i++) 
    {
      RowRecord row = getRowRecord(i);
      row.incrementOutlineLevel();
      row.setCollapsed(collapsed);
      maxRowOutlineLevel = Math.max(maxRowOutlineLevel, 
                                    row.getOutlineLevel());
    }
  }

  /** 
   * Unsets a row grouping
   *
   * @param row1 the first row to unset
   * @param row2 the last row to unset
   * @exception WriteException
   * @exception RowsExceededException
   */
  public void unsetRowGroup(int row1, int row2) 
    throws WriteException, RowsExceededException 
  {
    if (row2 < row1)
    {
      logger.warn("Cannot merge cells - top and bottom rows incorrectly " +
                  "specified");
    }

    // Make sure the spreadsheet is up to size
    if (row2 >= numRows)
    {
      logger.warn("" + row2 + 
                  " is greater than the sheet bounds");
      row2 = numRows - 1;
    }

    for (int i = row1; i <= row2; i++) 
    {
      rows[i].decrementOutlineLevel();
    }

    // Recalculate the max outline level
    maxRowOutlineLevel = 0;
    for (int i = rows.length; i-- > 0; ) 
    {
      maxRowOutlineLevel = Math.max(maxRowOutlineLevel, 
                                    rows[i].getOutlineLevel());
    }
  }

⌨️ 快捷键说明

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