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

📄 writablesheetimpl.java

📁 java读写Excel文档的API
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    }

    sheetWriter.setWriteData(rows, 
                             rowBreaks, 
                             columnBreaks,
                             hyperlinks, 
                             mergedCells, 
                             columnFormats,
                             maxRowOutlineLevel,
                             maxColumnOutlineLevel);
    sheetWriter.setDimensions(getRows(), getColumns());
    sheetWriter.setSettings(settings);
    sheetWriter.setPLS(plsRecord);
    sheetWriter.setDrawings(drawings, dmod);
    sheetWriter.setButtonPropertySet(buttonPropertySet);
    sheetWriter.setDataValidation(dataValidation, validatedCells);
    sheetWriter.setConditionalFormats(conditionalFormats);
    sheetWriter.setAutoFilter(autoFilter);
    
    sheetWriter.write();
  }

  /**
   * 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);
    WritableSheetImpl si = (WritableSheetImpl) s;

    WritableSheetCopier sc = new WritableSheetCopier(s, this);
    sc.setColumnFormats(si.columnFormats, columnFormats);
    sc.setMergedCells(si.mergedCells, mergedCells);
    sc.setRows(si.rows);
    sc.setRowBreaks(si.rowBreaks, rowBreaks);
    sc.setColumnBreaks(si.columnBreaks, columnBreaks);
    sc.setDataValidation(si.dataValidation);
    sc.setSheetWriter(sheetWriter);
    sc.setDrawings(si.drawings, drawings, images);
    sc.setWorkspaceOptions(si.getWorkspaceOptions());
    sc.setPLSRecord(si.plsRecord);
    sc.setButtonPropertySetRecord(si.buttonPropertySet);
    sc.setHyperlinks(si.hyperlinks, hyperlinks);

    sc.copySheet();

    dataValidation = sc.getDataValidation();
    plsRecord = sc.getPLSRecord();
    buttonPropertySet = sc.getButtonPropertySet();
  }

  /**
   * 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);
      numRows = Math.max(i+1, numRows);
      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());
    }
  }

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

    for (int i = col1; i <= col2; i++) 
    {
      ColumnInfoRecord cir = getColumnInfo(i);

      // Create the column info record if not present using a default
      // cell view
      if (cir == null)
      {
        setColumnView(i, new CellView());
        cir = getColumnInfo(i);
      }

      cir.incrementOutlineLevel();
      cir.setCollapsed(collapsed);
      maxColumnOutlineLevel = Math.max(maxColumnOutlineLevel, 
                                       cir.getOutlineLevel());
    }
  }

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

    for (int i = col1; i <= col2; i++) 
    {
      ColumnInfoRecord cir = getColumnInfo(i);
      cir.decrementOutlineLevel();
    }
    
    // Recalculate the max outline level
    maxColumnOutlineLevel = 0;
    for (Iterator it = columnFormats.iterator(); it.hasNext(); ) 
    {
      ColumnInfoRecord cir = (ColumnInfoRecord)it.next();
      maxColumnOutlineLevel = Math.max(maxColumnOutlineLevel, 
                                       cir.getOutlineLevel());
    }
  }

  /**
   * Unmerges the specified cells.  The Range passed in should be one that
   * has been previously returned as a result of the getMergedCells method
   *
   * @param r the range of cells to unmerge
   */
  public void unmergeCells(Range r)
  {
    mergedCells.unmergeCells(r);
  }

  /**
   * Sets the header for this page
   *
   * @param l the print header to print on the left side
   * @param c the print header to print in the centre
   * @param r the print header to print on the right hand side
   * @deprecated Use the sheet settings bean
   */
  public void setHeader(String l, String c, String r)
  {
    HeaderFooter header = new HeaderFooter();
    header.getLeft().append(l);
    header.getCentre().append(c);
    header.getRight().append(r);
    settings.setHeader(header);
  }

  /**
   * Sets the footer for this page
   *
   * @param l the print header to print on the left side
   * @param c the print header to print in the centre
   * @param r the print header to print on the right hand side
   * @deprecated Use the sheet settings bean
   */

⌨️ 快捷键说明

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