Methods Summary |
---|
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.");
}
|
private int | determineType(org.apache.poi.hssf.record.CellValueRecordInterface cval)used internally -- given a cell value record, figure out its type
Record record = ( Record ) cval;
int sid = record.getSid();
int retval = 0;
switch (sid)
{
case NumberRecord.sid :
retval = HSSFCell.CELL_TYPE_NUMERIC;
break;
case BlankRecord.sid :
retval = HSSFCell.CELL_TYPE_BLANK;
break;
case LabelSSTRecord.sid :
retval = HSSFCell.CELL_TYPE_STRING;
break;
case FormulaRecordAggregate.sid :
retval = HSSFCell.CELL_TYPE_FORMULA;
break;
case BoolErrRecord.sid :
BoolErrRecord boolErrRecord = ( BoolErrRecord ) record;
retval = (boolErrRecord.isBoolean())
? HSSFCell.CELL_TYPE_BOOLEAN
: HSSFCell.CELL_TYPE_ERROR;
break;
}
return retval;
|
protected static org.apache.poi.hssf.usermodel.HSSFComment | findCellComment(org.apache.poi.hssf.model.Sheet sheet, int row, int column)Cell comment finder.
Returns cell comment for the specified sheet, row and 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;
|
public boolean | getBooleanCellValue()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.
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");
|
public org.apache.poi.hssf.usermodel.HSSFComment | getCellComment()Returns comment associated with this cell
if (comment == null) {
comment = findCellComment(sheet, record.getRow(), record.getColumn());
}
return comment;
|
public java.lang.String | getCellFormula()
//Workbook.currentBook=book;
String retval = FormulaParser.toFormulaString(book, ((FormulaRecordAggregate)record).getFormulaRecord().getParsedExpression());
//Workbook.currentBook=null;
return retval;
|
public short | getCellNum()get the cell's number within the row
return record.getColumn();
|
public org.apache.poi.hssf.usermodel.HSSFCellStyle | getCellStyle()get the style for the cell. This is a reference to a cell style contained in the workbook
object.
short styleIndex=record.getXFIndex();
ExtendedFormatRecord xf = book.getExFormatAt(styleIndex);
return new HSSFCellStyle(styleIndex, xf);
|
public int | getCellType()get the cells type (numeric, formula or string)
return cellType;
|
protected org.apache.poi.hssf.record.CellValueRecordInterface | getCellValueRecord()Should only be used by HSSFSheet and friends. Returns the low level CellValueRecordInterface record
return record;
|
public java.util.Date | getDateCellValue()get the value of the cell as a date. For strings we throw an exception.
For blank cells we return a null.
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.isUsing1904DateWindowing()) {
return HSSFDateUtil.getJavaDate(value,true);
}
else {
return HSSFDateUtil.getJavaDate(value,false);
}
|
public short | getEncoding()used for internationalization, currently -1 for unchanged, 0 for compressed unicode or 1 for 16-bit
return encoding;
|
public byte | getErrorCellValue()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.
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");
|
public double | getNumericCellValue()get the value of the cell as a number. For strings we throw an exception.
For blank cells we return a 0.
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);
|
public org.apache.poi.hssf.usermodel.HSSFRichTextString | getRichStringCellValue()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
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;
|
public java.lang.String | getStringCellValue()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
HSSFRichTextString str = getRichStringCellValue();
return str.getString();
|
public void | setAsActiveCell()Sets this cell as the active cell for the worksheet
int row=record.getRow();
short col=record.getColumn();
this.sheet.setActiveCellRow(row);
this.sheet.setActiveCellCol(col);
|
public void | setCellComment(org.apache.poi.hssf.usermodel.HSSFComment comment)Assign a comment to this cell
comment.setRow((short)record.getRow());
comment.setColumn(record.getColumn());
this.comment = comment;
|
public void | setCellErrorValue(byte value)set a error value for the cell
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);
|
public void | setCellFormula(java.lang.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)set the cell's number within the row (0 based)
record.setColumn(num);
|
public void | setCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle style)set the style for the cell. The style should be an HSSFCellStyle created/retreived from
the HSSFWorkbook.
record.setXFIndex(style.getIndex());
|
public void | setCellType(int cellType)set the cells type (numeric, formula or string)
int row=record.getRow();
short col=record.getColumn();
short styleIndex=record.getXFIndex();
setCellType(cellType, true, row, col, styleIndex);
|
private void | setCellType(int cellType, boolean setValue, int row, short col, short styleIndex)sets the cell type. The setValue flag indicates whether to bother about
trying to preserve the current value in the new record if one is created.
The @see #setCellValue method will call this method with false in setValue
since it will overwrite the cell value later
// if (cellType == CELL_TYPE_FORMULA)
// {
// throw new RuntimeException(
// "Formulas have not been implemented in this release");
// }
if (cellType > CELL_TYPE_ERROR)
{
throw new RuntimeException("I have no idea what type that is!");
}
switch (cellType)
{
case CELL_TYPE_FORMULA :
FormulaRecordAggregate frec = null;
if (cellType != this.cellType)
{
frec = new FormulaRecordAggregate(new FormulaRecord(),null);
}
else
{
frec = ( FormulaRecordAggregate ) record;
}
frec.setColumn(col);
if (setValue)
{
frec.getFormulaRecord().setValue(getNumericCellValue());
}
frec.setXFIndex(styleIndex);
frec.setRow(row);
record = frec;
break;
case CELL_TYPE_NUMERIC :
NumberRecord nrec = null;
if (cellType != this.cellType)
{
nrec = new NumberRecord();
}
else
{
nrec = ( NumberRecord ) record;
}
nrec.setColumn(col);
if (setValue)
{
nrec.setValue(getNumericCellValue());
}
nrec.setXFIndex(styleIndex);
nrec.setRow(row);
record = nrec;
break;
case CELL_TYPE_STRING :
LabelSSTRecord lrec = null;
if (cellType != this.cellType)
{
lrec = new LabelSSTRecord();
}
else
{
lrec = ( LabelSSTRecord ) record;
}
lrec.setColumn(col);
lrec.setRow(row);
lrec.setXFIndex(styleIndex);
if (setValue)
{
if ((getStringCellValue() != null)
&& (!getStringCellValue().equals("")))
{
int sst = 0;
UnicodeString str = getRichStringCellValue().getUnicodeString();
//jmh if (encoding == ENCODING_COMPRESSED_UNICODE)
//jmh {
// jmh str.setCompressedUnicode();
// jmh } else if (encoding == ENCODING_UTF_16)
// jmh {
// jmh str.setUncompressedUnicode();
// jmh }
sst = book.addSSTString(str);
lrec.setSSTIndex(sst);
getRichStringCellValue().setUnicodeString(book.getSSTString(sst));
}
}
record = lrec;
break;
case CELL_TYPE_BLANK :
BlankRecord brec = null;
if (cellType != this.cellType)
{
brec = new BlankRecord();
}
else
{
brec = ( BlankRecord ) record;
}
brec.setColumn(col);
// During construction the cellStyle may be null for a Blank cell.
brec.setXFIndex(styleIndex);
brec.setRow(row);
record = brec;
break;
case CELL_TYPE_BOOLEAN :
BoolErrRecord boolRec = null;
if (cellType != this.cellType)
{
boolRec = new BoolErrRecord();
}
else
{
boolRec = ( BoolErrRecord ) record;
}
boolRec.setColumn(col);
if (setValue)
{
boolRec.setValue(getBooleanCellValue());
}
boolRec.setXFIndex(styleIndex);
boolRec.setRow(row);
record = boolRec;
break;
case CELL_TYPE_ERROR :
BoolErrRecord errRec = null;
if (cellType != this.cellType)
{
errRec = new BoolErrRecord();
}
else
{
errRec = ( BoolErrRecord ) record;
}
errRec.setColumn(col);
if (setValue)
{
errRec.setValue(getErrorCellValue());
}
errRec.setXFIndex(styleIndex);
errRec.setRow(row);
record = errRec;
break;
}
if (cellType != this.cellType &&
this.cellType!=-1 ) // Special Value to indicate an uninitialized Cell
{
int loc = sheet.getLoc();
sheet.replaceValueRecord(record);
sheet.setLoc(loc);
}
this.cellType = cellType;
|
public void | setCellValue(double value)set a numeric value for the cell
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);
}
(( NumberRecord ) record).setValue(value);
|
public void | setCellValue(java.util.Date value)set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
a date.
setCellValue(HSSFDateUtil.getExcelDate(value));
|
public void | setCellValue(java.util.Calendar value)set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
a date.
setCellValue(value.getTime());
|
public void | setCellValue(java.lang.String value)set a string value for the cell. Please note that if you are using
full 16 bit unicode you should call setEncoding() first.
HSSFRichTextString str = new HSSFRichTextString(value);
setCellValue(str);
|
public void | setCellValue(org.apache.poi.hssf.usermodel.HSSFRichTextString value)set a string value for the cell. Please note that if you are using
full 16 bit unicode you should call setEncoding() first.
int row=record.getRow();
short col=record.getColumn();
short styleIndex=record.getXFIndex();
if (value == null)
{
setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
}
else
{
if ((cellType != CELL_TYPE_STRING ) && ( cellType != CELL_TYPE_FORMULA))
{
setCellType(CELL_TYPE_STRING, false, row, col, styleIndex);
}
int index = 0;
UnicodeString str = value.getUnicodeString();
// jmh if (encoding == ENCODING_COMPRESSED_UNICODE)
// jmh {
// jmh str.setCompressedUnicode();
// jmh } else if (encoding == ENCODING_UTF_16)
// jmh {
// jmh str.setUncompressedUnicode();
// jmh }
index = book.addSSTString(str);
(( LabelSSTRecord ) record).setSSTIndex(index);
stringValue = value;
stringValue.setWorkbookReferences(book, (( LabelSSTRecord ) record));
stringValue.setUnicodeString(book.getSSTString(index));
}
|
public void | setCellValue(boolean value)set a boolean value for the cell
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);
|
public void | setEncoding(short encoding)set the encoding to either 8 or 16 bit. (US/UK use 8-bit, rest of the western world use 16bit)
this.encoding = encoding;
|
public java.lang.String | toString()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>
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();
}
|