use of org.apache.poi.ss.usermodel.CellType in project poi by apache.
the class XSSFCell method getRichStringCellValue.
/**
* Get the value of the cell as a XSSFRichTextString
* <p>
* For numeric cells we throw an exception. For blank cells we return an empty string.
* For formula cells we return the pre-calculated value if a string, otherwise an exception
* </p>
* @return the value of the cell as a XSSFRichTextString
*/
@Override
public XSSFRichTextString getRichStringCellValue() {
CellType cellType = getCellTypeEnum();
XSSFRichTextString rt;
switch(cellType) {
case BLANK:
rt = new XSSFRichTextString("");
break;
case STRING:
if (_cell.getT() == STCellType.INLINE_STR) {
if (_cell.isSetIs()) {
//string is expressed directly in the cell definition instead of implementing the shared string table.
rt = new XSSFRichTextString(_cell.getIs());
} else if (_cell.isSetV()) {
//cached result of a formula
rt = new XSSFRichTextString(_cell.getV());
} else {
rt = new XSSFRichTextString("");
}
} else if (_cell.getT() == STCellType.STR) {
//cached formula value
rt = new XSSFRichTextString(_cell.isSetV() ? _cell.getV() : "");
} else {
if (_cell.isSetV()) {
int idx = Integer.parseInt(_cell.getV());
rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(idx));
} else {
rt = new XSSFRichTextString("");
}
}
break;
case FORMULA:
checkFormulaCachedValueType(CellType.STRING, getBaseCellType(false));
rt = new XSSFRichTextString(_cell.isSetV() ? _cell.getV() : "");
break;
default:
throw typeMismatch(CellType.STRING, cellType, false);
}
rt.setStylesTableReference(_stylesSource);
return rt;
}
use of org.apache.poi.ss.usermodel.CellType in project poi by apache.
the class XSSFCell method setCellType.
/**
* Set the cells type (numeric, formula or string)
*
* @throws IllegalArgumentException if the specified cell type is invalid
*/
@Override
public void setCellType(CellType cellType) {
CellType prevType = getCellTypeEnum();
if (isPartOfArrayFormulaGroup()) {
notifyArrayFormulaChanging();
}
if (prevType == CellType.FORMULA && cellType != CellType.FORMULA) {
getSheet().getWorkbook().onDeleteFormula(this);
}
switch(cellType) {
case NUMERIC:
_cell.setT(STCellType.N);
break;
case STRING:
if (prevType != CellType.STRING) {
String str = convertCellValueToString();
XSSFRichTextString rt = new XSSFRichTextString(str);
rt.setStylesTableReference(_stylesSource);
int sRef = _sharedStringSource.addEntry(rt.getCTRst());
_cell.setV(Integer.toString(sRef));
}
_cell.setT(STCellType.S);
break;
case FORMULA:
if (!_cell.isSetF()) {
CTCellFormula f = CTCellFormula.Factory.newInstance();
f.setStringValue("0");
_cell.setF(f);
if (_cell.isSetT()) {
_cell.unsetT();
}
}
break;
case BLANK:
setBlank();
break;
case BOOLEAN:
String newVal = convertCellValueToBoolean() ? TRUE_AS_STRING : FALSE_AS_STRING;
_cell.setT(STCellType.B);
_cell.setV(newVal);
break;
case ERROR:
_cell.setT(STCellType.E);
break;
default:
throw new IllegalArgumentException("Illegal cell type: " + cellType);
}
if (cellType != CellType.FORMULA && _cell.isSetF()) {
_cell.unsetF();
}
}
use of org.apache.poi.ss.usermodel.CellType in project poi by apache.
the class SheetDataWriter method writeCell.
public void writeCell(int columnIndex, Cell cell) throws IOException {
if (cell == null) {
return;
}
String ref = new CellReference(_rownum, columnIndex).formatAsString();
_out.write("<c r=\"" + ref + "\"");
CellStyle cellStyle = cell.getCellStyle();
if (cellStyle.getIndex() != 0) {
// need to convert the short to unsigned short as the indexes can be up to 64k
// ideally we would use int for this index, but that would need changes to some more
// APIs
_out.write(" s=\"" + (cellStyle.getIndex() & 0xffff) + "\"");
}
CellType cellType = cell.getCellTypeEnum();
switch(cellType) {
case BLANK:
{
_out.write(">");
break;
}
case FORMULA:
{
_out.write(">");
_out.write("<f>");
outputQuotedString(cell.getCellFormula());
_out.write("</f>");
switch(cell.getCachedFormulaResultTypeEnum()) {
case NUMERIC:
double nval = cell.getNumericCellValue();
if (!Double.isNaN(nval)) {
_out.write("<v>" + nval + "</v>");
}
break;
default:
break;
}
break;
}
case STRING:
{
if (_sharedStringSource != null) {
XSSFRichTextString rt = new XSSFRichTextString(cell.getStringCellValue());
int sRef = _sharedStringSource.addEntry(rt.getCTRst());
_out.write(" t=\"" + STCellType.S + "\">");
_out.write("<v>");
_out.write(String.valueOf(sRef));
_out.write("</v>");
} else {
_out.write(" t=\"inlineStr\">");
_out.write("<is><t");
if (hasLeadingTrailingSpaces(cell.getStringCellValue())) {
_out.write(" xml:space=\"preserve\"");
}
_out.write(">");
outputQuotedString(cell.getStringCellValue());
_out.write("</t></is>");
}
break;
}
case NUMERIC:
{
_out.write(" t=\"n\">");
_out.write("<v>" + cell.getNumericCellValue() + "</v>");
break;
}
case BOOLEAN:
{
_out.write(" t=\"b\">");
_out.write("<v>" + (cell.getBooleanCellValue() ? "1" : "0") + "</v>");
break;
}
case ERROR:
{
FormulaError error = FormulaError.forInt(cell.getErrorCellValue());
_out.write(" t=\"e\">");
_out.write("<v>" + error.getString() + "</v>");
break;
}
default:
{
throw new IllegalStateException("Invalid cell type: " + cellType);
}
}
_out.write("</c>");
}
use of org.apache.poi.ss.usermodel.CellType in project poi by apache.
the class ExcelComparator method compareDataInCell.
private void compareDataInCell(Locator loc1, Locator loc2) {
if (isCellTypeMatches(loc1, loc2)) {
final CellType loc1cellType = loc1.cell.getCellTypeEnum();
switch(loc1cellType) {
case BLANK:
case STRING:
case ERROR:
isCellContentMatches(loc1, loc2);
break;
case BOOLEAN:
isCellContentMatchesForBoolean(loc1, loc2);
break;
case FORMULA:
isCellContentMatchesForFormula(loc1, loc2);
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(loc1.cell)) {
isCellContentMatchesForDate(loc1, loc2);
} else {
isCellContentMatchesForNumeric(loc1, loc2);
}
break;
default:
throw new IllegalStateException("Unexpected cell type: " + loc1cellType);
}
}
isCellFillPatternMatches(loc1, loc2);
isCellAlignmentMatches(loc1, loc2);
isCellHiddenMatches(loc1, loc2);
isCellLockedMatches(loc1, loc2);
isCellFontFamilyMatches(loc1, loc2);
isCellFontSizeMatches(loc1, loc2);
isCellFontBoldMatches(loc1, loc2);
isCellUnderLineMatches(loc1, loc2);
isCellFontItalicsMatches(loc1, loc2);
isCellBorderMatches(loc1, loc2, 't');
isCellBorderMatches(loc1, loc2, 'l');
isCellBorderMatches(loc1, loc2, 'b');
isCellBorderMatches(loc1, loc2, 'r');
isCellFillBackGroundMatches(loc1, loc2);
}
use of org.apache.poi.ss.usermodel.CellType in project poi by apache.
the class ExcelComparator method isCellTypeMatches.
/**
* Checks if cell type matches.
*/
private boolean isCellTypeMatches(Locator loc1, Locator loc2) {
CellType type1 = loc1.cell.getCellTypeEnum();
CellType type2 = loc2.cell.getCellTypeEnum();
if (type1 == type2)
return true;
addMessage(loc1, loc2, "Cell Data-Type does not Match in :: ", type1.name(), type2.name());
return false;
}
Aggregations