poi excel導入 數字自動加小數點


問題:導入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;
}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM