POI Excelを導入して各種のフォーマットのセルの公共の方法を解析して整理します。
4301 ワード
(テキスト、日付、数値、数式セル、ブール、空の値を解析できます。)
1、解析数値型は2桁の小数を保持し、日付書式はyyy-mm-ddと数式型のセルです。
1、解析数値型は2桁の小数を保持し、日付書式はyyy-mm-ddと数式型のセルです。
/**
* cell
* @param cell
* @return
*/
private String getCellValue(HSSFCell cell) {
if (cell != null) {
String value = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC: //
DecimalFormat df = new DecimalFormat("#.##");
if (HSSFDateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
// date , cell date
//value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString();
} else { //
value = df.format(cell.getNumericCellValue());
}
return value;
case HSSFCell.CELL_TYPE_STRING: //
value = cell.getRichStringCellValue().toString();
return value;
case HSSFCell.CELL_TYPE_BOOLEAN: //
value = "" + cell.getBooleanCellValue();
return value;
case HSSFCell.CELL_TYPE_BLANK: //
value = "";
case HSSFCell.CELL_TYPE_ERROR: //
value = "";
return value;
case HSSFCell.CELL_TYPE_FORMULA:
//
/*value = String.valueOf(cell.getNumericCellValue());
if (value.equals("NaN")) {// ,
value = cell.getStringCellValue().toString();
}
return value;*/
HSSFWorkbook wb = cell.getSheet().getWorkbook();
HSSFCreationHelper crateHelper = wb.getCreationHelper();
HSSFFormulaEvaluator evaluator = crateHelper.createFormulaEvaluator();
value = getCellValue(evaluator.evaluateInCell(cell));
return value;
default:
value = cell.getRichStringCellValue().toString();
return value;
}
}
return "";
}
2、数値型のセルを解析して、エクセルセルの中でどんな精度が保たれていますか?日付の種類はyyyy-mm-ddでなければなりません。日付の書式にしても、テキストの書式にしてもかまいません。数式型のセルを解析できます。/**
* cell
* @param cell
* @return
*/
private String getCellValue(XSSFCell cell) {
if (cell != null) {
String value = "";
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_NUMERIC: //
//DecimalFormat df = new DecimalFormat("#.################");
if (DateUtil.isCellDateFormatted(cell)) {
// yyyy-mm-dd
if(178==cell.getCellStyle().getDataFormat()){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
value = sdf.format(DateUtil.getJavaDate(cell.getNumericCellValue()));
}else{
//String str = cell.getCellStyle().getDataFormatString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
value = sdf.format(DateUtil.getJavaDate(cell.getNumericCellValue()));
}
} else { //
//value = df.format(cell.getNumericCellValue());
// , excel
cell.setCellType(XSSFCell.CELL_TYPE_STRING);// ,
value = cell.getRichStringCellValue().toString();
}
return value;
case XSSFCell.CELL_TYPE_STRING: //
value = cell.getRichStringCellValue().toString();
return value;
case XSSFCell.CELL_TYPE_BOOLEAN: //
value = "" + cell.getBooleanCellValue();
return value;
case XSSFCell.CELL_TYPE_BLANK: //
value = "";
case XSSFCell.CELL_TYPE_ERROR: //
value = "";
return value;
case XSSFCell.CELL_TYPE_FORMULA:
//
/*value = String.valueOf(cell.getNumericCellValue());
if (value.equals("NaN")) {// ,
value = cell.getStringCellValue().toString();
}
return value;*/
XSSFWorkbook wb = cell.getSheet().getWorkbook();
XSSFCreationHelper crateHelper = wb.getCreationHelper();
XSSFFormulaEvaluator evaluator = crateHelper.createFormulaEvaluator();
value = getCellValue(evaluator.evaluateInCell(cell));
return value;
default:
value = cell.getRichStringCellValue().toString();
return value;
}
}
return "";
}
ご指摘を歓迎します