導(dǎo)出數(shù)據(jù)到Excel,并把單元格內(nèi)容轉(zhuǎn)為字符串。文章來源:http://www.zghlxwxcb.cn/news/detail-767072.html
// 將單元格內(nèi)容轉(zhuǎn)化為字符串
private static String convertCellValueToString(Cell cell) {
if (null == cell) {
return null;
}
String returnValue = null;
switch (cell.getCellType()) {
case STRING: //字符串
returnValue = cell.getStringCellValue();
break;
case NUMERIC: //數(shù)字
double numericCellValue = cell.getNumericCellValue();
boolean isInteger = isInteger(numericCellValue);
if (isInteger) {
DecimalFormat df = new DecimalFormat("0");
returnValue = df.format(numericCellValue);
} else {
returnValue = Double.toString(numericCellValue);
}
break;
case BOOLEAN: //布爾
boolean booleanCellValue = cell.getBooleanCellValue();
returnValue = Boolean.toString(booleanCellValue);
break;
case BLANK: //空值
break;
case FORMULA: //公式
// returnValue = cell.getCellFormula();
try {
returnValue = String.valueOf(cell.getNumericCellValue());
} catch (IllegalStateException e) {
returnValue = String.valueOf(cell.getRichStringCellValue());
}
break;
case ERROR: //故障
break;
default:
break;
}
return returnValue;
}
// 判斷是否為整數(shù),是返回true,否則返回false.
public static boolean isInteger(Double num) {
double eqs = 1e-10; //精度范圍
return num - Math.floor(num) < eqs;
}
參考
POI讀取excel時(shí),單元格內(nèi)容轉(zhuǎn)化字符串
Java poi讀取Excel中公式的計(jì)算值文章來源地址http://www.zghlxwxcb.cn/news/detail-767072.html
到了這里,關(guān)于Java excel單元格內(nèi)容讀取為字符串格式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!