Excel数据入库,是通过存储过程判断字段是否为空,为空时给出提示,符合条件时修改数据字典数据入库。
Controller类:
@SuppressWarnings("unchecked")
@RequestMapping(params = "importExcel", method = RequestMethod.POST)
@ResponseBody
public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
String vposition = request.getParameter("vposition");
AjaxJson j = new AjaxJson();
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
MultipartFile file = entity.getValue();// 获取上传文件对象
CommonsMultipartFile cf = (CommonsMultipartFile) file;
DiskFileItem fi = (DiskFileItem) cf.getFileItem();//用来转换file文件格式
ImportParams params = new ImportParams();
params.setTitleRows(2);
params.setHeadRows(2);
params.setNeedSave(true);
try {
String flag = teaProductentityService.fromExcelToList(request, fi.getName(), vposition);
if (flag.equals("-1")) {
j.setMsg("文件导入失败!");
} else {
j.setMsg("文件导入成功!");
}
} catch (Exception e) {
j.setMsg("文件导入失败!");
logger.error(ExceptionUtil.getExceptionMessage(e));
} finally {
try {
file.getInputStream().close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return j;
}
ServiceImpl实现类:
@Override
public String fromExcelToList(HttpServletRequest request, String fileName, String vposition) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
MultipartHttpServletRequest mpt = (MultipartHttpServletRequest) request;
MultipartFile file = mpt.getFile("file");
String userId = ResourceUtil.getSessionUser().getId();
String result = "";
String depart = ResourceUtil.getSessionUser().getDepartid();
String tempTablePrefix = "tmp_imp_" + sdf.format(new Date()) + Math.round(Math.random() * 1000);
try {
String[] paramsStr = new String[] { "物料编码,vproduct_id,0", "设备编号,vserial_no,1",
"错误信息,remark,24", "错误提示,flag,25", "id,id,26" };
String[] inputValue = new String[] { tempTablePrefix };
int[] outValue = new int[] { java.sql.Types.FLOAT, java.sql.Types.VARCHAR };
// 存储过程名
// String procName = "product_entity";
String procName = "product_real";
result = new HssfHelper().importExcelToDB2(request, file, tempTablePrefix, paramsStr, procName, 1, true,
true, 3, inputValue, outValue, "upload/monitor");
if (result.equals("-1")) {
String sql = "select * from " + tempTablePrefix;
List<TeaProductEntityPo> productStructureList = namedParameterJdbcTemplate.query(sql, new HashMap(),
new BeanPropertyRowMapper<>(TeaProductEntityPo.class));
// 计算出需要多少个sheet
int sheetCount = 1;
if (productStructureList != null && productStructureList.size() > 0) {
sheetCount = productStructureList.size() / 65000 == 0 ? 1
: (productStructureList.size() % 65000 >