hssfrow.java

来自「EXCEL read and write」· Java 代码 · 共 669 行 · 第 1/2 页

JAVA
669
字号
        }        if(policy == RETURN_BLANK_AS_NULL) {            if(cell == null) return cell;            if(cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {                return null;            }            return cell;        }        if(policy == CREATE_NULL_AS_BLANK) {            if(cell == null) {                return createCell(cellnum, HSSFCell.CELL_TYPE_BLANK);            }            return cell;        }        throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")");    }    /**     * get the number of the first cell contained in this row.     * @return short representing the first logical cell in the row, or -1 if the row does not contain any cells.     */    public short getFirstCellNum()    {        if (getPhysicalNumberOfCells() == 0)            return -1;        else            return row.getFirstCol();    }    /**     * Gets the index of the last cell contained in this row <b>PLUS ONE</b>. The result also      * happens to be the 1-based column number of the last cell.  This value can be used as a     * standard upper bound when iterating over cells:     * <pre>      * short minColIx = row.getFirstCellNum();     * short maxColIx = row.getLastCellNum();     * for(short colIx=minColIx; colIx&lt;maxColIx; colIx++) {     *   HSSFCell cell = row.getCell(colIx);     *   if(cell == null) {     *     continue;     *   }     *   //... do something with cell     * }     * </pre>     *      * @return short representing the last logical cell in the row <b>PLUS ONE</b>, or -1 if the     *  row does not contain any cells.     */    public short getLastCellNum() {        if (getPhysicalNumberOfCells() == 0) {            return -1;        }        return row.getLastCol();    }    /**     * gets the number of defined cells (NOT number of cells in the actual row!).     * That is to say if only columns 0,4,5 have values then there would be 3.     * @return int representing the number of defined cells in the row.     */    public int getPhysicalNumberOfCells()    {      int count=0;      for(int i=0;i<cells.length;i++)      {        if(cells[i]!=null) count++;      }      return count;    }    /**     * set the row's height or set to ff (-1) for undefined/default-height.  Set the height in "twips" or     * 1/20th of a point.     * @param height  rowheight or 0xff for undefined (use sheet default)     */    public void setHeight(short height)    {        // row.setOptionFlags(        row.setBadFontHeight(true);        row.setHeight(height);    }    /**     * set whether or not to display this row with 0 height     * @param zHeight  height is zero or not.     */    public void setZeroHeight(boolean zHeight) {        row.setZeroHeight(zHeight);    }      /**     * get whether or not to display this row with 0 height     * @return - zHeight height is zero or not.     */    public boolean getZeroHeight() {        return row.getZeroHeight();    }    /**     * set the row's height in points.     * @param height  row height in points     */    public void setHeightInPoints(float height)    {        // row.setOptionFlags(        row.setBadFontHeight(true);        row.setHeight((short) (height * 20));    }    /**     * get the row's height or ff (-1) for undefined/default-height in twips (1/20th of a point)     * @return rowheight or 0xff for undefined (use sheet default)     */    public short getHeight()    {        short height = row.getHeight();        //The low-order 15 bits contain the row height.        //The 0x8000 bit indicates that the row is standard height (optional)         if ((height & 0x8000) != 0) height = sheet.getSheet().getDefaultRowHeight();        else height &= 0x7FFF;        return height;    }    /**     * get the row's height or ff (-1) for undefined/default-height in points (20*getHeight())     * @return rowheight or 0xff for undefined (use sheet default)     */    public float getHeightInPoints()    {        return ((float)getHeight() / 20);    }    /**     * get the lowlevel RowRecord represented by this object - should only be called     * by other parts of the high level API     *     * @return RowRecord this row represents     */    protected RowRecord getRowRecord()    {        return row;    }    /**     * used internally to refresh the "last cell" when the last cell is removed.     */    private short findLastCell(short lastcell)    {        short cellnum = (short) (lastcell - 1);        HSSFCell r = getCell(cellnum);        while (r == null && cellnum >= 0)        {            r = getCell(--cellnum);        }        return cellnum;    }    /**     * used internally to refresh the "first cell" when the first cell is removed.     */    private short findFirstCell(short firstcell)    {        short cellnum = (short) (firstcell + 1);        HSSFCell r = getCell(cellnum);        while (r == null && cellnum <= getLastCellNum())        {            r = getCell(++cellnum);        }        if (cellnum > getLastCellNum())            return -1;        return cellnum;    }        /**     * Is this row formatted? Most aren't, but some rows     *  do have whole-row styles. For those that do, you     *  can get the formatting from {@link #getRowStyle()}     */    public boolean isFormatted() {    	return row.getFormatted();    }    /**     * Returns the whole-row cell styles. Most rows won't     *  have one of these, so will return null. Call     *  {@link #isFormatted()} to check first.     */    public HSSFCellStyle getRowStyle() {    	if(!isFormatted()) { return null; }        short styleIndex = row.getXFIndex();        ExtendedFormatRecord xf = book.getWorkbook().getExFormatAt(styleIndex);        return new HSSFCellStyle(styleIndex, xf, book);    }    /**     * Applies a whole-row cell styling to the row.     */    public void setRowStyle(HSSFCellStyle style) {    	row.setFormatted(true);    	row.setXFIndex(style.getIndex());    }    /**     * Used to specify the different possible policies     *  if for the case of null and blank cells     */    public static class MissingCellPolicy {        private static int NEXT_ID = 1;        private final int id;        /* package */ MissingCellPolicy() {            this.id = NEXT_ID++;        }    }    /** Missing cells are returned as null, Blank cells are returned as normal */    public static final MissingCellPolicy RETURN_NULL_AND_BLANK = new MissingCellPolicy();    /** Missing cells are returned as null, as are blank cells */    public static final MissingCellPolicy RETURN_BLANK_AS_NULL = new MissingCellPolicy();    /** A new, blank cell is created for missing cells. Blank cells are returned as normal */    public static final MissingCellPolicy CREATE_NULL_AS_BLANK = new MissingCellPolicy();        /**     * @return cell iterator of the physically defined cells.      * Note that the 4th element might well not be cell 4, as the iterator     *  will not return un-defined (null) cells.     * Call getCellNum() on the returned cells to know which cell they are.     * As this only ever works on physically defined cells,      *  the {@link MissingCellPolicy} has no effect.     */    public Iterator cellIterator()    {      return new CellIterator();    }    /**     * Alias for {@link CellIterator} to allow     *  foreach loops     */    public Iterator iterator() {        return cellIterator();    }        /**     * An iterator over the (physical) cells in the row.     */    private class CellIterator implements Iterator    {      int thisId=-1;      int nextId=-1;            public CellIterator()      {        findNext();      }      public boolean hasNext() {        return nextId<cells.length;      }      public Object next() {          if (!hasNext())              throw new NoSuchElementException("At last element");        HSSFCell cell=cells[nextId];        thisId=nextId;        findNext();        return cell;      }      public void remove() {          if (thisId == -1)              throw new IllegalStateException("remove() called before next()");        cells[thisId]=null;      }            private void findNext()      {        int i=nextId+1;        for(;i<cells.length;i++)        {          if(cells[i]!=null) break;        }        nextId=i;      }          }    public int compareTo(Object obj)    {        HSSFRow loc = (HSSFRow) obj;        if (this.getRowNum() == loc.getRowNum())        {            return 0;        }        if (this.getRowNum() < loc.getRowNum())        {            return -1;        }        if (this.getRowNum() > loc.getRowNum())        {            return 1;        }        return -1;    }    public boolean equals(Object obj)    {        if (!(obj instanceof HSSFRow))        {            return false;        }        HSSFRow loc = (HSSFRow) obj;        if (this.getRowNum() == loc.getRowNum())        {            return true;        }        return false;    }}

⌨️ 快捷键说明

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