|
//这是3.7.1的代码 在功能测试里导入一对多数据不能实现
@RequestMapping(params = "importExcel", method = RequestMethod.POST)
@ResponseBody
@SuppressWarnings("all")
public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
String message = "上传成功";
AjaxJson j = new AjaxJson();
String configId = request.getParameter("tableName");
String jversion = cgFormFieldService.getCgFormVersionByTableName(configId);
Map<String, Object> configs = configService.queryConfigs(configId, jversion);
//数据库中版本号
String version = (String) configs.get(CgAutoListConstant.CONFIG_VERSION);
List<CgFormFieldEntity> lists = (List<CgFormFieldEntity>) configs.get(CgAutoListConstant.FILEDS);
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
MultipartFile file = entity.getValue();// 获取上传文件对象
//上传文件的版本号
String docVersion = getDocVersion(file.getOriginalFilename());
if (docVersion.equals(version)) {
List<Map<String, Object>> listDate;
//--author:luobaoli---------date:20150615--------for: 处理service层抛出的异常
try {
//读取excel模版数据
//--author:JueYue---------date:20150622--------for: 修改为EasyPoi 的导入
ImportParams params = new ImportParams();
params.setDataHanlder(new CgFormExcelHandler(lists));
listDate = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, params);
//--author:JueYue---------date:20150622--------for: 修改为EasyPoi 的导入
if (listDate == null) {
message = "识别模版数据错误";
logger.error(message);
} else {
//--author:zhoujf---start------date:20170207--------fornline表单物理表查询数据异常处理
configId = configId.split("__")[0];
for (Map<String, Object> map : listDate) {
map.put("id", UUIDGenerator.generate());
dataBaseService.insertTable(configId, map);
}
message = "文件导入成功!";
}
} catch (Exception e) {
message = "文件导入失败!";
logger.error(ExceptionUtil.getExceptionMessage(e));
}
//--author:luobaoli---------date:20150615--------for: 处理service层抛出的异常
} else {
message = "模版文件版本和表达不匹配,请重新下载模版";
logger.error(message);
}
}
j.setMsg(message);
return j;
}
//这是3.7.3的代码 在功能测试里导入一对多数据不能实现
@RequestMapping(params = "importExcel", method = RequestMethod.POST)
@ResponseBody
@SuppressWarnings("all")
public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
String message = "上传成功";
AjaxJson j = new AjaxJson();
String configId = request.getParameter("tableName");
String jversion = cgFormFieldService.getCgFormVersionByTableName(configId);
Map<String, Object> configs = configService.queryConfigs(configId, jversion);
//数据库中版本号
String version = (String) configs.get(CgAutoListConstant.CONFIG_VERSION);
List<CgFormFieldEntity> lists = (List<CgFormFieldEntity>) configs.get(CgAutoListConstant.FILEDS);
Object subTables = configs.get("subTables");
List<String> subTabList = new ArrayList();
if (null != subTables) {
subTabList.addAll(Arrays.asList(subTables.toString().split(",")));
}
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
MultipartFile file = entity.getValue();// 获取上传文件对象
//上传文件的版本号
String docVersion = getDocVersion(file.getOriginalFilename());
if (docVersion.equals(version)) {
List<Map<String, Object>> listDate;
//--author:luobaoli---------date:20150615--------for: 处理service层抛出的异常
try {
//读取excel模版数据
//--author:JueYue---------date:20150622--------for: 修改为EasyPoi 的导入
ImportParams params = new ImportParams();
params.setDataHanlder(new CgFormExcelHandler(lists));
listDate = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, params);
//--author:JueYue---------date:20150622--------for: 修改为EasyPoi 的导入
if (listDate == null) {
message = "识别模版数据错误";
logger.error(message);
} else {
//--author:zhoujf---start------date:20170207--------fornline表单物理表查询数据异常处理
configId = configId.split("__")[0];
String mainId = "";
for (Map<String, Object> map : listDate) {
//标志是否为主表数据
boolean isMainData = false;
Set<String> keySet = map.keySet();
Map mainData = new HashMap();
for (String key : keySet) {
if (key.indexOf("$subTable$")==-1) {
if (key.indexOf("$mainTable$")!=-1 && StringUtils.isNotEmpty(map.get(key).toString())) {
isMainData = true;
mainId = UUIDGenerator.generate();
}
mainData.put(key.replace("$mainTable$", ""), map.get(key));
}
}
map.put("$mainTable$id", mainId);//为子表准备
if (isMainData) {
//处理字典项
dealDicForImport(mainData, lists);
mainData.put("id", mainId);//主表数据
dataBaseService.insertTable(configId, mainData);
}
}
//导入子表数据,如果有
for (String subConfigId: subTabList) {
Map<String, Object> subConfigs = configService.queryConfigs(subConfigId, jversion);
List<CgFormFieldEntity> subLists = (List<CgFormFieldEntity>) subConfigs.get(CgAutoListConstant.FILEDS);
//将表头字段替换成数据库中对应的字段名
String configName = subConfigs.get("config_name").toString();
for (Map<String, Object> map : listDate) {
//标志是否为子表数据
boolean isSubData = false;
Map subData = new HashMap();
for (CgFormFieldEntity fieldEntity: subLists) {
String mainTab = fieldEntity.getMainTable();
String mainField = fieldEntity.getMainField();
boolean isForeignKey = configId.equals(mainTab) && StringUtil.isNotEmpty(mainField);
String tempKey = configName+"_"+fieldEntity.getContent();
//已经考虑兼容多个外键的情况
if(isForeignKey){
subData.put(fieldEntity.getFieldName(), map.get("$mainTable$"+mainField));
}
Object subObj = map.get("$subTable$"+tempKey);
if (null != subObj && StringUtils.isNotEmpty(subObj.toString())) {
isSubData = true;
//System.out.println(tempKey+"=>"+fieldEntity.getFieldName()+"--"+subObj);
subData.put(fieldEntity.getFieldName(), subObj);
}
}
//设置子表记录ID
if (isSubData) {
//处理字典项
dealDicForImport(subData, subLists);
subData.put("id", UUIDGenerator.generate());
dataBaseService.insertTable(subConfigId, subData);
}
}
}
message = "文件导入成功!";
}
} catch (Exception e) {
message = "文件导入失败!";
logger.error(ExceptionUtil.getExceptionMessage(e));
}
//--author:luobaoli---------date:20150615--------for: 处理service层抛出的异常
} else {
message = "模版文件版本和表达不匹配,请重新下载模版";
logger.error(message);
}
}
j.setMsg(message);
return j;
}
//这是我的代码
List<TempFieldEntity> fields = new ArrayList<>();
tempDataService.findTempFields(fields, tableName, import_temp, import_temp0, null);
AjaxJson j = new AjaxJson();
String message = null;
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
MultipartFile file = entity.getValue();// 获取上传文件对象
ImportParams params = new ImportParams();
List<Map<String, Object>> listData;
try {
List<String> fieldNames = new ArrayList<>();
tempDataService.getFieldNameList(fieldNames, tableName, import_temp);
CcFormExcelHandler excelHandler = new CcFormExcelHandler(fields);
Map linkmanMap = new HashMap<>();
if (tableName.equals(CUSTOMER_TABLE)) {
linkmanMap .put(mutiLangService.getLang("common.surname"), "fullName");
linkmanMap .put(mutiLangService.getLang("common.sex"), "sex");
linkmanMap .put(mutiLangService.getLang("common.mobile"), "telephone");
linkmanMap .put(mutiLangService.getLang("phone.belongto.place"), "mobileAttribution");
linkmanMap .put(mutiLangService.getLang("common.position"), "post");
linkmanMap .put(mutiLangService.getLang("common.mail"), "email");
linkmanMap .put("QQ", "qq");
linkmanMap .put(mutiLangService.getLang("common.address"), "proCityArea");
linkmanMap .put(mutiLangService.getLang("lky.detailed.address"), "address");
// 这段自己写的,报错了
Map map = new HashMap();
map.put("联系人",linkmanMap);
excelHandler.addMap(map);
// 如果这样是一对一的
//excelHandler.addMap(linkmanMap);
}
params.setDataHanlder(excelHandler);
listData = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, params);
if (listData == null) {
message = "识别模版数据错误";
logger.error(message);
} else {
////////////////////////////////////////////////////////////////////////////
求指教如何用map方式导入一对多数据????!!!!!!!!!!!
|
|