1.excel工具类ExcelUtil.java
package cn.deacy.work.util; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import java.io.InputStream; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Excel相关处理 */ public class ExcelUtil<T> { /** 总行数 */ private int totalRows = 0; /** 总列数 */ private int totalCells = 0; /** 错误信息 */ private String errorInfo; public int getTotalRows() { return totalRows; } public void setTotalRows(int totalRows) { this.totalRows = totalRows; } public void setTotalCells(int totalCells) { this.totalCells = totalCells; } public String getErrorInfo() { return errorInfo; } public void setErrorInfo(String errorInfo) { this.errorInfo = errorInfo; } /** * @描述:读取数据 * @时间:2014-08-29 下午16:50:15 * @参数:@param Workbook * @参数:@return * @返回值:List<List<String>> */ private List<List<String>> read(Workbook wb) { List<List<String>> dataLst = new ArrayList<List<String>>(); /** 得到第一个shell */ Sheet sheet = wb.getSheetAt(0); /** 得到Excel的行数 */ this.totalRows = sheet.getPhysicalNumberOfRows(); /** 得到Excel的列数 */ if (this.totalRows >= 1 && sheet.getRow(0) != null) { this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells(); } /** 循环Excel的行 */ for (int r = 0; r < this.totalRows; r++) { Row row = sheet.getRow(r); if (row == null) { continue; } List<String> rowLst = new ArrayList<String>(); /** 循环Excel的列 */ for (int c = 0; c < this.getTotalCells(); c++) { Cell cell = row.getCell(c); String cellValue = ""; if (null != cell) { // 以下是判断数据的类型 switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: // 数字 DecimalFormat df = new DecimalFormat("0"); cellValue = df.format(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_STRING: // 字符串 cellValue = cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean cellValue = cell.getBooleanCellValue() + ""; break; case HSSFCell.CELL_TYPE_FORMULA: // 公式 cellValue = cell.getCellFormula() + ""; break; case HSSFCell.CELL_TYPE_BLANK: // 空值 cellValue = ""; break; case HSSFCell.CELL_TYPE_ERROR: // 故障 cellValue = "非法字符"; break; default: cellValue = "未知类型"; break; } } rowLst.add(cellValue); } /** 保存第r行的第c列 */ dataLst.add(rowLst); } return dataLst; } /** * @描述:得到总列数 * @时间:2014-08-29 下午16:27:15 * @参数:@return * @返回值:int */ public int getTotalCells() { return totalCells; } public List<Map<String, String>> importExcel(InputStream is) throws Exception { List<Map<String, String>> dataList = new ArrayList<>(); try { HSSFWorkbook workbook = new HSSFWorkbook(is); List<List<String>> read = this.read(workbook); if (read != null) { for (int i = 1; i < read.size(); i++) { Map<String, String> map = new HashMap<>(); for (int j = 0; j < read.get(i).size(); j++) { map.put(read.get(0).get(j),read.get(i).get(j)); } dataList.add(map); } } } catch (Exception e) { e.printStackTrace(); } return dataList; } }
2.web层调用,或者写入到serviceimpl
@RequestMapping("importData") public BaseResult importData(@RequestParam("file") MultipartFile file, HttpServletRequest request) { BaseResult result = tWorkUcsaInfoService.importExcel(file,request);
try {
if (file != null) {
FileInputStream inputStream = (FileInputStream) file.getInputStream();
ExcelUtil excelUtil = new ExcelUtil();
//获取到excel里面的数据
List<Map<String,String>> list = excelUtil.importExcel(inputStream);
TWorkUcsaInfoExample workUcsaInfoExample = new TWorkUcsaInfoExample();
List<TWorkUcsaInfo> example = tWorkUcsaInfoMapper.selectByExample(workUcsaInfoExample);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; i < list.size(); i++) {
Map<String, String> map = list.get(i);
for (int j = 0; j <example.size() ; j++) {
}
TWorkUcsaInfo ucsaInfo = new TWorkUcsaInfo();
ucsaInfo.setXm(map.get("姓名"));
ucsaInfo.setSfzh(map.get("身份证号"));
ucsaInfo.setGsmc(map.get("公司名称"));
tWorkUcsaInfoService.insert(ucsaInfo);
}
}
} catch (Exception e) {
e.printStackTrace();
result.setMessage("失败");
}
return result;
}