使用Java读取excel的时候,小数自动转为为不精确的小数点了。
读取excel的代码
public static List readExcel(File file) throws Exception {
//初始化结果集
List dataList = new ArrayList();
//获得Workbook工作薄对象
Workbook workbook = getWorkBook(file);
if (workbook == null)
throw new Exception("读取excel数据失败,workbook build null。");
for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
//获得当前sheet工作表
Sheet sheet = workbook.getSheetAt(sheetNum);
if (sheet == null)
continue;
if (sheet.getNumMergedRegions() > 0)
setSheetMergedRegionsValue(sheet);
//获得当前sheet的开始行
int firstRowNum = sheet.getFirstRowNum();
//获得当前sheet的结束行
int lastRowNum = sheet.getLastRowNum();
//循环所有行
for (int rowNum = firstRowNum; rowNum <= lastRowNum; rowNum++) {
//获得当前行
Row row = sheet.getRow(rowNum);
if (row == null)
continue;
//获得当前行的开始列
int firstCellNum = row.getFirstCellNum();
//获得当前行的列数
int lastCellNum = row.getPhysicalNumberOfCells();
String[] cells = new String[row.getPhysicalNumberOfCells()];
//循环当前行
for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {
if (cellNum < 0)
continue;
Cell cell = row.getCell(cellNum);
if (cell == null)
continue;
cell.setCellType(Cell.CELL_TYPE_STRING);
cells[cellNum] = cell.getStringCellValue();
}
// 如果第一列没有数据,continue掉
if (cells.length > 0 && StringUtils.isBlank(cells[0]))
continue;
dataList.add(cells);
}
}
//workbook.close();
return dataList;
}
excel原始文件
读取后的效果
请教这种怎么解决?