📄 hssfcell.java
字号:
{ throw new NumberFormatException( "You cannot get a date value from a boolean cell"); } if (cellType == CELL_TYPE_ERROR) { throw new NumberFormatException( "You cannot get a date value from an error cell"); } double value=this.getNumericCellValue(); if (book.isUsing1904DateWindowing()) { return HSSFDateUtil.getJavaDate(value,true); } else { return HSSFDateUtil.getJavaDate(value,false); } } /** * get the value of the cell as a string - for numeric cells we throw an exception. * For blank cells we return an empty string. * For formulaCells that are not string Formulas, we return empty String * @deprecated Use the HSSFRichTextString return */ public String getStringCellValue() { HSSFRichTextString str = getRichStringCellValue(); return str.getString(); } /** * get the value of the cell as a string - for numeric cells we throw an exception. * For blank cells we return an empty string. * For formulaCells that are not string Formulas, we return empty String */ public HSSFRichTextString getRichStringCellValue() { if (cellType == CELL_TYPE_BLANK) { return new HSSFRichTextString(""); } if (cellType == CELL_TYPE_NUMERIC) { throw new NumberFormatException( "You cannot get a string value from a numeric cell"); } if (cellType == CELL_TYPE_BOOLEAN) { throw new NumberFormatException( "You cannot get a string value from a boolean cell"); } if (cellType == CELL_TYPE_ERROR) { throw new NumberFormatException( "You cannot get a string value from an error cell"); } if (cellType == CELL_TYPE_FORMULA) { if (stringValue==null) return new HSSFRichTextString(""); } return stringValue; } /** * set a boolean value for the cell * * @param value the boolean value to set this cell to. For formulas we'll set the * precalculated value, for booleans we'll set its value. For other types we * will change the cell to a boolean cell and set its value. */ public void setCellValue(boolean value) { int row=record.getRow(); short col=record.getColumn(); short styleIndex=record.getXFIndex(); if ((cellType != CELL_TYPE_BOOLEAN ) && ( cellType != CELL_TYPE_FORMULA)) { setCellType(CELL_TYPE_BOOLEAN, false, row, col, styleIndex); } (( BoolErrRecord ) record).setValue(value); } /** * set a error value for the cell * * @param value the error value to set this cell to. For formulas we'll set the * precalculated value ??? IS THIS RIGHT??? , for errors we'll set * its value. For other types we will change the cell to an error * cell and set its value. */ public void setCellErrorValue(byte value) { int row=record.getRow(); short col=record.getColumn(); short styleIndex=record.getXFIndex(); if ((cellType != CELL_TYPE_ERROR) && (cellType != CELL_TYPE_FORMULA)) { setCellType(CELL_TYPE_ERROR, false, row, col, styleIndex); } (( BoolErrRecord ) record).setValue(value); } /** * get the value of the cell as a boolean. For strings, numbers, and errors, we throw an exception. * For blank cells we return a false. */ public boolean getBooleanCellValue() { if (cellType == CELL_TYPE_BOOLEAN) { return (( BoolErrRecord ) record).getBooleanValue(); } if (cellType == CELL_TYPE_BLANK) { return false; } throw new NumberFormatException( "You cannot get a boolean value from a non-boolean cell"); } /** * get the value of the cell as an error code. For strings, numbers, and booleans, we throw an exception. * For blank cells we return a 0. */ public byte getErrorCellValue() { if (cellType == CELL_TYPE_ERROR) { return (( BoolErrRecord ) record).getErrorValue(); } if (cellType == CELL_TYPE_BLANK) { return ( byte ) 0; } throw new NumberFormatException( "You cannot get an error value from a non-error cell"); } /** * set the style for the cell. The style should be an HSSFCellStyle created/retreived from * the HSSFWorkbook. * * @param style reference contained in the workbook * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createCellStyle() * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short) */ public void setCellStyle(HSSFCellStyle style) { record.setXFIndex(style.getIndex()); } /** * get the style for the cell. This is a reference to a cell style contained in the workbook * object. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short) */ public HSSFCellStyle getCellStyle() { short styleIndex=record.getXFIndex(); ExtendedFormatRecord xf = book.getExFormatAt(styleIndex); return new HSSFCellStyle(styleIndex, xf); } /** * used for internationalization, currently -1 for unchanged, 0 for compressed unicode or 1 for 16-bit * * @see #ENCODING_UNCHANGED * @see #ENCODING_COMPRESSED_UNICODE * @see #ENCODING_UTF_16 * * @return -1, 1 or 0 for unchanged, compressed or uncompressed (used only with String type) * * @deprecated As of 3-Jan-06 POI now automatically handles Unicode without forcing the encoding. */ public short getEncoding() { return encoding; } /** * set the encoding to either 8 or 16 bit. (US/UK use 8-bit, rest of the western world use 16bit) * * @see #ENCODING_UNCHANGED * @see #ENCODING_COMPRESSED_UNICODE * @see #ENCODING_UTF_16 * * @param encoding either ENCODING_COMPRESSED_UNICODE (0) or ENCODING_UTF_16 (1) * @deprecated As of 3-Jan-06 POI now automatically handles Unicode without forcing the encoding. */ public void setEncoding(short encoding) { this.encoding = encoding; } /** * Should only be used by HSSFSheet and friends. Returns the low level CellValueRecordInterface record * * @return CellValueRecordInterface representing the cell via the low level api. */ protected CellValueRecordInterface getCellValueRecord() { return record; } /** * @throws RuntimeException if the bounds are exceeded. */ private void checkBounds(int cellNum) { if (cellNum > 255) { throw new RuntimeException("You cannot have more than 255 columns "+ "in a given row (IV). Because Excel can't handle it"); } else if (cellNum < 0) { throw new RuntimeException("You cannot reference columns with an index of less then 0."); } } /** * Sets this cell as the active cell for the worksheet */ public void setAsActiveCell() { int row=record.getRow(); short col=record.getColumn(); this.sheet.setActiveCellRow(row); this.sheet.setActiveCellCol(col); } /** * Returns a string representation of the cell * * This method returns a simple representation, * anthing more complex should be in user code, with * knowledge of the semantics of the sheet being processed. * * Formula cells return the formula string, * rather than the formula result. * Dates are displayed in dd-MMM-yyyy format * Errors are displayed as #ERR<errIdx> */ public String toString() { switch (getCellType()) { case CELL_TYPE_BLANK: return ""; case CELL_TYPE_BOOLEAN: return getBooleanCellValue()?"TRUE":"FALSE"; case CELL_TYPE_ERROR: return "#ERR"+getErrorCellValue(); case CELL_TYPE_FORMULA: return getCellFormula(); case CELL_TYPE_NUMERIC: //TODO apply the dataformat for this cell if (HSSFDateUtil.isCellDateFormatted(this)) { DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy"); return sdf.format(getDateCellValue()); }else { return getNumericCellValue() + ""; } case CELL_TYPE_STRING: return getStringCellValue(); default: return "Unknown Cell Type: " + getCellType(); } } /** * Assign a comment to this cell * * @param comment comment associated with this cell */ public void setCellComment(HSSFComment comment){ comment.setRow((short)record.getRow()); comment.setColumn(record.getColumn()); this.comment = comment; } /** * Returns comment associated with this cell * * @return comment associated with this cell */ public HSSFComment getCellComment(){ if (comment == null) { comment = findCellComment(sheet, record.getRow(), record.getColumn()); } return comment; } /** * Cell comment finder. * Returns cell comment for the specified sheet, row and column. * * @return cell comment or <code>null</code> if not found */ protected static HSSFComment findCellComment(Sheet sheet, int row, int column){ HSSFComment comment = null; HashMap txshapes = new HashMap(); //map shapeId and TextObjectRecord for (Iterator it = sheet.getRecords().iterator(); it.hasNext(); ) { Record rec = ( Record ) it.next(); if (rec instanceof NoteRecord){ NoteRecord note = (NoteRecord)rec; if (note.getRow() == row && note.getColumn() == column){ TextObjectRecord txo = (TextObjectRecord)txshapes.get(new Integer(note.getShapeId())); comment = new HSSFComment(note, txo); comment.setRow(note.getRow()); comment.setColumn(note.getColumn()); comment.setAuthor(note.getAuthor()); comment.setVisible(note.getFlags() == NoteRecord.NOTE_VISIBLE); comment.setString(txo.getStr()); break; } } else if (rec instanceof ObjRecord){ ObjRecord obj = (ObjRecord)rec; SubRecord sub = (SubRecord)obj.getSubRecords().get(0); if (sub instanceof CommonObjectDataSubRecord){ CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord)sub; if (cmo.getObjectType() == CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT){ //find the nearest TextObjectRecord which holds comment's text and map it to its shapeId while(it.hasNext()) { rec = ( Record ) it.next(); if (rec instanceof TextObjectRecord) { txshapes.put(new Integer(cmo.getObjectId()), rec); break; } } } } } } return comment; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -