最近应客户需求,导入excel表格,且要求数据精度和日期格式与文件一致。
之前虽然做过导入导出的功能,但要求没有这么细致,因此在网上查找了大量的文件,找到了表格的cell.getCellStyle().getDataFormatString()这个属性,可以根据属性在程序里转换成自己需要的格式。
public String getCellValue(Row row, int column) {
if (row == null) {
return "";
}
Object val = "";
String res = "";
Cell cell = row.getCell(column);
try {
if (StringUtils.isNotNull(cell)) {
// System.out.println("------------"+ cell.getCellType()
// +"====="+row.getCell(1).getStringCellValue()+"**********");
if (cell.getCellType() == CellType.NUMERIC) {
val = cell.getNumericCellValue();
// System.out.println(cell.getCellStyle().getDataFormat()+"------------"+ cell.getCellType()
// +"====="+row.getCell(1).getStringCellValue()+"*********$$$$"+val+"**********"+cell.getCellStyle().getDataFormatString());
SimpleDateFormat sdf = getDateFormat(cell.getCellStyle().getDataFormatString().replaceAll("\"", ""));
if (HSSFDateUtil.isCellDateFormatted(cell)) {//处理数值格式
res = sdf.format(cell.getDateCellValue());
} else if (DateUtil.isCellDateFormatted(cell)
|| sdf !=null)) {
val = DateUtil.getJavaDate((Double) val);
res = sdf.format(val);
} else {
if(cell.getCellStyle().getDataFormatString().contains("-")) {
String f1 = cell.getCellStyle().getDataFormatString();
// System.out.println("format========"+f1);
f1 =f1.replace("\\", "").replace("\"", "");
// System.out.println("format========"+f1);
DecimalFormat format = new DecimalFormat();
format.applyPattern(f1);
res = format.format(val);
}else if(cell.getCellStyle().getDataFormatString().contains("%")) {
res = new DecimalFormat(cell.getCellStyle().getDataFormatString().split("%")[0]).format(cell.getNumericCellValue()*100)+"%";
}else if(cell.getCellStyle().getDataFormatString().equals("General")) {
DecimalFormat format = new DecimalFormat();
format.applyPattern("#");
res = format.format(val);
}else if(cell.getCellStyle().getDataFormatString().contains(";")) {
String format = cell.getCellStyle().getDataFormatString().split(";")[0];
// System.out.println("format========"+format);
format =format.substring(0,format.lastIndexOf("0")+1);
// System.out.println("format========"+format);
res = new DecimalFormat(format).format(val);
}else {
val = new BigDecimal(val.toString());
res = val.toString();
}
}
}else if (cell.getCellType().equals(CellType.FORMULA)) {
switch (cell.getCachedFormulaResultType()) {
case STRING:
res = cell.getStringCellValue();
break;
case NUMERIC:
NumberFormat nf = NumberFormat.getInstance();
// System.out.println(cell.getCellStyle().getDataFormat()+"------------"+ cell.getCellType()
// +"====="+row.getCell(1).getStringCellValue()+"**********"+"$$$$"+HSSFDateUtil.isCellDateFormatted(cell)+"**********"+cell.getCellStyle().getDataFormatString());
nf.setGroupingUsed(false);
res = String.valueOf(nf.format(cell.getNumericCellValue()));
SimpleDateFormat sdf = getDateFormat(cell.getCellStyle().getDataFormatString().replaceAll("\"", ""));
if (row.getCell(1).getStringCellValue().contains("孵天")) {
res = getTime(cell.getNumericCellValue());
} else if (DateUtil.isCellDateFormatted(cell)||sdf!=null) {
// System.out.println(res+"------"+cell.getNumericCellValue());
val = DateUtil.getJavaDate(cell.getNumericCellValue()); // POI Excel 日期格式转换
res = sdf.format(val).replace("00时00分", "");
}else {
double d = cell.getNumericCellValue();
if(cell.getCellStyle().getDataFormatString().contains(";[Red]")) {
// System.out.println(cell.getCellStyle().getDataFormat()+"------------"+ cell.getCellType()
// +"====="+row.getCell(1).getStringCellValue()+"**********"+ val+" $$$$ "+d+"**********"+cell.getCellStyle().getDataFormatString());
res = new DecimalFormat(cell.getCellStyle().getDataFormatString().split(";[Red]")[0]).format(d);
}
}
break;
case BOOLEAN:
res = String.valueOf(cell.getBooleanCellValue());
break;
default:
res = "";
}
// res = evaluate.formatAsString();
} else if (cell.getCellType() == CellType.STRING) {
res = cell.getStringCellValue();
} else if (cell.getCellType() == CellType.BOOLEAN) {
res = Boolean.toString(cell.getBooleanCellValue());
} else if (cell.getCellType() == CellType.ERROR) {
res = Byte.toString(cell.getErrorCellValue());
}
}
} catch (Exception e) {
// TODO: handle exception
}
return res;
}
这个是我的导入的数据的一些年月日的格式,就加入了进来,希望能帮助大家。
private SimpleDateFormat getDateFormat(String s1) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = null;
if(s1.equals("yyyy年mm月dd日hh时mm分")) {
sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");
}else if(s1.equals("yyyy年mm月dd日")) {
sdf = new SimpleDateFormat("yyyy年MM月dd日");
}else if(s1.equals("mm月dd日hh时mm分")) {
sdf = new SimpleDateFormat("MM月dd日HH时mm分");
}else if(s1.equals("mm月dd日hh时")) {
sdf = new SimpleDateFormat("MM月dd日HH时");
}else if(s1.equals("mm月dd日")) {
sdf = new SimpleDateFormat("MM月dd日");
}else if(s1.equals("hh时mm分")) {
sdf = new SimpleDateFormat("HH时mm分");
}else if(s1.contains("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
}else if(s1.contains("yyyy.mm")) {
sdf = new SimpleDateFormat("yyyy.MM");
}else if(s1.equals("yyyy年mm月dd日hh时")) {
sdf = new SimpleDateFormat("yyyy年MM月dd日HH时");
}else if(s1.equals("hh时")) {
sdf = new SimpleDateFormat("HH时");
}
return sdf;
}