問題:導入excel表,若表格中為整數數字,不管單元格設置成數字格式還是文本格式,導入時都會出現小數點和0。
我遇到的問題是:一個名稱,做測試數據的時候做了純整形數字,發現了這個問題。
解決辦法:在代碼中判斷,格式化一下。
/**
* 處理導入小數點
*/
public static String numOfImport(HSSFCell cell) {
String value = cell.toString();
int i = cell.getCellType();
if (i == 1) {//字符串類型
return value;
} else {
String[] str = value.split("\\.");
if (str.length > 1) {
String str1 = str[1];
int m = Integer.parseInt(str1);
if (m == 0) {
return str[0];
} else {
return value;
}
}else{
return value;
}
}
}
ps補注:最近發現還有另一種方式:
/**
* 處理導入小數點
*/
public static String numOfImport(Cell cell) {
if (cell == null) {
return "";
}
String value = cell.toString();
int i = cell.getCellType();
if (i == 1) {//字符串類型
return value;
} else {
value = def.format(cell.getNumericCellValue());
return value;
}
}
