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

📄 writablesheetimpl.java

📁 一个非常有用的操作MCRSOFT EXCEL文件的工具。可以用JAVA方便的新建
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
      dataValidation.insertRow(row);
    }

    // Adjust any merged cells
    mergedCells.insertRow(row);

    // Adjust any page breaks
    ArrayList newRowBreaks = new ArrayList();
    Iterator ri = rowBreaks.iterator();
    while (ri.hasNext())
    {
      int val = ( (Integer) ri.next()).intValue();
      if (val >= row)
      {
        val++;
      }

      newRowBreaks.add(new Integer(val));
    }
    rowBreaks = newRowBreaks;

    // Handle interested cell references on the main workbook
    if (workbookSettings.getFormulaAdjust())
    {
      workbook.rowInserted(this, row);
    }

    // Adjust the maximum row record
    numRows++;
  }

  /**
   * Inserts a blank column into this spreadsheet.  If the column is out of 
   * range of the columns in the sheet, then no action is taken
   *
   * @param col the column to insert
   */
  public void insertColumn(int col)
  {
    if (col < 0 || col >= numColumns)
    {
      return;
    }

    // Iterate through all the row records adding in the column
    for (int i = 0 ; i < numRows ; i++)
    {
      if (rows[i] != null)
      {
        rows[i].insertColumn(col);
      }
    }

    // Adjust any hyperlinks
    HyperlinkRecord hr = null;
    Iterator i = hyperlinks.iterator();
    while (i.hasNext())
    {
      hr = (HyperlinkRecord) i.next();
      hr.insertColumn(col);
    }

    // Iterate through the column views, incrementing the column number
    i = columnFormats.iterator();
    while (i.hasNext())
    {
      ColumnInfoRecord cir = (ColumnInfoRecord) i.next();

      if (cir.getColumn() >= col)
      {
        cir.incrementColumn();
      }
    }

    // Handle any data validations
    if (dataValidation != null)
    {
      dataValidation.insertColumn(col);
    }

    // Adjust any merged cells
    mergedCells.insertColumn(col);

    // Handle interested cell references on the main workbook
    if (workbookSettings.getFormulaAdjust())
    {
      workbook.columnInserted(this, col);
    }

    numColumns++;
  }

  /**
   * Removes a column from this spreadsheet.  If the column is out of range
   * of the columns in the sheet, then no action is taken
   *
   * @param col the column to remove
   */
  public void removeColumn(int col)
  {
    if (col < 0 || col >= numColumns)
    {
      return;
    }

    // Iterate through all the row records removing the column
    for (int i = 0 ; i < numRows ; i++)
    {
      if (rows[i] != null)
      {
        rows[i].removeColumn(col);
      }
    }

    // Adjust any hyperlinks
    HyperlinkRecord hr = null;
    Iterator i = hyperlinks.iterator();
    while (i.hasNext())
    {
      hr = (HyperlinkRecord) i.next();

      if (hr.getColumn()      == col &&
          hr.getLastColumn()  == col)
      {
        // The row with the hyperlink on has been removed, so get
        // rid of it from the list
        hyperlinks.remove(hyperlinks.indexOf(hr));
      }
      else
      {
        hr.removeColumn(col);
      }
    }

    // Adjust any data validations
    if (dataValidation != null)
    {
      dataValidation.removeColumn(col);
    }

    // Adjust any merged cells
    mergedCells.removeColumn(col);

    // Iterate through the column views, decrementing the column number
    i = columnFormats.iterator();
    ColumnInfoRecord removeColumn = null;
    while (i.hasNext())
    {
      ColumnInfoRecord cir = (ColumnInfoRecord) i.next();

      if (cir.getColumn() == col)
      {
        removeColumn = cir;
      }
      else if (cir.getColumn() > col)
      {
        cir.decrementColumn();
      }
    }

    if (removeColumn != null)
    {
      columnFormats.remove(removeColumn);
    }

    // Handle interested cell references on the main workbook
    if (workbookSettings.getFormulaAdjust())
    {
      workbook.columnRemoved(this, col);
    }

    numColumns--;
  }

  /**
   * Removes a row from this spreadsheet.  If the row is out of 
   * range of the columns in the sheet, then no action is taken
   *
   * @param row the row to remove
   */
  public void removeRow(int row)
  {
    if (row < 0 || row >= numRows)
    {
      return;
    }

    // Create a new array to hold the new rows.  Grow it if need be
    RowRecord[] oldRows = rows;

    rows = new RowRecord[oldRows.length];

    // Copy in everything up to the row to be removed
    System.arraycopy(oldRows, 0, rows, 0, row);
    
    // Copy in the remaining rows
    System.arraycopy(oldRows, row + 1, rows, row, numRows - (row + 1));

    // Decrement all the internal row numbers by one
    for (int i = row; i < numRows; i++)
    {
      if (rows[i] != null)
      {
        rows[i].decrementRow();
      }
    }

    // Adjust any hyperlinks
    HyperlinkRecord hr = null;
    Iterator i = hyperlinks.iterator();
    while (i.hasNext())
    {
      hr = (HyperlinkRecord) i.next();

      if (hr.getRow()      == row &&
          hr.getLastRow()  == row)
      {
        // The row with the hyperlink on has been removed, so get
        // rid of it from the list
        i.remove();
      }
      else
      {
        hr.removeRow(row);
      }
    }

    // Adjust any data validations
    if (dataValidation != null)
    {
      dataValidation.removeRow(row);
    }

    // Adjust any merged cells
    mergedCells.removeRow(row);

    // Adjust any page breaks
    ArrayList newRowBreaks = new ArrayList();
    Iterator ri = rowBreaks.iterator();
    while (ri.hasNext())
    {
      int val = ( (Integer) ri.next()).intValue();

      if (val != row)
      {
        if (val > row)
        {
          val--;
        }
        
        newRowBreaks.add(new Integer(val));
      }
    }

    rowBreaks = newRowBreaks;

    // Handle interested cell references on the main workbook
    if (workbookSettings.getFormulaAdjust())
    {
      workbook.rowRemoved(this, row);
    }

    // Adjust the maximum row record
    numRows--;
  }

  /**
   * Adds the cell to this sheet.  If the cell has already been added to 
   * this sheet or another sheet, a WriteException is thrown.  If the
   * position to be occupied by this cell is already taken, the incumbent
   * cell is replaced.
   * The cell is then marked as referenced, and its formatting information 
   * registered with the list of formatting records updated if necessary
   * The RowsExceededException may be caught if client code wishes to
   * explicitly trap the case where too many rows have been written
   * to the current sheet.  If this behaviour is not desired, it is
   * sufficient simply to handle the WriteException, since this is a base
   * class of RowsExceededException
   * 
   * @exception WriteException 
   * @exception RowsExceededException
   * @param cell the cell to add
   */
  public void addCell(WritableCell cell) 
    throws WriteException, RowsExceededException
  {
    CellValue cv = (CellValue) cell;

    if (cv.isReferenced())
    {
      throw new JxlWriteException(JxlWriteException.cellReferenced);
    }

    int row = cell.getRow();
    RowRecord rowrec = getRowRecord(row);
    rowrec.addCell(cv);

    // Adjust the max rows and max columns accordingly
    numRows = Math.max(row+1, numRows);
    numColumns = Math.max(numColumns, rowrec.getMaxColumn());

    // Indicate this cell is now part of a worksheet, so that it can't be
    // added anywhere else
    cv.setCellDetails(formatRecords, sharedStrings, this);
  }

  /** 
   * Gets the row record at the specified row number, growing the
   * array as needs dictate
   * 
   * @param row the row number we are interested in
   * @return the row record at the specified row
   * @exception RowsExceededException
   */
  private RowRecord getRowRecord(int row) throws RowsExceededException
  {
    if (row >= numRowsPerSheet)
    {
      throw new RowsExceededException();
    }

    // Grow the array of rows if needs be
    // Thanks to Brendan for spotting the flaw in merely adding on the
    // grow size
    if (row >= rows.length)
    {
      RowRecord[] oldRows = rows;
      rows = new RowRecord[Math.max(oldRows.length + rowGrowSize, row+1)];
      System.arraycopy(oldRows, 0, rows, 0, oldRows.length);
      oldRows = null;
    }

    RowRecord rowrec = rows[row];

    if (rowrec == null)
    {
      rowrec = new RowRecord(row);
      rows[row] = rowrec;
    }

    return rowrec;
  }

  /**
   * Gets the row record for the specified row
   * 
   * @param r the row
   * @return the row record
   */
  RowRecord getRowInfo(int r)
  {
    if (r < 0 || r > rows.length)
    {
      return null;
    }

    return rows[r];
  }

  /**
   * Gets the column info record for the specified column
   *
   * @param c the column
   * @return the column record
   */
  ColumnInfoRecord getColumnInfo(int c)
  {
    Iterator i = columnFormats.iterator();
    ColumnInfoRecord cir = null;
    boolean stop = false;

    while (i.hasNext() && !stop)
    {
      cir = (ColumnInfoRecord) i.next();

      if (cir.getColumn() >= c)
      {
        stop = true;    
      }
    }

    if (!stop)
    {
      return null;
    }

    return cir.getColumn() == c ? cir : null;
  }

  /**
   * Sets the name of this worksheet
   * 
   * @param n the name of this sheet
   */
  public void setName(String n)
  {
    name = n;
  }

  /**
   * Sets the hidden status of this sheet
   * 
   * @param h the hiden flag
   * @deprecated use the settings bean instead
   */
  public void setHidden(boolean h)
  {
    settings.setHidden(h);
  }

  /**
   * Indicates whether or not this sheet is protected
   * 
   * @param prot protected flag
   * @deprecated use the settings bean instead
   */
  public void setProtected(boolean prot)
  {
    settings.setProtected(prot);
  }

  /**
   * Sets this sheet as selected
   * @deprecated use the settings bean
   */
  public void setSelected()
  {
    settings.setSelected();
  }
  
  /**
   * Retrieves the hidden status of this sheet
   * 
   * @return TRUE if hidden, FALSE otherwise
   * @deprecated in favour of the getSettings() method
   */
  public boolean isHidden()
  {
    return settings.isHidden();
  }

  /**
   * Sets the width (in characters) for a particular column in this sheet
   * 
   * @param col the column whose width to set
   * @param width the width of the column in characters
   */
  public void setColumnView(int col, int width)
  {
    CellView cv = new CellView();
    cv.setSize(width * 256);
    setColumnView(col, cv);
  }

  /**
   * Sets the width (in characters) and format options for a 
   * particular column in this sheet
   * 
   * @param col the column to set
   * @param width the width in characters
   * @param format the formt details for the column
   */
  public void setColumnView(int col, int width, CellFormat format)
  {
    CellView cv = new CellView();
    cv.setSize(width * 256);
    cv.setFormat(format);
    setColumnView(col, cv);
  }

  /** 
   * Sets the view for this column
   *
   * @param col the column on which to set the view
   * @param view the view to set
   */
  public void setColumnView(int col, CellView view)
  {
    XFRecord xfr = (XFRecord) view.getFormat();
    if (xfr == null)
    {
      xfr = (XFRecord) WritableWorkbookImpl.NORMAL_STYLE;
    }

    try
    {
      if (!xfr.isInitialized())
      {
        formatRecords.addStyle(xfr);
      }
      
      int width = view.depUsed() ? view.getDimension() * 256 : view.getSize();

      ColumnInfoRecord cir = new ColumnInfoRecord(col, 
                                                  width, 
                                                  xfr);

      if (view.isHidden())
      {
        cir.setHidden(true);
      }

      if (!columnFormats.contains(cir))
      {
        columnFormats.add(cir);
      }
      else
      {
        boolean removed = columnFormats.remove(cir);
        columnFormats.add(cir);
      }
    }
    catch (NumFormatRecordsException e)
    {
      logger.warn("Maximum number of format records exceeded.  Using " +
                  "default format.");

      ColumnInfoRecord cir = new ColumnInfoRecord
        (col, view.getDimension()*256, 
         (XFRecord) WritableWorkbook.NORMAL_STYLE);
      if (!columnFormats.contains(cir))
      {
        columnFormats.add(cir);
      }
    }
  }


  /**
   * Sets the height of the specified row, as well as its collapse status
   *
   * @param row the row to be formatted
   * @param height the row height in points
   * @exception RowsExceededException
   */
  public void setRowView(int row, int height) throws RowsExceededException
  {
    setRowView(row, height, false);
  }

  /**
   * Sets the height of the specified row, as well as its collapse status
   *
   * @param row the row to be formatted
   * @param collapsed indicates whether the row is collapsed
   * @exception jxl.write.biff.RowsExceededException
   */
  public void setRowView(int row, boolean collapsed)
    throws RowsExceededException
  {
    RowRecord rowrec = getRowRecord(row);
    rowrec.setCollapsed(collapsed);
  }

⌨️ 快捷键说明

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