首先 写这个方法时间不多,然后写的比较粗糙,如果大家想要使用可以完善它优化它,当然如果我有时间也会去完善它优化它把他打成jar包来使用
贴代码
/*控制器*/
public ModelAndView ExcelImport(@RequestParam(value="file",required = false)MultipartFile files, HttpServletRequest request){ getExcelInfo(files) }
/* * 解析Excel对象 * */ public int getExcelInfo(MultipartFile mFile) { String fileName = mFile.getOriginalFilename();// 获取文件名 int falg=0; try { if (!validateExcel(fileName)) {// 验证文件名是否合格 String errorMsg = "文件不是excel格式"; return -1; } boolean isExcel2003 = true;// 根据文件名判断文件是2003版本还是2007版本 if (isExcel2007(fileName)) { isExcel2003 = false; } falg=createExcel(mFile.getInputStream(), isExcel2003); } catch (IOException e) { e.printStackTrace(); } return falg; }
/** * 根据excel里面的内容读取客户信息 * * @param is 输入流 * @param isExcel2003 excel是2003还是2007版本 * @return * @throws IOException */ public int createExcel(InputStream is, boolean isExcel2003) { Workbook wb = null; int falg=0; try { if (isExcel2003) {// 当excel是2003时,创建excel2003 wb = new HSSFWorkbook(is); } else {// 当excel是2007时,创建excel2007 wb = new XSSFWorkbook(is); } Class clazz = null; try { //重要的信息在这里 clazz = Class.forName("com.hearing.cloud.background.model.实体类"); } catch (ClassNotFoundException e) { e.printStackTrace(); } //核心方法 List<Object> obj=readExcelValue(wb,clazz);// 读取Excel里面的数据 // 这个方法我就不写了 //-------------------因为你已经在上面拿到了excel的数据 obj 在使用数据的时候可以转换为实体类 //就像这样 for(Object i:obj){ os=(实体类)i; } } catch (IOException e) { e.printStackTrace(); } return falg; }
//这是个粗糙的方法
//通用导入核心 private List<Object> readExcelValue(Workbook wb,Class clazz) { List<Object> results = new ArrayList<Object>(); try { // 得到第一个shell Sheet sheet = wb.getSheetAt(0); // 得到Excel的行数 int totalRows = sheet.getPhysicalNumberOfRows(); // 得到Excel的列数(前提是有行数) int totalCells = 0; if (totalRows > 1 && sheet.getRow(0) != null) { totalCells = sheet.getRow(0).getPhysicalNumberOfCells(); } //获取标题栏 title Map<Integer, String> TitleList = new HashMap<Integer, String>(); for (int Rtitle = 0; Rtitle < 1; Rtitle++) {//循环第一行 Row row = sheet.getRow(Rtitle); for (int Ctitle = 0; Ctitle < totalCells; Ctitle++) { //循环第一行的每一列 Cell cell = row.getCell(Ctitle); int type = cell.getCellType(); if (type == Cell.CELL_TYPE_STRING) { TitleList.put(Ctitle, cell.getStringCellValue()); } else if (type == Cell.CELL_TYPE_NUMERIC || type == Cell.CELL_TYPE_FORMULA) { TitleList.put(Ctitle, String.valueOf(cell.getNumericCellValue())); } else if (type == Cell.CELL_TYPE_BOOLEAN) { TitleList.put(Ctitle, String.valueOf(cell.getBooleanCellValue())); } } } // 循环内部数据 跳过title 加入到实体类 for (int Rdata = 1; Rdata < totalRows; Rdata++) { Row row = sheet.getRow(Rdata); Object obj = clazz.newInstance(); for (int Cdata = 0; Cdata < totalCells; Cdata++) { String value = ""; Cell cell = row.getCell(Cdata); String types=obj.getClass().getDeclaredField(TitleList.get(Cdata)).getType().toString(); if(types.equals("class java.lang.String")){ switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: value=cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_FORMULA: value=cell.getCellFormula(); break; case HSSFCell.CELL_TYPE_NUMERIC: HSSFDataFormatter dataFormatter = new HSSFDataFormatter(); String cellFormatted = dataFormatter.formatCellValue(cell); value=cellFormatted; break; case HSSFCell.CELL_TYPE_ERROR: break; } if(value==null||"".equals(value)){ continue; } obj.getClass().getMethod("set"+toUpperCaseFirstOne(TitleList.get(Cdata)),String.class).invoke(obj,value); } if(types.equals("double")){ HSSFDataFormatter dataFormatter = new HSSFDataFormatter(); value = dataFormatter.formatCellValue(cell); if(value==null||"".equals(value)){ continue; } obj.getClass().getMethod("set"+toUpperCaseFirstOne(TitleList.get(Cdata)),double.class).invoke(obj,Double.valueOf(value)); } if(types.equals("int")){ HSSFDataFormatter dataFormatter = new HSSFDataFormatter(); value = dataFormatter.formatCellValue(cell); if(value==null||"".equals(value)){ continue; } obj.getClass().getMethod("set"+toUpperCaseFirstOne(TitleList.get(Cdata)),int.class).invoke(obj,Integer.valueOf(value)); } if(types.equals("boolean")){ value=cell.getBooleanCellValue()+""; if(value==null||"".equals(value)){ continue; } obj.getClass().getMethod("set"+toUpperCaseFirstOne(TitleList.get(Cdata)),boolean.class).invoke(obj,Boolean.valueOf(value)); } } results.add(obj); } } catch (Exception e) { e.printStackTrace(); } return results; }