java实现导入Excel数据入库
引入依赖
<dependency>
<groupId>jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6</version>
</dependency>
将Excel表格数据通过java导入到mysql数据库,废话不多说,直接上代码
如果导入失败,需要检查文件,另存为 97-2003版本的.xls
数据导入之后可以直接做其他业务操作
如果有错误的地方请指出,便于及时改正
package com.yihu.jw.base.endpoint.patient;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yihu.jw.entity.base.patient.BasePatientDO;
import com.yihu.jw.patient.dao.BasePatientDao;
import com.yihu.jw.restmodel.web.ObjEnvelop;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
import com.yihu.jw.rm.base.BaseRequestMapping;
import com.yihu.utils.security.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import jxl.Sheet;
import jxl.Workbook;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;
@RestController
@RequestMapping(value = BaseRequestMapping.BasePatient.PREFIX)
@Api(value = "数据导入", description = "数据导入", tags = {"数据导入"})
public class ImportExcelDemo extends EnvelopRestEndpoint {
@Autowired
private BasePatientDao basePatientDao;
@RequestMapping(value = "/importPatientFromExcel", produces = "application/json;charset=UTF-8",method = RequestMethod.POST)
@ResponseBody
public ObjEnvelop importPatientFromExcel(HttpServletRequest request, @ApiParam(value = "文件", required = true)
@RequestParam(value = "file", required = true) MultipartFile file) {
List errorLs = new ArrayList<>();
List correctLs = new ArrayList<>();
List idcardList = new ArrayList<>();
Map<String, String> errorMsgMap = new HashMap<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<String> deviceCodes = new ArrayList<>();
try {
request.setCharacterEncoding("UTF-8");
InputStream inputStream = file.getInputStream();
Workbook rwb = Workbook.getWorkbook(inputStream);
Sheet[] sheets = rwb.getSheets();
int rows;
int row;
String name=null;
String sex;
String age;
String idcard;
String mobile;
String residentialArea;
String address;
String signTeam;
String label;
Sheet sheet = sheets[0];
rows = sheet.getRows();
for (int j = 1; j < rows; j++) {
if (sheet.getRow(j).length == 0) {
continue;
}
BasePatientDO basePatientVO = new BasePatientDO();
JSONObject infoMap = new JSONObject();
row = j;
name = sheet.getCell(0, row).getContents().trim();
sex = sheet.getCell(1, row).getContents().trim();
age = sheet.getCell(2, row).getContents().trim();
idcard = sheet.getCell(3, row).getContents().trim();
mobile = sheet.getCell(4, row).getContents().trim();
residentialArea = sheet.getCell(5, row).getContents().trim();
address = sheet.getCell(6, row).getContents().trim();
signTeam = sheet.getCell(7, row).getContents().trim();;
label = sheet.getCell(8, row).getContents().trim();;
if (StringUtils.isBlank(idcard)){
continue;
}
if (StringUtils.isBlank(name)){
errorMsgMap.put(idcard,"姓名不能为空");
continue;
}
if (StringUtils.isBlank(sex)){
sex = "3";
} else {
if (sex.equals("男")) {
sex = "1";
} else {
sex = "2";
}
}
if (StringUtils.isBlank(signTeam)){
errorMsgMap.put(idcard,"签约团队不能为空");
continue;
}
if (StringUtils.isBlank(label)){
errorMsgMap.put(idcard,"能力类型不能为空");
continue;
}
basePatientVO.setAddress(address);
basePatientVO.setDel("1");
basePatientVO.setIdcard(idcard);
basePatientVO.setSex(new Integer(sex));
basePatientVO.setName(name);
basePatientVO.setMobile(mobile);
basePatientVO.setArchiveStatus(3);
basePatientVO.setArchiveType(1);
String pw = idcard.substring(idcard.length()-6);
String salt = UUID.randomUUID().toString().substring(0,5);
basePatientVO.setPassword(MD5.md5Hex(pw + "{" + salt + "}"));
basePatientVO.setSalt(salt);
basePatientVO.setId(getUID());
infoMap.put("idcard",idcard);
infoMap.put("signTeam",signTeam);
infoMap.put("label",label);
infoMap.put("patient",getUID());
correctLs.add(basePatientVO);
idcardList.add(infoMap);
}
basePatientDao.save(correctLs);
Map<String, Object> map = new HashMap<>();
map.put("successNum", correctLs.size());
map.put("failedNum", rows-1 - correctLs.size() );
map.put("errorData", JSON.toJSONString(errorMsgMap, SerializerFeature.WriteMapNullValue));
System.out.println(map);
return ObjEnvelop.getSuccess("获取成功",map);
} catch (Exception e) {
e.printStackTrace();
return failedObjEnvelopException(e);
}
}
}
实体类代码就没必要上代码了,一张图
测试方式:postman进行测试
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)