2002-01-30 21:22:28 -05:00
|
|
|
/* ====================================================================
|
2006-12-22 14:18:16 -05:00
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
2004-04-09 09:05:39 -04:00
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
2004-08-23 04:52:54 -04:00
|
|
|
|
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Cell.java
|
|
|
|
*
|
|
|
|
* Created on September 30, 2001, 3:46 PM
|
|
|
|
*/
|
|
|
|
package org.apache.poi.hssf.usermodel;
|
|
|
|
|
2002-07-14 20:14:40 -04:00
|
|
|
import org.apache.poi.hssf.model.FormulaParser;
|
2004-08-23 04:52:54 -04:00
|
|
|
import org.apache.poi.hssf.model.Sheet;
|
|
|
|
import org.apache.poi.hssf.model.Workbook;
|
|
|
|
import org.apache.poi.hssf.record.*;
|
2002-08-21 07:56:49 -04:00
|
|
|
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
|
2002-04-27 10:07:53 -04:00
|
|
|
import org.apache.poi.hssf.record.formula.Ptg;
|
|
|
|
|
2005-05-20 05:13:14 -04:00
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.SimpleDateFormat;
|
2007-01-01 16:02:22 -05:00
|
|
|
import java.util.*;
|
2002-02-11 21:19:12 -05:00
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
/**
|
|
|
|
* High level representation of a cell in a row of a spreadsheet.
|
|
|
|
* Cells can be numeric, formula-based or string-based (text). The cell type
|
|
|
|
* specifies this. String cells cannot conatin numbers and numeric cells cannot
|
|
|
|
* contain strings (at least according to our model). Client apps should do the
|
2005-05-20 05:13:14 -04:00
|
|
|
* conversions themselves. Formula cells have the formula string, as well as
|
|
|
|
* the formula result, which can be numeric or string.
|
2002-03-15 07:15:59 -05:00
|
|
|
* <p>
|
2002-01-30 21:22:28 -05:00
|
|
|
* Cells should have their number (0 based) before being added to a row. Only
|
|
|
|
* cells that have values should be added.
|
2002-03-15 07:15:59 -05:00
|
|
|
* <p>
|
2002-01-30 21:22:28 -05:00
|
|
|
*
|
|
|
|
* @author Andrew C. Oliver (acoliver at apache dot org)
|
2002-11-28 14:32:52 -05:00
|
|
|
* @author Dan Sherman (dsherman at isisph.com)
|
2002-12-25 12:27:08 -05:00
|
|
|
* @author Brian Sanders (kestrel at burdell dot org) Active Cell support
|
2007-01-01 16:02:22 -05:00
|
|
|
* @author Yegor Kozlov cell comments support
|
2002-01-30 21:22:28 -05:00
|
|
|
* @version 1.0-pre
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class HSSFCell
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Numeric Cell type (0)
|
|
|
|
* @see #setCellType(int)
|
|
|
|
* @see #getCellType()
|
|
|
|
*/
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
public final static int CELL_TYPE_NUMERIC = 0;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* String Cell type (1)
|
|
|
|
* @see #setCellType(int)
|
|
|
|
* @see #getCellType()
|
|
|
|
*/
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
public final static int CELL_TYPE_STRING = 1;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Formula Cell type (2)
|
|
|
|
* @see #setCellType(int)
|
|
|
|
* @see #getCellType()
|
|
|
|
*/
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
public final static int CELL_TYPE_FORMULA = 2;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Blank Cell type (3)
|
|
|
|
* @see #setCellType(int)
|
|
|
|
* @see #getCellType()
|
|
|
|
*/
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
public final static int CELL_TYPE_BLANK = 3;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Boolean Cell type (4)
|
|
|
|
* @see #setCellType(int)
|
|
|
|
* @see #getCellType()
|
|
|
|
*/
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
public final static int CELL_TYPE_BOOLEAN = 4;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Error Cell type (5)
|
|
|
|
* @see #setCellType(int)
|
|
|
|
* @see #getCellType()
|
|
|
|
*/
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
public final static int CELL_TYPE_ERROR = 5;
|
2005-09-01 23:48:31 -04:00
|
|
|
public final static short ENCODING_UNCHANGED = -1;
|
2002-01-30 21:22:28 -05:00
|
|
|
public final static short ENCODING_COMPRESSED_UNICODE = 0;
|
|
|
|
public final static short ENCODING_UTF_16 = 1;
|
|
|
|
private int cellType;
|
2005-08-18 03:06:44 -04:00
|
|
|
private HSSFRichTextString stringValue;
|
2005-09-01 23:48:31 -04:00
|
|
|
private short encoding = ENCODING_UNCHANGED;
|
2002-01-30 21:22:28 -05:00
|
|
|
private Workbook book;
|
|
|
|
private Sheet sheet;
|
2004-08-23 04:52:54 -04:00
|
|
|
private CellValueRecordInterface record;
|
2007-01-01 16:02:22 -05:00
|
|
|
private HSSFComment comment;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates new Cell - Should only be called by HSSFRow. This creates a cell
|
|
|
|
* from scratch.
|
|
|
|
* <p>
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param book - Workbook record of the workbook containing this cell
|
|
|
|
* @param sheet - Sheet record of the sheet containing this cell
|
|
|
|
* @param row - the row of this cell
|
|
|
|
* @param col - the column for this cell
|
|
|
|
*
|
|
|
|
* @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(short)
|
|
|
|
*/
|
|
|
|
|
2002-05-09 23:01:11 -04:00
|
|
|
//protected HSSFCell(Workbook book, Sheet sheet, short row, short col)
|
|
|
|
protected HSSFCell(Workbook book, Sheet sheet, int row, short col)
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
2002-08-01 14:18:16 -04:00
|
|
|
checkBounds(col);
|
2002-01-30 21:22:28 -05:00
|
|
|
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.
|
2005-07-20 10:12:24 -04:00
|
|
|
short xfindex = sheet.getXFIndexForColAt(col);
|
2006-01-03 02:24:01 -05:00
|
|
|
setCellType(CELL_TYPE_BLANK, false, row, col,xfindex);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates new Cell - Should only be called by HSSFRow. This creates a cell
|
|
|
|
* from scratch.
|
|
|
|
*
|
|
|
|
* @param book - Workbook record of the workbook containing this cell
|
|
|
|
* @param sheet - Sheet record of the sheet containing this cell
|
|
|
|
* @param row - the row of this cell
|
|
|
|
* @param col - the column for this cell
|
|
|
|
* @param type - CELL_TYPE_NUMERIC, CELL_TYPE_STRING, CELL_TYPE_FORMULA, CELL_TYPE_BLANK,
|
|
|
|
* CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR
|
|
|
|
* Type of cell
|
|
|
|
* @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(short,int)
|
|
|
|
*/
|
|
|
|
|
2002-05-09 23:01:11 -04:00
|
|
|
//protected HSSFCell(Workbook book, Sheet sheet, short row, short col,
|
|
|
|
protected HSSFCell(Workbook book, Sheet sheet, int row, short col,
|
2002-01-30 21:22:28 -05:00
|
|
|
int type)
|
|
|
|
{
|
2002-08-01 14:18:16 -04:00
|
|
|
checkBounds(col);
|
2006-01-03 02:24:01 -05:00
|
|
|
cellType = -1; // Force 'setCellType' to create a first Record
|
2002-01-30 21:22:28 -05:00
|
|
|
stringValue = null;
|
|
|
|
this.book = book;
|
|
|
|
this.sheet = sheet;
|
2006-01-03 02:24:01 -05:00
|
|
|
|
|
|
|
short xfindex = sheet.getXFIndexForColAt(col);
|
|
|
|
setCellType(type,false,row,col,xfindex);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an HSSFCell from a CellValueRecordInterface. HSSFSheet uses this when
|
|
|
|
* reading in cells from an existing sheet.
|
|
|
|
*
|
|
|
|
* @param book - Workbook record of the workbook containing this cell
|
|
|
|
* @param sheet - Sheet record of the sheet containing this cell
|
|
|
|
* @param cval - the Cell Value Record we wish to represent
|
|
|
|
*/
|
|
|
|
|
2002-05-09 23:01:11 -04:00
|
|
|
//protected HSSFCell(Workbook book, Sheet sheet, short row,
|
|
|
|
protected HSSFCell(Workbook book, Sheet sheet, int row,
|
2002-01-30 21:22:28 -05:00
|
|
|
CellValueRecordInterface cval)
|
|
|
|
{
|
2004-08-23 04:52:54 -04:00
|
|
|
record = cval;
|
2002-01-30 21:22:28 -05:00
|
|
|
cellType = determineType(cval);
|
|
|
|
stringValue = null;
|
|
|
|
this.book = book;
|
|
|
|
this.sheet = sheet;
|
|
|
|
switch (cellType)
|
|
|
|
{
|
|
|
|
case CELL_TYPE_STRING :
|
2005-08-18 03:06:44 -04:00
|
|
|
stringValue = new HSSFRichTextString(book, (LabelSSTRecord ) cval);
|
2002-01-30 21:22:28 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CELL_TYPE_BLANK :
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CELL_TYPE_FORMULA :
|
2005-08-18 03:06:44 -04:00
|
|
|
stringValue=new HSSFRichTextString(((FormulaRecordAggregate) cval).getStringValue());
|
2002-01-30 21:22:28 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
ExtendedFormatRecord xf = book.getExFormatAt(cval.getXFIndex());
|
|
|
|
|
|
|
|
setCellStyle(new HSSFCellStyle(( short ) cval.getXFIndex(), xf));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* private constructor to prevent blank construction
|
|
|
|
*/
|
|
|
|
private HSSFCell()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* used internally -- given a cell value record, figure out its type
|
|
|
|
*/
|
|
|
|
private int determineType(CellValueRecordInterface cval)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2002-08-21 07:56:49 -04:00
|
|
|
case FormulaRecordAggregate.sid :
|
2002-01-30 21:22:28 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the cell's number within the row (0 based)
|
2002-02-11 05:45:51 -05:00
|
|
|
* @param num short the cell number
|
2002-01-30 21:22:28 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
public void setCellNum(short num)
|
|
|
|
{
|
2004-08-23 04:52:54 -04:00
|
|
|
record.setColumn(num);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the cell's number within the row
|
|
|
|
* @return short reperesenting the column number (logical!)
|
|
|
|
*/
|
|
|
|
|
|
|
|
public short getCellNum()
|
|
|
|
{
|
2006-01-03 02:24:01 -05:00
|
|
|
return record.getColumn();
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-02-17 00:35:50 -05:00
|
|
|
* set the cells type (numeric, formula or string)
|
2002-01-30 21:22:28 -05:00
|
|
|
* @see #CELL_TYPE_NUMERIC
|
|
|
|
* @see #CELL_TYPE_STRING
|
|
|
|
* @see #CELL_TYPE_FORMULA
|
|
|
|
* @see #CELL_TYPE_BLANK
|
|
|
|
* @see #CELL_TYPE_BOOLEAN
|
|
|
|
* @see #CELL_TYPE_ERROR
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void setCellType(int cellType)
|
|
|
|
{
|
2006-01-03 02:24:01 -05:00
|
|
|
int row=record.getRow();
|
|
|
|
short col=record.getColumn();
|
|
|
|
short styleIndex=record.getXFIndex();
|
|
|
|
setCellType(cellType, true, row, col, styleIndex);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* <p>
|
|
|
|
* The @see #setCellValue method will call this method with false in setValue
|
|
|
|
* since it will overwrite the cell value later
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-01-03 02:24:01 -05:00
|
|
|
private void setCellType(int cellType, boolean setValue, int row,short col, short styleIndex)
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
// 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 :
|
2002-08-21 07:56:49 -04:00
|
|
|
FormulaRecordAggregate frec = null;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
if (cellType != this.cellType)
|
|
|
|
{
|
2002-08-21 07:56:49 -04:00
|
|
|
frec = new FormulaRecordAggregate(new FormulaRecord(),null);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-08-23 04:52:54 -04:00
|
|
|
frec = ( FormulaRecordAggregate ) record;
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2006-01-03 02:24:01 -05:00
|
|
|
frec.setColumn(col);
|
2002-01-30 21:22:28 -05:00
|
|
|
if (setValue)
|
|
|
|
{
|
2002-08-21 07:56:49 -04:00
|
|
|
frec.getFormulaRecord().setValue(getNumericCellValue());
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2006-01-03 02:24:01 -05:00
|
|
|
frec.setXFIndex(styleIndex);
|
2002-01-30 21:22:28 -05:00
|
|
|
frec.setRow(row);
|
2004-08-23 04:52:54 -04:00
|
|
|
record = frec;
|
2002-01-30 21:22:28 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CELL_TYPE_NUMERIC :
|
|
|
|
NumberRecord nrec = null;
|
|
|
|
|
|
|
|
if (cellType != this.cellType)
|
|
|
|
{
|
|
|
|
nrec = new NumberRecord();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-08-23 04:52:54 -04:00
|
|
|
nrec = ( NumberRecord ) record;
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2006-01-03 02:24:01 -05:00
|
|
|
nrec.setColumn(col);
|
2002-01-30 21:22:28 -05:00
|
|
|
if (setValue)
|
|
|
|
{
|
|
|
|
nrec.setValue(getNumericCellValue());
|
|
|
|
}
|
2006-01-03 02:24:01 -05:00
|
|
|
nrec.setXFIndex(styleIndex);
|
2002-01-30 21:22:28 -05:00
|
|
|
nrec.setRow(row);
|
2004-08-23 04:52:54 -04:00
|
|
|
record = nrec;
|
2002-01-30 21:22:28 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CELL_TYPE_STRING :
|
|
|
|
LabelSSTRecord lrec = null;
|
|
|
|
|
|
|
|
if (cellType != this.cellType)
|
|
|
|
{
|
|
|
|
lrec = new LabelSSTRecord();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-08-23 04:52:54 -04:00
|
|
|
lrec = ( LabelSSTRecord ) record;
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2006-01-03 02:24:01 -05:00
|
|
|
lrec.setColumn(col);
|
2002-01-30 21:22:28 -05:00
|
|
|
lrec.setRow(row);
|
2006-01-03 02:24:01 -05:00
|
|
|
lrec.setXFIndex(styleIndex);
|
2002-01-30 21:22:28 -05:00
|
|
|
if (setValue)
|
|
|
|
{
|
|
|
|
if ((getStringCellValue() != null)
|
|
|
|
&& (!getStringCellValue().equals("")))
|
|
|
|
{
|
|
|
|
int sst = 0;
|
|
|
|
|
2005-09-01 23:48:31 -04:00
|
|
|
UnicodeString str = getRichStringCellValue().getUnicodeString();
|
2006-01-03 06:41:36 -05:00
|
|
|
//jmh if (encoding == ENCODING_COMPRESSED_UNICODE)
|
|
|
|
//jmh {
|
|
|
|
// jmh str.setCompressedUnicode();
|
|
|
|
// jmh } else if (encoding == ENCODING_UTF_16)
|
|
|
|
// jmh {
|
|
|
|
// jmh str.setUncompressedUnicode();
|
|
|
|
// jmh }
|
2005-09-01 23:48:31 -04:00
|
|
|
sst = book.addSSTString(str);
|
2002-01-30 21:22:28 -05:00
|
|
|
lrec.setSSTIndex(sst);
|
2005-09-01 23:48:31 -04:00
|
|
|
getRichStringCellValue().setUnicodeString(book.getSSTString(sst));
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
}
|
2004-08-23 04:52:54 -04:00
|
|
|
record = lrec;
|
2002-01-30 21:22:28 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CELL_TYPE_BLANK :
|
|
|
|
BlankRecord brec = null;
|
|
|
|
|
|
|
|
if (cellType != this.cellType)
|
|
|
|
{
|
|
|
|
brec = new BlankRecord();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-08-23 04:52:54 -04:00
|
|
|
brec = ( BlankRecord ) record;
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2006-01-03 02:24:01 -05:00
|
|
|
brec.setColumn(col);
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
// During construction the cellStyle may be null for a Blank cell.
|
2006-01-03 02:24:01 -05:00
|
|
|
brec.setXFIndex(styleIndex);
|
2002-01-30 21:22:28 -05:00
|
|
|
brec.setRow(row);
|
2004-08-23 04:52:54 -04:00
|
|
|
record = brec;
|
2002-01-30 21:22:28 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CELL_TYPE_BOOLEAN :
|
|
|
|
BoolErrRecord boolRec = null;
|
|
|
|
|
|
|
|
if (cellType != this.cellType)
|
|
|
|
{
|
|
|
|
boolRec = new BoolErrRecord();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-08-23 04:52:54 -04:00
|
|
|
boolRec = ( BoolErrRecord ) record;
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2006-01-03 02:24:01 -05:00
|
|
|
boolRec.setColumn(col);
|
2002-01-30 21:22:28 -05:00
|
|
|
if (setValue)
|
|
|
|
{
|
|
|
|
boolRec.setValue(getBooleanCellValue());
|
|
|
|
}
|
2006-01-03 02:24:01 -05:00
|
|
|
boolRec.setXFIndex(styleIndex);
|
2002-01-30 21:22:28 -05:00
|
|
|
boolRec.setRow(row);
|
2004-08-23 04:52:54 -04:00
|
|
|
record = boolRec;
|
2002-01-30 21:22:28 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CELL_TYPE_ERROR :
|
|
|
|
BoolErrRecord errRec = null;
|
|
|
|
|
|
|
|
if (cellType != this.cellType)
|
|
|
|
{
|
|
|
|
errRec = new BoolErrRecord();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-08-23 04:52:54 -04:00
|
|
|
errRec = ( BoolErrRecord ) record;
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2006-01-03 02:24:01 -05:00
|
|
|
errRec.setColumn(col);
|
2002-01-30 21:22:28 -05:00
|
|
|
if (setValue)
|
|
|
|
{
|
|
|
|
errRec.setValue(getErrorCellValue());
|
|
|
|
}
|
2006-01-03 02:24:01 -05:00
|
|
|
errRec.setXFIndex(styleIndex);
|
2002-01-30 21:22:28 -05:00
|
|
|
errRec.setRow(row);
|
2004-08-23 04:52:54 -04:00
|
|
|
record = errRec;
|
2002-01-30 21:22:28 -05:00
|
|
|
break;
|
|
|
|
}
|
2006-01-03 02:24:01 -05:00
|
|
|
if (cellType != this.cellType &&
|
|
|
|
this.cellType!=-1 ) // Special Value to indicate an uninitialized Cell
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
|
|
|
int loc = sheet.getLoc();
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
sheet.replaceValueRecord(record);
|
2002-01-30 21:22:28 -05:00
|
|
|
sheet.setLoc(loc);
|
|
|
|
}
|
|
|
|
this.cellType = cellType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the cells type (numeric, formula or string)
|
|
|
|
* @see #CELL_TYPE_STRING
|
|
|
|
* @see #CELL_TYPE_NUMERIC
|
|
|
|
* @see #CELL_TYPE_FORMULA
|
|
|
|
* @see #CELL_TYPE_BOOLEAN
|
|
|
|
* @see #CELL_TYPE_ERROR
|
|
|
|
*/
|
|
|
|
|
|
|
|
public int getCellType()
|
|
|
|
{
|
|
|
|
return cellType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set a numeric value for the cell
|
|
|
|
*
|
2002-02-11 05:45:51 -05:00
|
|
|
* @param value the numeric value to set this cell to. For formulas we'll set the
|
2002-01-30 21:22:28 -05:00
|
|
|
* precalculated value, for numerics we'll set its value. For other types we
|
|
|
|
* will change the cell to a numeric cell and set its value.
|
|
|
|
*/
|
|
|
|
public void setCellValue(double value)
|
|
|
|
{
|
2006-01-03 02:24:01 -05:00
|
|
|
int row=record.getRow();
|
|
|
|
short col=record.getColumn();
|
|
|
|
short styleIndex=record.getXFIndex();
|
2002-02-11 21:19:12 -05:00
|
|
|
if ((cellType != CELL_TYPE_NUMERIC) && (cellType != CELL_TYPE_FORMULA))
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
2006-01-03 02:24:01 -05:00
|
|
|
setCellType(CELL_TYPE_NUMERIC, false, row, col, styleIndex);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2004-08-23 04:52:54 -04:00
|
|
|
(( NumberRecord ) record).setValue(value);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
2002-02-11 21:19:12 -05:00
|
|
|
/**
|
|
|
|
* set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
|
|
|
|
* a date.
|
|
|
|
*
|
|
|
|
* @param value the date value to set this cell to. For formulas we'll set the
|
|
|
|
* precalculated value, for numerics we'll set its value. For other types we
|
|
|
|
* will change the cell to a numeric cell and set its value.
|
|
|
|
*/
|
|
|
|
public void setCellValue(Date value)
|
|
|
|
{
|
|
|
|
setCellValue(HSSFDateUtil.getExcelDate(value));
|
|
|
|
}
|
|
|
|
|
2002-03-14 21:47:56 -05:00
|
|
|
/**
|
|
|
|
* set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
|
|
|
|
* a date.
|
|
|
|
*
|
|
|
|
* @param value the date value to set this cell to. For formulas we'll set the
|
|
|
|
* precalculated value, for numerics we'll set its value. For othertypes we
|
|
|
|
* will change the cell to a numeric cell and set its value.
|
|
|
|
*/
|
|
|
|
public void setCellValue(Calendar value)
|
|
|
|
{
|
|
|
|
setCellValue(value.getTime());
|
|
|
|
}
|
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
/**
|
2002-07-05 01:30:30 -04:00
|
|
|
* 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.
|
2002-01-30 21:22:28 -05:00
|
|
|
*
|
2002-02-11 05:45:51 -05:00
|
|
|
* @param value value to set the cell to. For formulas we'll set the formula
|
2002-01-30 21:22:28 -05:00
|
|
|
* 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.
|
2005-08-18 03:06:44 -04:00
|
|
|
* @deprecated Use setCellValue(HSSFRichTextString) instead.
|
2002-01-30 21:22:28 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
public void setCellValue(String value)
|
2005-08-18 03:06:44 -04:00
|
|
|
{
|
|
|
|
HSSFRichTextString str = new HSSFRichTextString(value);
|
|
|
|
setCellValue(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(HSSFRichTextString value)
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
2006-01-03 02:24:01 -05:00
|
|
|
int row=record.getRow();
|
|
|
|
short col=record.getColumn();
|
|
|
|
short styleIndex=record.getXFIndex();
|
2002-01-30 21:22:28 -05:00
|
|
|
if (value == null)
|
|
|
|
{
|
2006-01-03 02:24:01 -05:00
|
|
|
setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-08-21 07:56:49 -04:00
|
|
|
if ((cellType != CELL_TYPE_STRING ) && ( cellType != CELL_TYPE_FORMULA))
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
2006-01-03 02:24:01 -05:00
|
|
|
setCellType(CELL_TYPE_STRING, false, row, col, styleIndex);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
int index = 0;
|
|
|
|
|
2005-09-01 23:48:31 -04:00
|
|
|
UnicodeString str = value.getUnicodeString();
|
2006-01-03 06:41:36 -05:00
|
|
|
// jmh if (encoding == ENCODING_COMPRESSED_UNICODE)
|
|
|
|
// jmh {
|
|
|
|
// jmh str.setCompressedUnicode();
|
|
|
|
// jmh } else if (encoding == ENCODING_UTF_16)
|
|
|
|
// jmh {
|
|
|
|
// jmh str.setUncompressedUnicode();
|
|
|
|
// jmh }
|
2005-09-01 23:48:31 -04:00
|
|
|
index = book.addSSTString(str);
|
2004-08-23 04:52:54 -04:00
|
|
|
(( LabelSSTRecord ) record).setSSTIndex(index);
|
2002-01-30 21:22:28 -05:00
|
|
|
stringValue = value;
|
2005-08-18 03:06:44 -04:00
|
|
|
stringValue.setWorkbookReferences(book, (( LabelSSTRecord ) record));
|
2005-09-01 23:48:31 -04:00
|
|
|
stringValue.setUnicodeString(book.getSSTString(index));
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-27 10:07:53 -04:00
|
|
|
public void setCellFormula(String formula) {
|
2006-01-03 02:24:01 -05:00
|
|
|
int row=record.getRow();
|
|
|
|
short col=record.getColumn();
|
|
|
|
short styleIndex=record.getXFIndex();
|
2002-07-14 20:14:40 -04:00
|
|
|
//Workbook.currentBook=book;
|
2002-04-27 10:07:53 -04:00
|
|
|
if (formula==null) {
|
2006-01-03 02:24:01 -05:00
|
|
|
setCellType(CELL_TYPE_BLANK,false,row,col,styleIndex);
|
2002-04-27 10:07:53 -04:00
|
|
|
} else {
|
2006-01-03 02:24:01 -05:00
|
|
|
setCellType(CELL_TYPE_FORMULA,false,row,col,styleIndex);
|
2004-08-23 04:52:54 -04:00
|
|
|
FormulaRecordAggregate rec = (FormulaRecordAggregate) record;
|
2006-08-09 16:26:40 -04:00
|
|
|
FormulaRecord frec = rec.getFormulaRecord();
|
|
|
|
frec.setOptions(( short ) 2);
|
|
|
|
frec.setValue(0);
|
2004-08-23 04:52:54 -04:00
|
|
|
|
|
|
|
//only set to default if there is no extended format index already set
|
|
|
|
if (rec.getXFIndex() == (short)0) rec.setXFIndex(( short ) 0x0f);
|
2002-06-25 04:35:16 -04:00
|
|
|
FormulaParser fp = new FormulaParser(formula+";",book);
|
2002-04-27 10:07:53 -04:00
|
|
|
fp.parse();
|
|
|
|
Ptg[] ptg = fp.getRPNPtg();
|
|
|
|
int size = 0;
|
2006-08-09 16:26:40 -04:00
|
|
|
|
|
|
|
// 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
|
2002-04-27 10:07:53 -04:00
|
|
|
for (int k = 0; k < ptg.length; k++) {
|
|
|
|
size += ptg[ k ].getSize();
|
2006-08-09 16:26:40 -04:00
|
|
|
frec.pushExpressionToken(ptg[ k ]);
|
2002-04-27 10:07:53 -04:00
|
|
|
}
|
2002-08-21 07:56:49 -04:00
|
|
|
rec.getFormulaRecord().setExpressionLength(( short ) size);
|
2002-07-14 20:14:40 -04:00
|
|
|
//Workbook.currentBook = null;
|
2002-04-27 10:07:53 -04:00
|
|
|
}
|
|
|
|
}
|
2002-09-11 08:14:28 -04:00
|
|
|
|
2002-04-27 22:04:58 -04:00
|
|
|
public String getCellFormula() {
|
2002-07-14 20:14:40 -04:00
|
|
|
//Workbook.currentBook=book;
|
2004-08-23 04:52:54 -04:00
|
|
|
String retval = FormulaParser.toFormulaString(book, ((FormulaRecordAggregate)record).getFormulaRecord().getParsedExpression());
|
2002-07-14 20:14:40 -04:00
|
|
|
//Workbook.currentBook=null;
|
2002-09-11 08:14:28 -04:00
|
|
|
return retval;
|
2002-04-27 22:04:58 -04:00
|
|
|
}
|
2002-09-11 08:14:28 -04:00
|
|
|
|
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
/**
|
|
|
|
* 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");
|
|
|
|
}
|
2006-01-03 02:24:01 -05:00
|
|
|
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);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
2002-02-11 21:19:12 -05:00
|
|
|
/**
|
|
|
|
* 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");
|
|
|
|
}
|
2006-01-03 02:24:01 -05:00
|
|
|
double value=this.getNumericCellValue();
|
2002-11-28 14:32:52 -05:00
|
|
|
if (book.isUsing1904DateWindowing()) {
|
2006-01-03 02:24:01 -05:00
|
|
|
return HSSFDateUtil.getJavaDate(value,true);
|
2002-11-28 14:32:52 -05:00
|
|
|
}
|
|
|
|
else {
|
2006-01-03 02:24:01 -05:00
|
|
|
return HSSFDateUtil.getJavaDate(value,false);
|
2002-11-28 14:32:52 -05:00
|
|
|
}
|
2002-02-11 21:19:12 -05:00
|
|
|
}
|
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
/**
|
|
|
|
* get the value of the cell as a string - for numeric cells we throw an exception.
|
|
|
|
* For blank cells we return an empty string.
|
2003-08-31 02:19:41 -04:00
|
|
|
* For formulaCells that are not string Formulas, we return empty String
|
2005-08-18 03:06:44 -04:00
|
|
|
* @deprecated Use the HSSFRichTextString return
|
2002-01-30 21:22:28 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
public String getStringCellValue()
|
2005-08-18 03:06:44 -04:00
|
|
|
{
|
|
|
|
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()
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
|
|
|
if (cellType == CELL_TYPE_BLANK)
|
|
|
|
{
|
2005-08-18 03:06:44 -04:00
|
|
|
return new HSSFRichTextString("");
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
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");
|
|
|
|
}
|
2003-08-31 02:19:41 -04:00
|
|
|
if (cellType == CELL_TYPE_FORMULA)
|
|
|
|
{
|
2005-08-18 03:06:44 -04:00
|
|
|
if (stringValue==null) return new HSSFRichTextString("");
|
2003-08-31 02:19:41 -04:00
|
|
|
}
|
2002-01-30 21:22:28 -05:00
|
|
|
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)
|
|
|
|
{
|
2006-01-03 02:24:01 -05:00
|
|
|
int row=record.getRow();
|
|
|
|
short col=record.getColumn();
|
|
|
|
short styleIndex=record.getXFIndex();
|
2004-08-23 04:52:54 -04:00
|
|
|
if ((cellType != CELL_TYPE_BOOLEAN ) && ( cellType != CELL_TYPE_FORMULA))
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
2006-01-03 02:24:01 -05:00
|
|
|
setCellType(CELL_TYPE_BOOLEAN, false, row, col, styleIndex);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
(( 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)
|
|
|
|
{
|
2006-01-03 02:24:01 -05:00
|
|
|
int row=record.getRow();
|
|
|
|
short col=record.getColumn();
|
|
|
|
short styleIndex=record.getXFIndex();
|
2004-08-23 04:52:54 -04:00
|
|
|
if ((cellType != CELL_TYPE_ERROR) && (cellType != CELL_TYPE_FORMULA))
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
2006-01-03 02:24:01 -05:00
|
|
|
setCellType(CELL_TYPE_ERROR, false, row, col, styleIndex);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2004-08-23 04:52:54 -04:00
|
|
|
(( BoolErrRecord ) record).setValue(value);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2006-01-03 02:24:01 -05:00
|
|
|
return (( BoolErrRecord ) record).getBooleanValue();
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2006-01-03 02:24:01 -05:00
|
|
|
return (( BoolErrRecord ) record).getErrorValue();
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
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.
|
|
|
|
*
|
2002-02-11 05:45:51 -05:00
|
|
|
* @param style reference contained in the workbook
|
2002-01-30 21:22:28 -05:00
|
|
|
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createCellStyle()
|
|
|
|
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short)
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void setCellStyle(HSSFCellStyle style)
|
|
|
|
{
|
2004-08-23 04:52:54 -04:00
|
|
|
record.setXFIndex(style.getIndex());
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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()
|
|
|
|
{
|
2006-01-03 02:24:01 -05:00
|
|
|
short styleIndex=record.getXFIndex();
|
|
|
|
ExtendedFormatRecord xf = book.getExFormatAt(styleIndex);
|
|
|
|
return new HSSFCellStyle(styleIndex, xf);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-09-01 23:48:31 -04:00
|
|
|
* used for internationalization, currently -1 for unchanged, 0 for compressed unicode or 1 for 16-bit
|
2002-01-30 21:22:28 -05:00
|
|
|
*
|
2005-09-01 23:48:31 -04:00
|
|
|
* @see #ENCODING_UNCHANGED
|
2002-01-30 21:22:28 -05:00
|
|
|
* @see #ENCODING_COMPRESSED_UNICODE
|
|
|
|
* @see #ENCODING_UTF_16
|
|
|
|
*
|
2005-09-01 23:48:31 -04:00
|
|
|
* @return -1, 1 or 0 for unchanged, compressed or uncompressed (used only with String type)
|
2006-01-03 06:41:36 -05:00
|
|
|
*
|
|
|
|
* @deprecated As of 3-Jan-06 POI now automatically handles Unicode without forcing the encoding.
|
2002-01-30 21:22:28 -05:00
|
|
|
*/
|
|
|
|
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)
|
|
|
|
*
|
2005-09-01 23:48:31 -04:00
|
|
|
* @see #ENCODING_UNCHANGED
|
2002-01-30 21:22:28 -05:00
|
|
|
* @see #ENCODING_COMPRESSED_UNICODE
|
|
|
|
* @see #ENCODING_UTF_16
|
|
|
|
*
|
|
|
|
* @param encoding either ENCODING_COMPRESSED_UNICODE (0) or ENCODING_UTF_16 (1)
|
2006-01-03 06:41:36 -05:00
|
|
|
* @deprecated As of 3-Jan-06 POI now automatically handles Unicode without forcing the encoding.
|
2002-01-30 21:22:28 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2004-08-23 04:52:54 -04:00
|
|
|
return record;
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2002-09-11 08:14:28 -04:00
|
|
|
|
2002-08-01 14:18:16 -04:00
|
|
|
/**
|
|
|
|
* @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");
|
2002-09-11 08:14:28 -04:00
|
|
|
}
|
|
|
|
else if (cellNum < 0) {
|
|
|
|
throw new RuntimeException("You cannot reference columns with an index of less then 0.");
|
|
|
|
}
|
2002-08-01 14:18:16 -04:00
|
|
|
}
|
2002-12-25 12:27:08 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets this cell as the active cell for the worksheet
|
|
|
|
*/
|
|
|
|
public void setAsActiveCell()
|
|
|
|
{
|
2006-01-03 02:24:01 -05:00
|
|
|
int row=record.getRow();
|
|
|
|
short col=record.getColumn();
|
|
|
|
this.sheet.setActiveCellRow(row);
|
|
|
|
this.sheet.setActiveCellCol(col);
|
2002-12-25 12:27:08 -05:00
|
|
|
}
|
2005-05-20 05:13:14 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
}
|
2007-01-01 16:02:22 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-03-12 11:43:14 -04:00
|
|
|
* Returns comment associated with this cell
|
2007-01-01 16:02:22 -05:00
|
|
|
*
|
|
|
|
* @return comment associated with this cell
|
|
|
|
*/
|
|
|
|
public HSSFComment getCellComment(){
|
|
|
|
if (comment == null) {
|
2007-03-12 11:43:14 -04:00
|
|
|
comment = findCellComment(sheet, record.getRow(), record.getColumn());
|
2007-01-01 16:02:22 -05:00
|
|
|
}
|
|
|
|
return comment;
|
|
|
|
}
|
2007-03-12 11:43:14 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|