📄 hssfcell.java
字号:
/** * set a string value for the cell. Please note that if you are using * full 16 bit unicode you should call <code>setEncoding()</code> first. * * @param value value to set the cell to. For formulas we'll set the formula * string, for String cells we'll set its value. For other types we will * change the cell to a string cell and set its value. * If value is null then we will change the cell to a Blank cell. */ public void setCellValue(String value) { if (value == null) { setCellType(CELL_TYPE_BLANK, false); } else { if ((cellType != CELL_TYPE_STRING ) && ( cellType != CELL_TYPE_FORMULA)) { setCellType(CELL_TYPE_STRING, false); } int index = 0; if (encoding == ENCODING_COMPRESSED_UNICODE) { index = book.addSSTString(value); } if (encoding == ENCODING_UTF_16) { index = book.addSSTString(value, true); } (( LabelSSTRecord ) record).setSSTIndex(index); stringValue = value; } } public void setCellFormula(String formula) { //Workbook.currentBook=book; if (formula==null) { setCellType(CELL_TYPE_BLANK,false); } else { setCellType(CELL_TYPE_FORMULA,false); FormulaRecordAggregate rec = (FormulaRecordAggregate) record; rec.getFormulaRecord().setOptions(( short ) 2); rec.getFormulaRecord().setValue(0); //only set to default if there is no extended format index already set if (rec.getXFIndex() == (short)0) rec.setXFIndex(( short ) 0x0f); FormulaParser fp = new FormulaParser(formula+";",book); fp.parse(); Ptg[] ptg = fp.getRPNPtg(); int size = 0; //System.out.println("got Ptgs " + ptg.length); for (int k = 0; k < ptg.length; k++) { size += ptg[ k ].getSize(); rec.getFormulaRecord().pushExpressionToken(ptg[ k ]); } rec.getFormulaRecord().setExpressionLength(( short ) size); //Workbook.currentBook = null; } } public String getCellFormula() { //Workbook.currentBook=book; String retval = FormulaParser.toFormulaString(book, ((FormulaRecordAggregate)record).getFormulaRecord().getParsedExpression()); //Workbook.currentBook=null; return retval; } /** * get the value of the cell as a number. For strings we throw an exception. * For blank cells we return a 0. */ public double getNumericCellValue() { if (cellType == CELL_TYPE_BLANK) { return 0; } if (cellType == CELL_TYPE_STRING) { throw new NumberFormatException( "You cannot get a numeric value from a String based cell"); } if (cellType == CELL_TYPE_BOOLEAN) { throw new NumberFormatException( "You cannot get a numeric value from a boolean cell"); } if (cellType == CELL_TYPE_ERROR) { throw new NumberFormatException( "You cannot get a numeric value from an error cell"); } return cellValue; } /** * get the value of the cell as a date. For strings we throw an exception. * For blank cells we return a null. */ public Date getDateCellValue() { if (cellType == CELL_TYPE_BLANK) { return null; } if (cellType == CELL_TYPE_STRING) { throw new NumberFormatException( "You cannot get a date value from a String based cell"); } if (cellType == CELL_TYPE_BOOLEAN) { 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"); } if (book.isUsing1904DateWindowing()) { return HSSFDateUtil.getJavaDate(cellValue,true); } else { return HSSFDateUtil.getJavaDate(cellValue,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 */ public String getStringCellValue() { if (cellType == CELL_TYPE_BLANK) { return ""; } 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 ""; } 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) { if ((cellType != CELL_TYPE_BOOLEAN ) && ( cellType != CELL_TYPE_FORMULA)) { setCellType(CELL_TYPE_BOOLEAN, false); } (( BoolErrRecord ) record).setValue(value); booleanValue = 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) { if ((cellType != CELL_TYPE_ERROR) && (cellType != CELL_TYPE_FORMULA)) { setCellType(CELL_TYPE_ERROR, false); } (( BoolErrRecord ) record).setValue(value); errorValue = 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 booleanValue; } 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 errorValue; } 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) { cellStyle = 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() { return cellStyle; } /** * used for internationalization, currently 0 for compressed unicode or 1 for 16-bit * * @see #ENCODING_COMPRESSED_UNICODE * @see #ENCODING_UTF_16 * * @return 1 or 0 for compressed or uncompressed (used only with String type) */ 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_COMPRESSED_UNICODE * @see #ENCODING_UTF_16 * * @param encoding either ENCODING_COMPRESSED_UNICODE (0) or ENCODING_UTF_16 (1) */ 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() { this.sheet.setActiveCellRow(this.row); this.sheet.setActiveCellCol(this.cellNum); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -