controller层调用:
//melon-树形excel表格进数据库
@RequestMapping(value = "/test_excel", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
@ResponseBody
public String test_excel(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
if (file.isEmpty()) {
return NR.r(CodeDict.CODE_MESSAGE, -1, CodeDict.UPLOAD_EMPTY, null, null, 0, 0);
} else {
String fileName = setFile(file, "CheckItemExcell\\");
String path = System.getProperty("catalina.home") + "\\webapps\\CheckItemExcell\\" + fileName;
uploadService.testExcel(path);
return NR.r(CodeDict.CODE_MESSAGE, 1, 0, null, null, 0, 0);
}
}
public void testExcel(String path) {
Workbook wb = null;
try {
wb = WorkbookFactory.create(new File(path));
readExcel(wb, 0, 0, 0);
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException | org.apache.poi.openxml4j.exceptions.InvalidFormatException e) {
e.printStackTrace();
}
}
具体实现:
public void readExcel(Workbook wb, int sheetIndex, int startReadLine, int tailLine) {
List<HSEImportDto> lists = new ArrayList<>();//接收从Excel中读出来映射的实体
HSEImportDto hSEImportDto = new HSEImportDto();
HashMap<String, String> result = new HashMap<>();//存放最终结果
HashSet<HSEImportMapDto>hs=new HashSet<>();//存放最终放入数据库的集合
HashMap<String, Integer> index1 = new HashMap<>();//判断子节点序号大小
HashMap<String, Integer> index2 = new HashMap<>();//判断子节点序号大小
HashMap<String, Integer> index3 = new HashMap<>();//判断子节点序号大小
HashMap<String, String> mid = new HashMap<>();//存放最后一级节点的值
int num = 0;
DataFormatter dataFormat = new DataFormatter();//格式化
Sheet sheet = wb.getSheetAt(sheetIndex);
Row row = null;
for (int i = startReadLine; i < sheet.getLastRowNum() - tailLine + 1; i++) {
row = sheet.getRow(i);
if (isMergedRegion(sheet, i, 0)) hSEImportDto.setTheme(getMergedRegionValue(sheet, row.getRowNum(), 0));
else hSEImportDto.setTheme(dataFormat.formatCellValue(row.getCell(0)));
if (isMergedRegion(sheet, i, 1)) hSEImportDto.setItems(getMergedRegionValue(sheet, row.getRowNum(), 1));
else hSEImportDto.setItems(dataFormat.formatCellValue(row.getCell(1)));
if (isMergedRegion(sheet, i, 2)) hSEImportDto.setContent(getMergedRegionValue(sheet, row.getRowNum(), 2));
else hSEImportDto.setContent(dataFormat.formatCellValue(row.getCell(2)));
if (isMergedRegion(sheet, i, 3)) hSEImportDto.setAssessment(getMergedRegionValue(sheet, row.getRowNum(), 3));
else hSEImportDto.setAssessment(dataFormat.formatCellValue(row.getCell(3)));
if (isMergedRegion(sheet, i, 4)) hSEImportDto.setInstructions(getMergedRegionValue(sheet, row.getRowNum(), 4));
else hSEImportDto.setInstructions(dataFormat.formatCellValue(row.getCell(4)));
lists.add(hSEImportDto);
System.out.println(lists);//每一行数据都变成一个实体类
for (HSEImportDto pojo : lists) {
if (!result.containsKey(pojo.getTheme())) { //第一层集合中没有的话添加
System.out.println("0" + num);
result.put(pojo.getTheme(), "0" + num);//放入全局map中
num++;
index1.put(pojo.getTheme(), 0);//判断本身有几个孩子节点
}
if (!result.containsKey(pojo.getItems())) { //第二级结果中没有
Integer s = index1.get(pojo.getTheme());//得到编号
index1.put(pojo.getTheme(), s + 1);//编号加一放回
System.out.println("查看编号:" + index1.get(pojo.getTheme()));
result.put(pojo.getItems(), result.get(pojo.getTheme()) + "0" + (s + 1));
index2.put(pojo.getItems(), 0);//判断本身有几个孩子节点
}
if(!result.containsKey(pojo.getContent())){ //第三级没有
Integer s1 = index2.get(pojo.getItems());//得到编号
index2.put(pojo.getItems(), s1+1);//编号加一放回
result.put(pojo.getContent(), result.get(pojo.getItems())+"0"+(s1+1));
index3.put(pojo.getContent(), 0);//判断本身有几个孩子节点
}
if(!result.containsKey(pojo.getAssessment())){ //第四级没有
Integer s2 = index3.get(pojo.getContent());//得到编号
index3.put(pojo.getContent(), s2+1);//编号加一放回
result.put(pojo.getAssessment(), result.get(pojo.getContent())+"0"+(s2+1));
mid.put(pojo.getAssessment(),pojo.getInstructions());
}
}
}
//遍历map
for (Map.Entry<String, String> entry : result.entrySet()) {
HSEImportMapDto hSEImportMapDto=new HSEImportMapDto();
hSEImportMapDto.setStatus("启用");
hSEImportMapDto.setType("HSE审核");
hSEImportMapDto.setHseAuditSystemCode(entry.getValue());
hSEImportMapDto.setEvalueItem(entry.getKey());
if(mid.containsKey(entry.getKey())) hSEImportMapDto.setEvalueDescritption(mid.get(entry.getKey()));
hs.add(hSEImportMapDto);
//System.out.println(hSEImportMapDto);
}
List<HSEImportMapDto> pojos=new ArrayList<>();
pojos.addAll(hs);
//批量插入数据库
checkListDao.banchHSEImportMap(pojos);
}
/**
* 获取合并单元格的值
*/
public String getMergedRegionValue(Sheet sheet, int row, int column) {
int sheetMergeCount = sheet.getNumMergedRegions();
for (int i = 0; i < sheetMergeCount; i++) {
CellRangeAddress ca = sheet.getMergedRegion(i);
int firstColumn = ca.getFirstColumn();
int lastColumn = ca.getLastColumn();
int firstRow = ca.getFirstRow();
int lastRow = ca.getLastRow();
if (row >= firstRow && row <= lastRow) {
if (column >= firstColumn && column <= lastColumn) {
Row fRow = sheet.getRow(firstRow);
Cell fCell = fRow.getCell(firstColumn);
return getCellValue(fCell);
}
}
}
return null;
}
/**
* 判断合并了行
*/
private boolean isMergedRow(Sheet sheet, int row, int column) {
int sheetMergeCount = sheet.getNumMergedRegions();
for (int i = 0; i < sheetMergeCount; i++) {
CellRangeAddress range = sheet.getMergedRegion(i);
int firstColumn = range.getFirstColumn();
int lastColumn = range.getLastColumn();
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
if (row == firstRow && row == lastRow) {
if (column >= firstColumn && column <= lastColumn) {
return true;
}
}
}
return false;
}
/**
* 判断指定的单元格是否是合并单元格
* @param sheet
* @param row 行下标
* @param column 列下标
* @return
*/
private boolean isMergedRegion(Sheet sheet, int row, int column) {
int sheetMergeCount = sheet.getNumMergedRegions();
for (int i = 0; i < sheetMergeCount; i++) {
CellRangeAddress range = sheet.getMergedRegion(i);
int firstColumn = range.getFirstColumn();
int lastColumn = range.getLastColumn();
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
if (row >= firstRow && row <= lastRow) {
if (column >= firstColumn && column <= lastColumn) {
return true;
}
}
}
return false;
}
/**
* 判断sheet页中是否含有合并单元格
* @param sheet
* @return
*/
private boolean hasMerged(Sheet sheet) {
return sheet.getNumMergedRegions() > 0 ? true : false;
}
/**
* 合并单元格
* @param sheet
* @param firstRow 开始行
* @param lastRow 结束行
* @param firstCol 开始列
* @param lastCol 结束列
*/
private void mergeRegion(Sheet sheet, int firstRow, int lastRow, int firstCol, int lastCol) {
sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol));
}
/**
* 获取单元格的值
*
* @param cell
* @return
*/
public String getCellValue(Cell cell) {
if (cell == null)
return "";
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
return cell.getStringCellValue();
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
return String.valueOf(cell.getBooleanCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
return cell.getCellFormula();
} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
return String.valueOf(cell.getNumericCellValue());
}
return "";
}