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

📄 writablesheetimpl.java

📁 jxtl API Java中Excel的生成与导入解析参考文档
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
      for (Iterator drawingIt = drawings.iterator() ; drawingIt.hasNext() ; )
      {
        DrawingGroupObject dgo = (DrawingGroupObject) drawingIt.next();
        dgo.removeRow(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
  {
    if (cell.getType() == CellType.EMPTY)
    {
      if (cell != null && cell.getCellFormat() == null)
      {
        // return if it's a blank cell with no particular cell formatting
        // information
        return;
      }
    }
    
    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
   */
  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, this);
      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 Use the sheet settings bean instead
   */
  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)
    {
      Styles styles = getWorkbook().getStyles();
      xfr = styles.getNormalStyle();
    }

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

      if (view.isAutosize())
      {
        autosizedColumns.add(new Integer(col));
      }

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

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

      if (!columnFormats.contains(cir))
      {
        columnFormats.add(cir);
      }
      else
      {
        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, 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 1/20ths of a  point
   * @exception RowsExceededException
   * @deprecated use the override which takes a CellView object
   */
  public void setRowView(int row, int height) throws RowsExceededException
  {
    CellView cv = new CellView();
    cv.setSize(height);
    cv.setHidden(false);
    setRowView(row, cv);
  }

  /**
   * 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
   * @deprecated use the override which takes a CellView object
   */
  public void setRowView(int row, boolean collapsed)
    throws RowsExceededException
  {
    CellView cv = new CellView();
    cv.setHidden(collapsed);
    setRowView(row, cv);
  }

  /**
   * 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 1/20th of a point
   * @param collapsed indicates whether the row is collapsed
   * @param zeroHeight indicates that the row has zero height
   * @exception RowsExceededException
   * @deprecated use the override which takes a CellView object
   */
  public void setRowView(int row, int height, 
                         boolean collapsed)
                         throws RowsExceededException
  {
    CellView cv = new CellView();
    cv.setSize(height);
    cv.setHidden(collapsed);
    setRowView(row, cv);
  }

  /**
   * Sets the view for this column
   *
   * @param row the column on which to set the view
   * @param view the view to set
   * @exception RowsExceededException
   */
  public void setRowView(int row, CellView view) throws RowsExceededException
  {
    RowRecord rowrec = getRowRecord(row);

    XFRecord xfr =  (XFRecord) view.getFormat();

    try
    {
      if (xfr != null)
      {
        if (!xfr.isInitialized())
        {
          formatRecords.addStyle(xfr);
        }
      }
    }
    catch (NumFormatRecordsException e)
    {
      logger.warn("Maximum number of format records exceeded.  Using " +
                  "default format.");

      xfr = null;
    }

    rowrec.setRowDetails(view.getSize(),
                         false,
                         view.isHidden(),
                         0,
                         false,
                         xfr);
    numRows = Math.max(numRows, row + 1);
  }

  /**
   * Writes out this sheet.  This functionality is delegated off to the 
   * SheetWriter class in order to reduce the bloated nature of this source
   * file
   *
   * @exception IOException 
   */
  public void write() throws IOException
  {
    boolean dmod = drawingsModified;
    if (workbook.getDrawingGroup() != null)
    {
      dmod |= workbook.getDrawingGroup().hasDrawingsOmitted();
    }

    if (autosizedColumns.size() > 0)
    {
      autosizeColumns();
    }

    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 cell contents from the specified sheet into this one
   *
   * @param s the sheet to copy
   */
  private void copyCells(Sheet 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];
        CellType ct = cell.getType();

        // Encase the calls to addCell in a try-catch block
        // These should not generate any errors, because we are
        // copying from an existing spreadsheet.  In the event of
        // errors, catch the exception and then bomb out with an
        // assertion
        try
        {
          if (ct == CellType.LABEL)
          {
            Label l = new Label((LabelCell) cell);
            addCell(l);
          }
          else if (ct == CellType.NUMBER)
          {
            Number n = new Number((NumberCell) cell);
            addCell(n);
          }
          else if (ct == CellType.DATE)
          {
            DateTime dt = new DateTime((DateCell) cell);
            addCell(dt);
          }
          else if (ct == CellType.BOOLEAN)
          {
            Boolean b = new Boolean((BooleanCell) cell);
            addCell(b);
          }
          else if (ct == CellType.NUMBER_FORMULA)
          {
            ReadNumberFormulaRecord fr = new ReadNumberFormulaRecord
              ((FormulaData) cell);
            addCell(fr);
          }
          else if (ct == CellType.STRING_FORMULA)
          {
            ReadStringFormulaRecord fr = new ReadStringFormulaRecord
              ((FormulaData) cell);
            addCell(fr);
          }
          else if( ct == CellType.BOOLEAN_FORMULA)
          {
            ReadBooleanFormulaRecord fr = new ReadBooleanFormulaRecord
              ((FormulaData) cell);
            addCell(fr);
          }
          else if (ct == CellType.DATE_FORMULA)
          {
            ReadDateFormulaRecord fr = new ReadDateFormulaRecord
              ((FormulaData) cell);
            addCell(fr);
          }
          else if(ct == CellType.FORMULA_ERROR)
          {
            ReadErrorFormulaRecord fr = new ReadErrorFormulaRecord
              ((FormulaData) cell);
            addCell(fr);
          }
          else if (ct == CellType.EMPTY)
          {
            if (cell.getCellFormat() != null)
            {
              // It is a blank cell, rather than an empty cell, so

⌨️ 快捷键说明

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