poi excel导入 数字自动加小数点

时间:2023-03-09 09:30:52
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;
}

}