| Constructor: |
protected HSSFCell(HSSFWorkbook book,
Sheet sheet,
int row,
short col) {
//protected HSSFCell(Workbook book, Sheet sheet, short row, short col)
checkBounds(col);
stringValue = null;
this.book = book;
this.sheet = sheet;
// Relying on the fact that by default the cellType is set to 0 which
// is different to CELL_TYPE_BLANK hence the following method call correctly
// creates a new blank cell.
short xfindex = sheet.getXFIndexForColAt(col);
setCellType(CELL_TYPE_BLANK, false, row, col,xfindex);
}
Creates new Cell - Should only be called by HSSFRow. This creates a cell
from scratch.
When the cell is initially created it is set to CELL_TYPE_BLANK. Cell types
can be changed/overwritten by calling setCellValue with the appropriate
type as a parameter although conversions from one type to another may be
prohibited. Parameters:
book - - Workbook record of the workbook containing this cell
sheet - - Sheet record of the sheet containing this cell
row - - the row of this cell
col - - the column for this cell
Also see:
- org.apache.poi.hssf.usermodel.HSSFRow#createCell(short)
|
protected HSSFCell(HSSFWorkbook book,
Sheet sheet,
int row,
CellValueRecordInterface cval) {
record = cval;
cellType = determineType(cval);
stringValue = null;
this.book = book;
this.sheet = sheet;
switch (cellType)
{
case CELL_TYPE_STRING :
stringValue = new HSSFRichTextString(book.getWorkbook(), (LabelSSTRecord ) cval);
break;
case CELL_TYPE_BLANK :
break;
case CELL_TYPE_FORMULA :
stringValue=new HSSFRichTextString(((FormulaRecordAggregate) cval).getStringValue());
break;
}
ExtendedFormatRecord xf = book.getWorkbook().getExFormatAt(cval.getXFIndex());
setCellStyle(new HSSFCellStyle(( short ) cval.getXFIndex(), xf, book));
}
Creates an HSSFCell from a CellValueRecordInterface. HSSFSheet uses this when
reading in cells from an existing sheet. Parameters:
book - - Workbook record of the workbook containing this cell
sheet - - Sheet record of the sheet containing this cell
cval - - the Cell Value Record we wish to represent
|
protected HSSFCell(HSSFWorkbook book,
Sheet sheet,
int row,
short col,
int type) {
checkBounds(col);
cellType = -1; // Force 'setCellType' to create a first Record
stringValue = null;
this.book = book;
this.sheet = sheet;
short xfindex = sheet.getXFIndexForColAt(col);
setCellType(type,false,row,col,xfindex);
}
Creates new Cell - Should only be called by HSSFRow. This creates a cell
from scratch. Parameters:
book - - Workbook record of the workbook containing this cell
sheet - - Sheet record of the sheet containing this cell
row - - the row of this cell
col - - the column for this cell
type - - CELL_TYPE_NUMERIC, CELL_TYPE_STRING, CELL_TYPE_FORMULA, CELL_TYPE_BLANK,
CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR
Type of cell
Also see:
- org.apache.poi.hssf.usermodel.HSSFRow#createCell(short,int)
|
| Method from org.apache.poi.hssf.usermodel.HSSFCell Detail: |
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;
}
Cell comment finder.
Returns cell comment for the specified sheet, row and column. |
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 a boolean. For strings, numbers, and errors, we throw an exception.
For blank cells we return a false. |
protected Workbook getBoundWorkbook() {
return book.getWorkbook();
}
Returns the Workbook that this Cell is bound to |
public HSSFComment getCellComment() {
if (comment == null) {
comment = findCellComment(sheet, record.getRow(), record.getColumn());
}
return comment;
}
Returns comment associated with this cell |
public String getCellFormula() {
//Workbook.currentBook=book;
String retval = FormulaParser.toFormulaString(book, ((FormulaRecordAggregate)record).getFormulaRecord().getParsedExpression());
//Workbook.currentBook=null;
return retval;
}
|
public short getCellNum() {
return record.getColumn();
}
get the cell's number within the row |
public HSSFCellStyle getCellStyle() {
short styleIndex=record.getXFIndex();
ExtendedFormatRecord xf = book.getWorkbook().getExFormatAt(styleIndex);
return new HSSFCellStyle(styleIndex, xf, book);
}
get the style for the cell. This is a reference to a cell style contained in the workbook
object. |
public int getCellType() {
return cellType;
}
get the cells type (numeric, formula or string) |
protected CellValueRecordInterface getCellValueRecord() {
return record;
}
Should only be used by HSSFSheet and friends. Returns the low level CellValueRecordInterface record |
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");
}
double value=this.getNumericCellValue();
if (book.getWorkbook().isUsing1904DateWindowing()) {
return HSSFDateUtil.getJavaDate(value,true);
}
else {
return HSSFDateUtil.getJavaDate(value,false);
}
}
get the value of the cell as a date. For strings we throw an exception.
For blank cells we return a null. |
public short getEncoding() {
return encoding;
} Deprecated! As - of 3-Jan-06 POI now automatically handles Unicode without forcing the encoding.
used for internationalization, currently -1 for unchanged, 0 for compressed unicode or 1 for 16-bit |
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");
}
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 HSSFHyperlink getHyperlink() {
for (Iterator it = sheet.getRecords().iterator(); it.hasNext(); ) {
Record rec = ( Record ) it.next();
if (rec instanceof HyperlinkRecord){
HyperlinkRecord link = (HyperlinkRecord)rec;
if(link.getFirstColumn() == record.getColumn() && link.getFirstRow() == record.getRow()){
return new HSSFHyperlink(link);
}
}
}
return null;
}
Returns hyperlink associated with this cell |
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");
}
if(cellType == CELL_TYPE_NUMERIC)
{
return ((NumberRecord)record).getValue();
}
if(cellType == CELL_TYPE_FORMULA)
{
return ((FormulaRecordAggregate)record).getFormulaRecord().getValue();
}
throw new NumberFormatException("Unknown Record Type in Cell:"+cellType);
}
get the value of the cell as a number. For strings we throw an exception.
For blank cells we return a 0. |
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;
}
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() {
HSSFRichTextString str = getRichStringCellValue();
return str.getString();
} Deprecated! Use - the HSSFRichTextString return
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 void setAsActiveCell() {
int row=record.getRow();
short col=record.getColumn();
this.sheet.setActiveCellRow(row);
this.sheet.setActiveCellCol(col);
}
Sets this cell as the active cell for the worksheet |
public void setCellComment(HSSFComment comment) {
comment.setRow((short)record.getRow());
comment.setColumn(record.getColumn());
this.comment = comment;
}
Assign a comment to this cell |
public void setCellErrorValue(byte value) {
int row=record.getRow();
short col=record.getColumn();
short styleIndex=record.getXFIndex();
if (cellType != CELL_TYPE_ERROR) {
setCellType(CELL_TYPE_ERROR, false, row, col, styleIndex);
}
(( BoolErrRecord ) record).setValue(value);
}
set a error value for the cell |
public void setCellFormula(String formula) {
int row=record.getRow();
short col=record.getColumn();
short styleIndex=record.getXFIndex();
//Workbook.currentBook=book;
if (formula==null) {
setCellType(CELL_TYPE_BLANK,false,row,col,styleIndex);
} else {
setCellType(CELL_TYPE_FORMULA,false,row,col,styleIndex);
FormulaRecordAggregate rec = (FormulaRecordAggregate) record;
FormulaRecord frec = rec.getFormulaRecord();
frec.setOptions(( short ) 2);
frec.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;
// clear the Ptg Stack
for (int i=0, iSize=frec.getNumberOfExpressionTokens(); i< iSize; i++) {
frec.popExpressionToken();
}
// fill the Ptg Stack with Ptgs of new formula
for (int k = 0; k < ptg.length; k++) {
size += ptg[ k ].getSize();
frec.pushExpressionToken(ptg[ k ]);
}
rec.getFormulaRecord().setExpressionLength(( short ) size);
//Workbook.currentBook = null;
}
}
|
public void setCellNum(short num) {
record.setColumn(num);
} Deprecated! Doesn - 't update the row's idea of what cell this is, use HSSFRow#moveCell(HSSFCell, short) instead
Set the cell's number within the row (0 based). |
public void setCellStyle(HSSFCellStyle style) {
record.setXFIndex(style.getIndex());
}
set the style for the cell. The style should be an HSSFCellStyle created/retreived from
the HSSFWorkbook. |
public void setCellType(int cellType) {
int row=record.getRow();
short col=record.getColumn();
short styleIndex=record.getXFIndex();
setCellType(cellType, true, row, col, styleIndex);
}
set the cells type (numeric, formula or string) |
public void setCellValue(double value) {
int row=record.getRow();
short col=record.getColumn();
short styleIndex=record.getXFIndex();
if ((cellType != CELL_TYPE_NUMERIC) && (cellType != CELL_TYPE_FORMULA))
{
setCellType(CELL_TYPE_NUMERIC, false, row, col, styleIndex);
}
// Save into the apropriate record
if(record instanceof FormulaRecordAggregate) {
(( FormulaRecordAggregate ) record).getFormulaRecord().setValue(value);
} else {
(( NumberRecord ) record).setValue(value);
}
}
set a numeric value for the cell |
public void setCellValue(Date value) {
setCellValue(HSSFDateUtil.getExcelDate(value, this.book.getWorkbook().isUsing1904DateWindowing()));
}
set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
a date. |
public void setCellValue(Calendar value) {
setCellValue( HSSFDateUtil.getExcelDate(value, this.book.getWorkbook().isUsing1904DateWindowing()) );
}
set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
a date.
This will set the cell value based on the Calendar's timezone. As Excel
does not support timezones this means that both 20:00+03:00 and
20:00-03:00 will be reported as the same value (20:00) even that there
are 6 hours difference between the two times. This difference can be
preserved by using setCellValue(value.getTime()) which will
automatically shift the times to the default timezone. |
public void setCellValue(String value) {
HSSFRichTextString str = new HSSFRichTextString(value);
setCellValue(str);
} Deprecated! Use - setCellValue(HSSFRichTextString) instead.
set a string value for the cell. Please note that if you are using
full 16 bit unicode you should call setEncoding() first. |
public void setCellValue(HSSFRichTextString value) {
int row=record.getRow();
short col=record.getColumn();
short styleIndex=record.getXFIndex();
if (value == null) {
setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
return;
}
if (cellType == CELL_TYPE_FORMULA) {
// Set the 'pre-evaluated result' for the formula
// note - formulas do not preserve text formatting.
FormulaRecordAggregate fr = (FormulaRecordAggregate) record;
// must make new sr because fr.getStringRecord() may be null
StringRecord sr = new StringRecord();
sr.setString(value.getString()); // looses format
fr.setStringRecord(sr);
return;
}
if (cellType != CELL_TYPE_STRING) {
setCellType(CELL_TYPE_STRING, false, row, col, styleIndex);
}
int index = 0;
UnicodeString str = value.getUnicodeString();
index = book.getWorkbook().addSSTString(str);
(( LabelSSTRecord ) record).setSSTIndex(index);
stringValue = value;
stringValue.setWorkbookReferences(book.getWorkbook(), (( LabelSSTRecord ) record));
stringValue.setUnicodeString(book.getWorkbook().getSSTString(index));
}
set a string value for the cell. Please note that if you are using
full 16 bit unicode you should call setEncoding() first. |
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 boolean value for the cell |
public void setEncoding(short encoding) {
this.encoding = encoding;
} Deprecated! As - of 3-Jan-06 POI now automatically handles Unicode without forcing the encoding.
set the encoding to either 8 or 16 bit. (US/UK use 8-bit, rest of the western world use 16bit) |
public void setHyperlink(HSSFHyperlink link) {
link.setFirstRow(record.getRow());
link.setLastRow(record.getRow());
link.setFirstColumn(record.getColumn());
link.setLastColumn(record.getColumn());
switch(link.getType()){
case HSSFHyperlink.LINK_EMAIL:
case HSSFHyperlink.LINK_URL:
link.setLabel("url");
break;
case HSSFHyperlink.LINK_FILE:
link.setLabel("file");
break;
case HSSFHyperlink.LINK_DOCUMENT:
link.setLabel("place");
break;
}
int eofLoc = sheet.findFirstRecordLocBySid( EOFRecord.sid );
sheet.getRecords().add( eofLoc, link.record );
}
Assign a hypelrink to this cell |
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();
}
}
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> |
protected void updateCellNum(short num) {
record.setColumn(num);
}
Updates the cell record's idea of what
column it belongs in (0 based) |