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);
}
}
}
POI封装之六
最新推荐文章于 2023-12-06 08:35:55 发布