导入复杂excel

导入复杂excel_第1张图片
前台页面:

//导入(预)他类Excel文件
导入复杂excel_第2张图片

function importTaLeiExcel() {
    $("#sCategoryTaleiImport").combobox({disabled : false});
    $('#importmentTaleiForm').form('clear');//清空原来的数据
    //$('#importmentAcceptListForm').form('clear');//清空原来的数据
    $('#ImportTaleiExcelDialog').dialog('open').dialog('setTitle', '导入(预)他项文件');
}
//确认导入预他excel按钮
function saveTaleiList() {
    debugger;
    var fileEle = document.getElementById("excelTaleifiles");
    if(fileEle.files.length == 0){
        $.messager.alert("提示", "请选择待上传的Excel文件", "info");
        return;
    }
    var formData = new FormData();
    for(var i=0; i

client中:

@RequestMapping(value = "/importTaleiList", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity> importTaleiList(@RequestParam(value = "file", required =       false) MultipartFile[] files,@RequestParam("categoryImp") Integer categoryImp,HttpServletRequest req){
	logger.trace(" importAcceptList  files: {}",  (files == null ? files.length : 0) );
	String url = baseUrl + "/archive/importTaleiList";
	ReadExcel readExcel = new ReadExcel();
	// 解析excel,获取上传的数据
	List  list=new ArrayList<>();
	for(int i=0;i map2 = new HashMap<>();
	//map.put(categoryImp,list);
	map2.put("category", categoryImp);
	map2.put("list", list);
	restTemplate.postForObject(url, map2, Map.class);
	return null;
}

server中:

@RequestMapping(value = “/importTaleiList”, method = RequestMethod.POST)

public Message importTaleiList(@RequestBody Map map,Authentication auth) throws Exception{
    String createUserId = null;
    String createUser = null;
    if(auth != null) {
        // 获取当前用户 (这里的auth即 AuthenticationTokenFilter中设置的 UsernamePasswordAuthenticationToken line: 51)
        logger.trace("user {}", auth.getPrincipal().getClass().getName());
        PortalUser user = (PortalUser) auth.getPrincipal();
        createUserId =  user.getId();
        createUser = user.getName();
    }
    Message message;
    try {
        logger.trace("map: {}", map);
        message = archiveService.importTaleiArchiveExcel(map, createUserId,createUser);
    } catch (RuntimeException e) {
        logger.error("出错了", e);
        return Messager.GetFailMessage(400, e.getMessage());
    }
    return message;
}

ReadExcel类中:

/**
 *  读取(预)他类 Excel文件,获取信息集合
 * @param mFile
 * @param type 要导入的excel是哪个类的,根据此字段区分你要导入的excel是什么
 * @return
 */
public List getExcelTaleiInfo(MultipartFile mFile){
	mFile.getSize();
	String fileName = mFile.getOriginalFilename();//获取文件名
	try {
		if(!validateExcel(fileName)){   //如果不合格
			return null;
		}
		boolean isExcel2003 = true;//根据文件名判断文件是2003版本还是2007版本
		if(isExcel2007(fileName)){
			isExcel2003 = false;
		}
		return createTaleiExcel(mFile.getInputStream(), isExcel2003);//创建excel
	}catch (Exception e){
		e.printStackTrace();
	}
	return null;
}
public List createTaleiExcel(InputStream is,boolean isExcel2003) throws Exception{
	Workbook wb=null;
	if(isExcel2003) {
		wb=new HSSFWorkbook(is);
	}else {
		wb=new XSSFWorkbook(is);
	}
	return readTaleiExcelValue(wb);
}
/**
 * 读取excel中的信息
 * @param wb
 * @return
 * @throws IOException
 */
@SuppressWarnings("deprecation")
private List readTaleiExcelValue(Workbook wb) throws IOException {
	//拿到第一个sheet
	Sheet sheet=wb.getSheetAt(0);
	String caseNo = null;
	List list = new ArrayList<>();
	for (int i=sheet.getFirstRowNum(); i 0) {
					caseNo = data;
				}
			}
		}
	for(int j=2; j<12; j += 2){
			String sjsj = getCellValue(row.getCell(j));
			String qz = getCellValue(row.getCell(j+1));
 	if(caseNo == null){
				//表头行
				if(!"收件收据号".equals(sjsj)){
					throw new RuntimeException("格式错误 row: " + i + ", col: " + j + ",期待[收件收据号]获得[" + sjsj + "]");
				}
				if(!"权证号".equals(qz)){
					throw new RuntimeException("格式错误 row: " + i + ", col: " + (j+1) + ",期待[权证号]获得[" + qz + "]");
				}
			}else{
				//数据行
				if(sjsj.length() > 0 && qz.length() > 0) {
					list.add(new TaleiArchive(caseNo, sjsj, qz));
				}
			}
		}
	}
	return  list;
}
/**
*@Description 获取单元格的内容
*@Param 
*@Return 
*@Author syh
*@Date 2019/1/29
*/
public String getCellValue(Cell cell){
	cell.setCellType(CellType.STRING);
	return cell.getStringCellValue();
}

你可能感兴趣的:(开发技术)