批量导入

//批量导入
@Transactional(rollbackFor = BusinessException.class)
public void doImport(MultipartFile file) throws BusinessException {
List<Document> documentList = new ArrayList<Document>();
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date bgnDate = new Date();
System.out.println("开始导入时间:" + dateFormater.format(bgnDate));
try {
List<Document> list = excelTransFromList(file);
for (int i = 0; i < list.size(); i++) {
Document document = list.get(i);

//查询所属医院
if (document.getCode() != null && !"".equals(document.getCode())) {
if ("无".equals(document.getCode())) {
document.setCode("0");
}
} else {
document.setCode("0");
}
document.setGmtCreate(new Date());
documentList.add(document);
}
try {
iDocumentMapper.insertBatch(documentList);
} catch (Exception e) {
throw new BusinessException(BusinessException.CODE_SAVE_ERROR, BusinessException.MSG_SAVE_ERROR);
}
} catch (BusinessException e) {
throw new BusinessException(e.getErrCode(), e.getErrMsg());
}
}

/**
* @throws BusinessException
* @Title: excelTransFromList
* @Description: 模板转换成对象list
* @param: MultipartFile
* @return: List<Book>
*/
public List<Document> excelTransFromList(MultipartFile file) throws BusinessException {
InputStream in;
try {
in = file.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new BusinessException(BusinessException.CODE_EXCEL_VESION_ERROR, BusinessException.MSG_EXCEL_VESION_ERROR);
}
//获取sheetName名字
String sheetName = "专业文献导入";
// excel的表头与文字对应,获取excel表头
LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put("标题", "title");
map.put("作者", "author");
map.put("刊名", "journalName");
map.put("年", "year");
map.put("卷", "file");
map.put("期", "period");
map.put("页码", "pageNum");
map.put("摘要", "summary");
map.put("关键字", "keyword");
map.put("作者单位", "authorUnit");
map.put("全文附件", "fileUrl");
map.put("专题类型", "specialId");
map.put("组织机构编码", "code");
map.put("组织机构名称", "orgName");
map.put("语言", "language");
map.put("创建人", "createrId");
//获取组合excle表头数组,防止重复用的
String[] uniqueFields = new String[]{"标题", "作者", "刊名", "年", "卷",
"期", "页码", "摘要", "关键字", "作者单位", "全文附件", "专题类型", "组织机构编码", "组织机构名称", "语言", "创建人"};
//excel转化成的list集合
//调用excle共用类,转化成list
List<Document> list = ExcelUtil.excelToList(in, sheetName, Document.class, map, uniqueFields, "document");
return list;
}
原文地址:https://www.cnblogs.com/vlsion/p/7423803.html