POI封装之六


public class POICell implements ICell {

private Cell cell;

private String key;

private FormulaEvaluator fe;

private IRow row;

private static final Map<String, CellStyle> CACHE = new HashMap<String, CellStyle>();

POICell(Cell cell, String key) {
this.cell = cell;
this.key = key;
}

@Override
public int getType() {
return cell.getCellType();
}

@Override
public void setValue(Object value) {
int type = getType();
if (Cell.CELL_TYPE_NUMERIC == type) {
if (value instanceof Date) {
Workbook wb = cell.getRow().getSheet().getWorkbook();
CellStyle cs = wb.createCellStyle();
cs.setDataFormat(wb.createDataFormat().getFormat("yyyy-m-d"));
cell.setCellStyle(cs);
cell.setCellValue((Date) value);
} else {
cell.setCellValue(Double.parseDouble(String.valueOf(value)));
}

} else if (Cell.CELL_TYPE_STRING == type) {
cell.setCellValue(String.valueOf(value));
} else if (Cell.CELL_TYPE_BLANK == type) {
cell.setCellValue(String.valueOf(value));
} else if (Cell.CELL_TYPE_BOOLEAN == type) {
cell.setCellValue(Boolean.parseBoolean(String.valueOf(value)));
} else if (Cell.CELL_TYPE_FORMULA == type) {
cell.setCellFormula(String.valueOf(value));
} else {
cell.setCellValue((RichTextString) value);
}

}

@Override
public String getKey() {
return key;
}

@Override
public Object evaluate() {
if (null == fe) {
fe = cell.getSheet().getWorkbook().getCreationHelper()
.createFormulaEvaluator();
}
CellValue cv = fe.evaluate(cell);
if (cv == null) {
return null;
}
return getCellValue(cv);
}

private Object getCellValue(CellValue cv) {

switch (cv.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
return cv.getBooleanValue();
case Cell.CELL_TYPE_NUMERIC:
return cv.getNumberValue();
case Cell.CELL_TYPE_STRING:
return cv.getStringValue();
case Cell.CELL_TYPE_BLANK:
return null;
case Cell.CELL_TYPE_ERROR:
return null;
case Cell.CELL_TYPE_FORMULA:
return null;
}
return null;
}

@Override
public IRow getRow() {
return this.row;
}

void setRow(IRow row) {
this.row = row;
}

@Override
public ICellValue getCellValue() {
return new POICellValue(cell);
}

@Override
public void setValue(Object value, String dataFormat) {
int type = getType();
Workbook wb = cell.getRow().getSheet().getWorkbook();

CellStyle cs = null;
if (CACHE.containsKey(dataFormat)) {
cs = CACHE.get(dataFormat);
} else {
cs = wb.createCellStyle();
cs.setDataFormat(wb.createDataFormat().getFormat(dataFormat));
cs.setWrapText(true);
cs.setShrinkToFit(true);
CACHE.put(dataFormat, cs);
}

cell.setCellStyle(cs);

if (Cell.CELL_TYPE_NUMERIC == type) {
if (value instanceof Date) {
cell.setCellValue((Date) value);
} else {
cell.setCellValue(Double.parseDouble(String.valueOf(value)));
}

} else if (Cell.CELL_TYPE_STRING == type) {
cell.setCellValue(String.valueOf(value));
} else if (Cell.CELL_TYPE_BLANK == type) {
cell.setCellValue(String.valueOf(value));
} else if (Cell.CELL_TYPE_BOOLEAN == type) {
cell.setCellValue(Boolean.parseBoolean(String.valueOf(value)));
} else if (Cell.CELL_TYPE_FORMULA == type) {
cell.setCellFormula(String.valueOf(value));
} else {
cell.setCellValue((RichTextString) value);
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值