poi/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java

528 lines
19 KiB
Java
Raw Normal View History

/* ====================================================================
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
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.
==================================================================== */
package org.apache.poi.xssf.usermodel;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.poi.hssf.record.formula.eval.ErrorEval;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.StylesSource;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.xssf.model.SharedStringSource;
import org.apache.poi.xssf.model.StylesTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
/**
*
*/
public final class XSSFCell implements Cell {
private static final String FALSE_AS_STRING = "0";
private static final String TRUE_AS_STRING = "1";
private final CTCell cell;
private final XSSFRow row;
private int cellNum;
private SharedStringSource sharedStringSource;
private StylesTable stylesSource;
private POILogger logger = POILogFactory.getLogger(XSSFCell.class);
/**
* Create a new XSSFCell. This method is protected to be used only by
* tests.
*/
protected XSSFCell(XSSFRow row) {
this(row, CTCell.Factory.newInstance());
}
public XSSFCell(XSSFRow row, CTCell cell) {
this.cell = cell;
this.row = row;
if (cell.getR() != null) {
this.cellNum = parseCellNum(cell.getR());
}
this.sharedStringSource = row.getSheet().getWorkbook().getSharedStringSource();
this.stylesSource = (StylesTable)row.getSheet().getWorkbook().getStylesSource();
}
protected SharedStringSource getSharedStringSource() {
return this.sharedStringSource;
}
protected StylesSource getStylesSource() {
return this.stylesSource;
}
public Sheet getSheet() {
return this.row.getSheet();
}
public boolean getBooleanCellValue() {
if (STCellType.B != cell.getT()) {
throw new NumberFormatException("You cannot get a boolean value from a non-boolean cell");
}
if (cell.isSetV()) {
return (TRUE_AS_STRING.equals(this.cell.getV()));
}
return false;
}
public Comment getCellComment() {
return row.getSheet().getCellComment(row.getRowNum(), getCellNum());
}
public String getCellFormula() {
if(this.cell.getF() == null) {
throw new NumberFormatException("You cannot get a formula from a non-formula cell");
}
return this.cell.getF().getStringValue();
}
public short getCellNum() {
return (short)this.cellNum;
}
Merged revisions 638786-638802,638805-638811,638813-638814,638816-639230,639233-639241,639243-639253,639255-639486,639488-639601,639603-639835,639837-639917,639919-640056,640058-640710,640712-641156,641158-641184,641186-641795,641797-641798,641800-641933,641935-641963,641965-641966,641968-641995,641997-642230,642232-642562,642564-642565,642568-642570,642572-642573,642576-642736,642739-642877,642879,642881-642890,642892-642903,642905-642945,642947-643624,643626-643653,643655-643669,643671,643673-643830,643832-643833,643835-644342,644344-644472,644474-644508,644510-645347,645349-645351,645353-645559,645561-645565,645568-645951,645953-646193,646195-646311,646313-646404,646406-646665,646667-646853,646855-646869,646871-647151,647153-647185,647187-647277,647279-647566,647568-647573,647575,647578-647711,647714-647737,647739-647823,647825-648155,648157-648202,648204-648273,648275,648277-648302,648304-648333,648335-648588,648590-648622,648625-648673,648675-649141,649144,649146-649556,649558-649795,649799,649801-649910,649912-649913,649915-650128,650131-650132,650134-650137,650140-650914,650916-651991,651993-652284,652286-652287,652289,652291,652293-652297,652299-652328,652330-652425,652427-652445,652447-652560,652562-652933,652935,652937-652993,652995-653116,653118-653124,653126-653483,653487-653519,653522-653550,653552-653607,653609-653667,653669-653674,653676-653814,653817-653830,653832-653891,653893-653944,653946-654055,654057-654355,654357-654365,654367-654648,654651-655215,655217-655277,655279-655281,655283-655911,655913-656212,656214,656216-656251,656253-656698,656700-656756,656758-656892,656894-657135,657137-657165,657168-657179,657181-657354,657356-657357,657359-657701,657703-657874,657876-658032,658034-658284,658286,658288-658301,658303-658307,658309-658321,658323-658335,658337-658348,658351,658353-658832,658834-658983,658985,658987-659066,659068-659402,659404-659428,659430-659451,659453-659454,659456-659461,659463-659477,659479-659524,659526-659571,659574,659576-660255,660257-660262,660264-660279,660281-660343,660345-660473,660475-660827,660829-660833,660835-660888,660890-663321,663323-663435,663437-663764,663766-663854,663856-664219,664221-664489,664494-664514,664516-668013,668015-668142,668144-668152,668154,668156-668256,668258,668260-669139,669141-669455,669457-669657,669659-669808,669810-670189,670191-671321,671323-672229,672231-672549,672551-672552,672554-672561,672563-672566,672568,672571-673049,673051-673852,673854-673862,673864-673986,673988-673996,673998-674347,674349-674890,674892-674910,674912-674936,674938-674952,674954-675078,675080-675085,675087-675217,675219-675660,675662-675670,675672-675716,675718-675726,675728-675733,675735-675775,675777-675782,675784,675786-675791,675794-675852,675854-676200,676202,676204,676206-676220,676222-676309,676311-676456,676458-676994,676996-677027,677030-677040,677042-677056,677058-677375,677377-677968,677970-677971,677973,677975-677994,677996-678286,678288-678538,678540-680393,680395-680469,680471-680529,680531-680852,680854-681529,681531-681571,681573-682224,682226,682228,682231-682281,682283-682335,682337-682507,682509,682512-682517,682519-682532,682534-682619,682622-682777,682779-682998,683000-683019,683021-683022,683024-683080,683082-683092,683094-683095,683097-683127,683129-683131,683133-683166,683168-683698,683700-683705,683707-683757,683759-683787,683789-683870,683872-683879,683881-683900,683902-684066,684068-684074,684076-684222,684224-684254,684257-684370 via svnmerge from https://svn.apache.org/repos/asf/poi/trunk ........ r684282 | nick | 2008-08-09 16:58:24 +0100 (Sat, 09 Aug 2008) | 1 line Prepare to change how we do ranges, to handle different kinds of text in the cp area ........ r684287 | nick | 2008-08-09 17:24:20 +0100 (Sat, 09 Aug 2008) | 1 line Start to document the whole FIB stuff better ........ r684293 | nick | 2008-08-09 17:46:39 +0100 (Sat, 09 Aug 2008) | 1 line Add lots more getters/setters for useful things to the FIB ........ r684299 | nick | 2008-08-09 18:23:42 +0100 (Sat, 09 Aug 2008) | 1 line More tests to show that the range based stuff is working properly ........ r684302 | nick | 2008-08-09 18:33:29 +0100 (Sat, 09 Aug 2008) | 1 line More header and footer files, this time with unicode in it too ........ r684309 | nick | 2008-08-09 18:58:35 +0100 (Sat, 09 Aug 2008) | 1 line More range tests, which show that we do have a bug in the hwpf unicode support ........ r684318 | josh | 2008-08-09 20:29:23 +0100 (Sat, 09 Aug 2008) | 1 line Converted rows map within HSSFSheet to use Integer keys ........ r684319 | nick | 2008-08-09 20:34:38 +0100 (Sat, 09 Aug 2008) | 1 line Big big unicode rationalisation in text piece code ........ r684321 | josh | 2008-08-09 20:47:39 +0100 (Sat, 09 Aug 2008) | 1 line added getRowIndex() to HSSFCell, deprecated HSSFFormulaEvaluator.setCurrentRow() ........ r684322 | nick | 2008-08-09 20:56:37 +0100 (Sat, 09 Aug 2008) | 1 line Get most of the hwpf tests passing again ........ r684336 | nick | 2008-08-09 21:31:48 +0100 (Sat, 09 Aug 2008) | 1 line Improve FIB updating on range changes, and add passing tests for non unicode paragraph properties ........ r684349 | nick | 2008-08-09 22:31:28 +0100 (Sat, 09 Aug 2008) | 1 line Start on headers/footers support ........ r684355 | nick | 2008-08-09 22:46:14 +0100 (Sat, 09 Aug 2008) | 1 line Tests to show that header stuff all works right ........ r684362 | nick | 2008-08-09 23:08:34 +0100 (Sat, 09 Aug 2008) | 1 line Add header/footer support to HWPF WordExtractor ........ r684370 | nick | 2008-08-09 23:24:47 +0100 (Sat, 09 Aug 2008) | 1 line Disable a few HWPF tests that aren't working while the unicode/paragraph bug is outstanding ........ git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@684374 13f79535-47bb-0310-9956-ffa450edef68
2008-08-09 18:55:33 -04:00
public int getRowIndex() {
return row.getRowNum();
}
public XSSFCellStyle getCellStyle() {
// Zero is the empty default
if(this.cell.getS() > 0) {
return stylesSource.getStyleAt(this.cell.getS());
}
return null;
}
public int getCellType() {
// Detecting formulas is quite pesky,
// as they don't get their type set
if(this.cell.getF() != null) {
return CELL_TYPE_FORMULA;
}
switch (this.cell.getT().intValue()) {
case STCellType.INT_B:
return CELL_TYPE_BOOLEAN;
case STCellType.INT_N:
if(!cell.isSetV()) {
// ooxml does have a separate cell type of 'blank'. A blank cell gets encoded as
// (either not present or) a numeric cell with no value set.
// The formula evaluator (and perhaps other clients of this interface) needs to
// distinguish blank values which sometimes get translated into zero and sometimes
// empty string, depending on context
return CELL_TYPE_BLANK;
}
return CELL_TYPE_NUMERIC;
case STCellType.INT_E:
return CELL_TYPE_ERROR;
case STCellType.INT_S: // String is in shared strings
case STCellType.INT_INLINE_STR: // String is inline in cell
return CELL_TYPE_STRING;
case STCellType.INT_STR:
return CELL_TYPE_FORMULA;
default:
throw new IllegalStateException("Illegal cell type: " + this.cell.getT());
}
}
public Date getDateCellValue() {
if (STCellType.N == this.cell.getT() || STCellType.STR == this.cell.getT()) {
double value = this.getNumericCellValue();
if (false /* book.isUsing1904DateWindowing() */) { // FIXME
return DateUtil.getJavaDate(value,true);
}
else {
return DateUtil.getJavaDate(value,false);
}
}
throw new NumberFormatException("You cannot get a date value from a cell of type " + this.cell.getT());
}
/**
* Returns the error message, such as #VALUE!
*/
public String getErrorCellString() {
if (STCellType.E != cell.getT()) {
throw new NumberFormatException("You cannot get a error value from a non-error cell");
}
if (this.cell.isSetV()) {
return this.cell.getV();
}
return null;
}
/**
* Returns the error type, in the same way that
* HSSFCell does. See {@link Cell} for details
*/
public byte getErrorCellValue() {
if (STCellType.E != cell.getT()) {
throw new NumberFormatException("You cannot get a error value from a non-error cell");
}
if (this.cell.isSetV()) {
String errS = this.cell.getV();
if(errS.equals(Cell.ERROR_NULL.getStringRepr())) {
return Cell.ERROR_NULL.getType();
}
if(errS.equals(Cell.ERROR_DIV0.getStringRepr())) {
return Cell.ERROR_DIV0.getType();
}
if(errS.equals(Cell.ERROR_VALUE.getStringRepr())) {
return Cell.ERROR_VALUE.getType();
}
if(errS.equals(Cell.ERROR_REF.getStringRepr())) {
return Cell.ERROR_REF.getType();
}
if(errS.equals(Cell.ERROR_NAME.getStringRepr())) {
return Cell.ERROR_NAME.getType();
}
if(errS.equals(Cell.ERROR_NUM.getStringRepr())) {
return Cell.ERROR_NUM.getType();
}
return Cell.ERROR_NA.getType();
}
return 0;
}
public double getNumericCellValue() {
if (STCellType.N != cell.getT() && STCellType.STR != cell.getT()) {
throw new NumberFormatException("You cannot get a numeric value from a non-numeric cell");
}
if (this.cell.isSetV()) {
return Double.parseDouble(this.cell.getV());
}
// else - cell is blank.
// TODO - behaviour in the case of blank cells.
// Revise spec, choose best alternative below, and comment why.
if (true) {
// returning NaN from a blank cell seems wrong
// there are a few junits which assert this behaviour, though.
return Double.NaN;
}
if (true) {
// zero is probably a more reasonable value.
return 0.0;
} else {
// or perhaps disallow reading value from blank cell.
throw new RuntimeException("You cannot get a numeric value from a blank cell");
}
// Note - it would be nice if the behaviour is consistent with getRichStringCellValue
// (i.e. whether to return empty string or throw exception).
}
public XSSFRichTextString getRichStringCellValue() {
if(this.cell.getT() == STCellType.INLINE_STR) {
if(this.cell.isSetV()) {
return new XSSFRichTextString(this.cell.getV());
} else {
return new XSSFRichTextString("");
}
}
if(this.cell.getT() == STCellType.S) {
XSSFRichTextString rt;
if(this.cell.isSetV()) {
int sRef = Integer.parseInt(this.cell.getV());
rt = new XSSFRichTextString(sharedStringSource.getEntryAt(sRef));
} else {
rt = new XSSFRichTextString("");
}
rt.setStylesTableReference(stylesSource);
return rt;
}
throw new NumberFormatException("You cannot get a string value from a non-string cell");
}
/**
* Sets this cell as the active cell for the worksheet
*/
public void setAsActiveCell() {
row.getSheet().setActiveCell(cell.getR());
}
public void setCellComment(Comment comment) {
String cellRef = new CellReference(row.getRowNum(), getCellNum()).formatAsString();
row.getSheet().setCellComment(cellRef, (XSSFComment)comment);
}
public void setCellErrorValue(byte value) {
if(value == Cell.ERROR_DIV0.getType()) {
setCellErrorValue(Cell.ERROR_DIV0);
} else if(value == Cell.ERROR_NA.getType()) {
setCellErrorValue(Cell.ERROR_NA);
} else if(value == Cell.ERROR_NAME.getType()) {
setCellErrorValue(Cell.ERROR_NAME);
} else if(value == Cell.ERROR_NULL.getType()) {
setCellErrorValue(Cell.ERROR_NULL);
} else if(value == Cell.ERROR_NUM.getType()) {
setCellErrorValue(Cell.ERROR_NUM);
} else if(value == Cell.ERROR_REF.getType()) {
setCellErrorValue(Cell.ERROR_REF);
} else if(value == Cell.ERROR_VALUE.getType()) {
setCellErrorValue(Cell.ERROR_VALUE);
} else {
logger.log(POILogger.WARN, "Unknown error type " + value + " specified, treating as #N/A");
setCellErrorValue(Cell.ERROR_NA);
}
}
public void setCellErrorValue(CELL_ERROR_TYPE errorType) {
if ((this.cell.getT() != STCellType.E) && (this.cell.getT() != STCellType.STR))
{
this.cell.setT(STCellType.E);
}
this.cell.setV( errorType.getStringRepr() );
}
public void setCellFormula(String formula) {
CTCellFormula f = CTCellFormula.Factory.newInstance();
f.setStringValue(formula);
this.cell.setF(f);
// XXX: is this correct? Should we recompute the value when the formula changes?
if (this.cell.isSetV()) {
this.cell.unsetV();
}
}
public void setCellNum(int num) {
setCellNum((short)num);
}
public void setCellNum(short num) {
checkBounds(num);
this.cellNum = num;
this.cell.setR(formatPosition());
}
protected static short parseCellNum(String r) {
r = r.split("\\d+")[0];
if (r.length() == 1) {
return (short) (r.charAt(0) - 'A');
} else {
return (short) (r.charAt(1) - 'A' + 26 * (r.charAt(0) - '@'));
}
}
protected String formatPosition() {
int col = this.getCellNum();
String result = Character.valueOf((char) (col % 26 + 'A')).toString();
if (col >= 26){
col = col / 26;
result = Character.valueOf((char) (col + '@')) + result;
}
result = result + String.valueOf(row.getRowNum() + 1);
return result;
}
public void setCellStyle(CellStyle style) {
if(style == null) {
this.cell.setS(0);
} else {
Merged revisions 638786-638802,638805-638811,638813-638814,638816-639230,639233-639241,639243-639253,639255-639486,639488-639601,639603-639835,639837-639917,639919-640056,640058-640710,640712-641156,641158-641184,641186-641795,641797-641798,641800-641933,641935-641963,641965-641966,641968-641995,641997-642230,642232-642562,642564-642565,642568-642570,642572-642573,642576-642736,642739-642877,642879,642881-642890,642892-642903,642905-642945,642947-643624,643626-643653,643655-643669,643671,643673-643830,643832-643833,643835-644342,644344-644472,644474-644508,644510-645347,645349-645351,645353-645559,645561-645565,645568-645951,645953-646193,646195-646311,646313-646404,646406-646665,646667-646853,646855-646869,646871-647151,647153-647185,647187-647277,647279-647566,647568-647573,647575,647578-647711,647714-647737,647739-647823,647825-648155,648157-648202,648204-648273,648275,648277-648302,648304-648333,648335-648588,648590-648622,648625-648673,648675-649141,649144,649146-649556,649558-649795,649799,649801-649910,649912-649913,649915-650128,650131-650132,650134-650137,650140-650914,650916-651991,651993-652284,652286-652287,652289,652291,652293-652297,652299-652328,652330-652425,652427-652445,652447-652560,652562-652933,652935,652937-652993,652995-653116,653118-653124,653126-653483,653487-653519,653522-653550,653552-653607,653609-653667,653669-653674,653676-653814,653817-653830,653832-653891,653893-653944,653946-654055,654057-654355,654357-654365,654367-654648,654651-655215,655217-655277,655279-655281,655283-655911,655913-656212,656214,656216-656251,656253-656698,656700-656756,656758-656892,656894-657135,657137-657165,657168-657179,657181-657354,657356-657357,657359-657701,657703-657874,657876-658032,658034-658284,658286,658288-658301,658303-658307,658309-658321,658323-658335,658337-658348,658351,658353-658832,658834-658983,658985,658987-659066,659068-659402,659404-659428,659430-659451,659453-659454,659456-659461,659463-659477,659479-659524,659526-659571,659574,659576-660255,660257-660262,660264-660279,660281-660343,660345-660473,660475-660827,660829-660833,660835-660888,660890-663321,663323-663435,663437-663764,663766-663854,663856-664219,664221-664489,664494-664514,664516-668013,668015-668142,668144-668152,668154,668156-668256,668258,668260-669139,669141-669455,669457-669657,669659-669808,669810-670189,670191-671321,671323-672229,672231-672549,672551-672552,672554-672561,672563-672566,672568,672571-673049,673051-673852,673854-673862,673864-673986,673988-673996,673998-674347,674349-674890,674892-674910,674912-674936,674938-674952,674954-675078,675080-675085,675087-675217,675219-675660,675662-675670,675672-675716,675718-675726,675728-675733,675735-675775,675777-675782,675784,675786-675791,675794-676205 via svnmerge from https://svn.apache.org:443/repos/asf/poi/trunk ........ r675853 | josh | 2008-07-11 08:59:44 +0100 (Fri, 11 Jul 2008) | 1 line Patch 45289 - finished support for special comparison operators in COUNTIF ........ r676201 | nick | 2008-07-12 17:56:55 +0100 (Sat, 12 Jul 2008) | 1 line Support for cloning one font record onto another, plus tests ........ r676203 | nick | 2008-07-12 18:21:54 +0100 (Sat, 12 Jul 2008) | 1 line Support for cloning one extended format record onto another, plus tests ........ r676205 | nick | 2008-07-12 18:38:10 +0100 (Sat, 12 Jul 2008) | 1 line Allow the cloning of one HSSFCellStyle onto another, including cloning styles from one HSSFWorkbook onto another ........ git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@676209 13f79535-47bb-0310-9956-ffa450edef68
2008-07-12 14:17:16 -04:00
XSSFCellStyle xStyle = (XSSFCellStyle)style;
xStyle.verifyBelongsToStylesSource(
row.getSheet().getWorkbook().getStylesSource()
);
this.cell.setS(
Merged revisions 638786-638802,638805-638811,638813-638814,638816-639230,639233-639241,639243-639253,639255-639486,639488-639601,639603-639835,639837-639917,639919-640056,640058-640710,640712-641156,641158-641184,641186-641795,641797-641798,641800-641933,641935-641963,641965-641966,641968-641995,641997-642230,642232-642562,642564-642565,642568-642570,642572-642573,642576-642736,642739-642877,642879,642881-642890,642892-642903,642905-642945,642947-643624,643626-643653,643655-643669,643671,643673-643830,643832-643833,643835-644342,644344-644472,644474-644508,644510-645347,645349-645351,645353-645559,645561-645565,645568-645951,645953-646193,646195-646311,646313-646404,646406-646665,646667-646853,646855-646869,646871-647151,647153-647185,647187-647277,647279-647566,647568-647573,647575,647578-647711,647714-647737,647739-647823,647825-648155,648157-648202,648204-648273,648275,648277-648302,648304-648333,648335-648588,648590-648622,648625-648673,648675-649141,649144,649146-649556,649558-649795,649799,649801-649910,649912-649913,649915-650128,650131-650132,650134-650137,650140-650914,650916-651991,651993-652284,652286-652287,652289,652291,652293-652297,652299-652328,652330-652425,652427-652445,652447-652560,652562-652933,652935,652937-652993,652995-653116,653118-653124,653126-653483,653487-653519,653522-653550,653552-653607,653609-653667,653669-653674,653676-653814,653817-653830,653832-653891,653893-653944,653946-654055,654057-654355,654357-654365,654367-654648,654651-655215,655217-655277,655279-655281,655283-655911,655913-656212,656214,656216-656251,656253-656698,656700-656756,656758-656892,656894-657135,657137-657165,657168-657179,657181-657354,657356-657357,657359-657701,657703-657874,657876-658032,658034-658284,658286,658288-658301,658303-658307,658309-658321,658323-658335,658337-658348,658351,658353-658832,658834-658983,658985,658987-659066,659068-659402,659404-659428,659430-659451,659453-659454,659456-659461,659463-659477,659479-659524,659526-659571,659574,659576-660255,660257-660262,660264-660279,660281-660343,660345-660473,660475-660827,660829-660833,660835-660888,660890-663321,663323-663435,663437-663764,663766-663854,663856-664219,664221-664489,664494-664514,664516-668013,668015-668142,668144-668152,668154,668156-668256,668258,668260-669139,669141-669455,669457-669657,669659-669808,669810-670189,670191-671321,671323-672229,672231-672549,672551-672552,672554-672561,672563-672566,672568,672571-673049,673051-673852,673854-673862,673864-673986,673988-673996,673998-674347,674349-674890,674892-674910,674912-674936,674938-674952,674954-675078,675080-675085,675087-675217,675219-675660,675662-675670,675672-675716,675718-675726,675728-675733,675735-675775,675777-675782,675784,675786-675791,675794-676205 via svnmerge from https://svn.apache.org:443/repos/asf/poi/trunk ........ r675853 | josh | 2008-07-11 08:59:44 +0100 (Fri, 11 Jul 2008) | 1 line Patch 45289 - finished support for special comparison operators in COUNTIF ........ r676201 | nick | 2008-07-12 17:56:55 +0100 (Sat, 12 Jul 2008) | 1 line Support for cloning one font record onto another, plus tests ........ r676203 | nick | 2008-07-12 18:21:54 +0100 (Sat, 12 Jul 2008) | 1 line Support for cloning one extended format record onto another, plus tests ........ r676205 | nick | 2008-07-12 18:38:10 +0100 (Sat, 12 Jul 2008) | 1 line Allow the cloning of one HSSFCellStyle onto another, including cloning styles from one HSSFWorkbook onto another ........ git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@676209 13f79535-47bb-0310-9956-ffa450edef68
2008-07-12 14:17:16 -04:00
row.getSheet().getWorkbook().getStylesSource().putStyle(xStyle)
);
}
}
/**
* set the cells type (numeric, formula or string)
* @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) {
switch (cellType) {
case CELL_TYPE_BOOLEAN:
this.cell.setT(STCellType.B);
break;
case CELL_TYPE_NUMERIC:
this.cell.setT(STCellType.N);
break;
case CELL_TYPE_ERROR:
this.cell.setT(STCellType.E);
break;
case CELL_TYPE_STRING:
this.cell.setT(STCellType.S);
break;
default:
throw new IllegalArgumentException("Illegal type: " + cellType);
}
}
public void setCellValue(double value) {
if ((this.cell.getT() != STCellType.N) && (this.cell.getT() != STCellType.STR))
{
this.cell.setT(STCellType.N);
}
this.cell.setV(String.valueOf(value));
}
/**
* 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) {
boolean date1904 = this.row.getSheet().getWorkbook().isDate1904();
setCellValue(DateUtil.getExcelDate(value, date1904));
}
/**
* 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 <code>setCellValue(value.getTime())</code> which will
* automatically shift the times to the default timezone.
*
* @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) {
boolean date1904 = this.row.getSheet().getWorkbook().isDate1904();
setCellValue( DateUtil.getExcelDate(value, date1904 ));
}
public void setCellValue(String str) {
this.setCellValue(new XSSFRichTextString(str));
}
public void setCellValue(RichTextString value) {
if(this.cell.getT() == STCellType.INLINE_STR) {
this.cell.setV(value.getString());
return;
}
if(this.cell.getT() != STCellType.S) {
this.cell.setT(STCellType.S);
}
XSSFRichTextString rt = (XSSFRichTextString)value;
rt.setStylesTableReference(stylesSource);
int sRef = sharedStringSource.addEntry(rt.getCTRst());
this.cell.setV(Integer.toString(sRef));
}
public void setCellValue(boolean value) {
if ((this.cell.getT() != STCellType.B) && (this.cell.getT() != STCellType.STR))
{
this.cell.setT(STCellType.B);
}
this.cell.setV(value ? TRUE_AS_STRING : FALSE_AS_STRING);
}
/**
* 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&lt;errIdx&gt;
*/
public String toString() {
// return "[" + this.row.getRowNum() + "," + this.getCellNum() + "] " + this.cell.getV();
switch (getCellType()) {
case CELL_TYPE_BLANK:
return "";
case CELL_TYPE_BOOLEAN:
return getBooleanCellValue() ? "TRUE" : "FALSE";
case CELL_TYPE_ERROR:
return ErrorEval.getText(getErrorCellValue());
case CELL_TYPE_FORMULA:
return getCellFormula();
case CELL_TYPE_NUMERIC:
//TODO apply the dataformat for this cell
if (DateUtil.isCellDateFormatted(this)) {
DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
return sdf.format(getDateCellValue());
} else {
return getNumericCellValue() + "";
}
case CELL_TYPE_STRING:
return getRichStringCellValue().toString();
default:
return "Unknown Cell Type: " + getCellType();
}
}
/**
* Returns the raw, underlying ooxml value for the cell
*/
public String getRawValue() {
return this.cell.getV();
}
/**
* @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.");
}
}
public Hyperlink getHyperlink() {
return row.getSheet().getHyperlink(row.getRowNum(), cellNum);
}
public void setHyperlink(Hyperlink hyperlink) {
XSSFHyperlink link = (XSSFHyperlink)hyperlink;
// Assign to us
link.setCellReference( new CellReference(row.getRowNum(), cellNum).formatAsString() );
// Add to the lists
row.getSheet().setCellHyperlink(link);
}
}