工作中为了方便人员操作,定下来一版Excl,然后我负责识别读取成Json转存到库里遇到了两个小小的问题,发出来方便自己查询
问题1:数字过长变成科学计数法
问题2:文本框的数字读取后面自动加 ' .0 '
OK 第一步 Excl读取
/*读取excel*/
public List readExcel(File file, int headerIndex, int headType) {
List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
if (!fileNameFileter(file)) {
return null;
} else {
try {
WorkbookFactory factory = new WorkbookFactory();
Workbook workbook = factory.create(file);
Sheet sheet = workbook.getSheetAt(0);
Row headerRow = getHeaderRow(sheet, headerIndex);
FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
for (int r = headerIndex + 1; r < sheet.getLastRowNum() + 1; r++) {
Row dataRow = sheet.getRow(r);
Map<String, Object> map = new HashMap<String, Object>();
for (int h = 0; h < dataRow.getLastCellNum(); h++) {
String key = getHeaderCellValue(headerRow, h, headType);
Object value = getCellValue(dataRow, h, formulaEvaluator);
if (!key.equals("") && !key.equals("null") && key != null) {
map.put(key, value);
}
}
lists.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return lists;
}
/*文件过滤,只有表格才可以处理*/
public boolean fileNameFileter(File file) {
boolean endsWith = false;
if (file != null) {
String fileName = file.getName();
endsWith = fileName.endsWith(".xls") || fileName.endsWith(".xlsx");
}
return endsWith;
}
/*获取表的行*/
public Row getHeaderRow(Sheet sheet, int index) {
Row headerRow = null;
if (sheet != null) {
headerRow = sheet.getRow(index);
}
return headerRow;
}
/*获取表头的value*/
public String getHeaderCellValue(Row headerRow, int cellIndex, int type) {
Cell cell = headerRow.getCell(cellIndex);
String headerValue = null;
if (cell != null) {
if (HEADER_VALUE_TYPE_O == type) {
headerValue = cell.getRichStringCellValue().getString();
}
}
return headerValue;
}
/*获取单元格的值*/
public Object getCellValue(Row row, int cellIndex, FormulaEvaluator formulaEvaluator) {
Cell cell = row.getCell(cellIndex);
if (cell != null) {
switch (cell.getCellType()) {
//String
case Cell.CELL_TYPE_STRING:
return cell.getRichStringCellValue().getString();
//Number
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getNumericCellValue();
} else {
DecimalFormat df = new DecimalFormat("0");
return df.format(cell.getNumericCellValue());
}
//boolean
case Cell.CELL_TYPE_BOOLEAN:
System.out.println("Boolean");
return cell.getBooleanCellValue();
//公式
case Cell.CELL_TYPE_FORMULA:
System.out.println("FORMULA");
return formulaEvaluator.evaluate(cell).getNumberValue();
default:
return null;
}
}
return null;
}
测试
/*测试入口*/
public static void main(String[] args) {
File file = new File("D:\\b.xlsx");
test1 test1 = new test1();
List<Map<String,Object>> list= test1.readExcel(file, 0, 1);
for (Map<String,Object> obj:list) {
System.out.println(obj);
}
}
为什么不会出现科学计数和点零呢,。。。 看这点代码
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getNumericCellValue();
} else {
DecimalFormat df = new DecimalFormat("0");
return df.format(cell.getNumericCellValue());
}
判断value是否是数字类型,,。 是就格式下就完事。。。。唉 耽误了几个小时 想麻烦了