您的位置:首页 > 编程语言 > Java开发

java excel 导入 XSSFWorkbook

2016-11-08 17:19 197 查看
@RequestMapping(value="/importUserInfo")
public void ImportUserInfo(HttpServletRequest request,HttpServletResponse response){
try{
String pid = request.getParameter("pid");//产品id
ProductList pro = productService.selectByPrimaryKey(Byte.valueOf(pid));
String userExcelPath = request.getParameter("userExcelPath");//excel路径
ServletContext application = request.getSession().getServletContext();
String savePath = application.getRealPath("") ;
String saveUrl = savePath.replaceAll("\\\\", "/") + userExcelPath;//excel绝对路径
InputStream input = null;  
       XSSFWorkbook wb = null;  
       int result =0;
try {
input = new FileInputStream(saveUrl);
wb = new XSSFWorkbook(input);
for(int numSheet=0;numSheet<wb.getNumberOfSheets();numSheet++){
XSSFSheet sheet = wb.getSheetAt(numSheet);

int count = sheet.getLastRowNum();
for (int i = 1; i <= count; i++) {
XSSFRow row = sheet.getRow(i);
//学生信息
String school = getCellStringValue(row, 0); // 学校
String trueName = getCellStringValue(row, 1); // 姓名
String sex = getCellStringValue(row, 2); //性别
String usercard = getCellStringValue(row, 3); // 身份证
String type = getCellStringValue(row, 8);//用户类型
String tel = getCellStringValue(row, 9);//联系方式
if(StringUtil.isEmpty(usercard)){
continue;
}
//查询学生是否已存在
UserInfo uf = new UserInfo();
uf.setUsername(usercard);
List<UserInfo> slist = userInfoService.selectDataByInfo(uf);
OrderPeople orderPeople = new OrderPeople();
UserInfo uinfo = new UserInfo();//用户信息
Integer usize=slist.size();//已存在用户的记录数量
if(usize>0){//用户已存在只关联订单
uinfo = slist.get(0);
}
if("老师".equals(type)){
trueName+="(老师)";
}
String pwd = MD5.convertToMD5("123456");
if(usize==0){//用户不存在
//添加用户信息
uinfo.setUsername(usercard);
uinfo.setUsercard(usercard);
uinfo.setTel(tel);
uinfo.setCreatedate(new Date());
uinfo.setSchool(school);
uinfo.setTrueName(trueName);
uinfo.setSex(sex);
uinfo.setType(1);
uinfo.setPassword(pwd);
uinfo.setStatus("0");
uinfo.setCreatedate(new Date());
userInfoService.insertSelective(uinfo);
}
if("学生".equals(type)&&usize==0){
//父亲信息
String fatherName = getCellStringValue(row, 4);//父亲姓名
String fatherTel = getCellStringValue(row, 5);//父亲电话
if(StringUtil.isNotEmpty(fatherTel)){
Linkuser link = new Linkuser();
link.setTruename(fatherName);
link.setTel(fatherTel);
link.setRelation("父亲");
link.setUserid(uinfo.getId());
linkUserService.insertSelective(link);//添加学生与家长的关系信息
//判断父亲的信息是否已存在用户表中
UserInfo uif = new UserInfo();
uif.setUsername(fatherTel);//家长的用户账号是电话号码
List<UserInfo> listf = userInfoService.selectDataByInfo(uif);
if(listf.size()==0){//用户不存在,
UserInfo userF = new UserInfo();
userF.setUsername(fatherTel);
userF.setTrueName(fatherName);
userF.setPassword(pwd);
userF.setSex("男");
userF.setType(2);
userF.setCreatedate(new Date());
userF.setStatus("0");
userInfoService.insertSelective(userF);

}
}
//母亲信息
String matherName = getCellStringValue(row, 6);//母亲姓名
String matherTel = getCellStringValue(row, 7);//母亲电话

if(StringUtil.isNotEmpty(matherTel)){
Linkuser linkm = new Linkuser();
linkm.setTruename(matherName);
linkm.setTel(matherTel);
linkm.setRelation("母亲");
linkm.setUserid(uinfo.getId());
linkUserService.insertSelective(linkm);//添加学生与家长的关系信息
UserInfo uif = new UserInfo();
uif.setUsername(matherTel);
List<UserInfo> listm = userInfoService.selectDataByInfo(uif);
if(listm.size()==0){//家长用户不存在
UserInfo userM = new UserInfo();
userM.setUsername(matherTel);
userM.setTrueName(matherName);
userM.setPassword(pwd);
userM.setSex("女");
userM.setType(2);
userM.setStatus("0");
userM.setCreatedate(new Date());
userInfoService.insertSelective(userM);
}
}
}
orderPeople.setName(trueName);
orderPeople.setTel(tel);
orderPeople.setUsercard(usercard);
orderPeople.setSex(sex);
orderPeople.setNation(uinfo.getNation());
orderPeople.setSchool(uinfo.getSchool());
orderPeople.setAdress(uinfo.getAddress());
orderPeople.setInterest(uinfo.getInterest());
orderPeople.setSpeciality(uinfo.getSpeciality());
orderPeople.setUserid(uinfo.getId());
//订单号:唯一不重复编号规则:Pro+用户ID+年月日时分秒+四位随机数
String orderNum = Tools.makeOrderNumber("Pro"+uinfo.getId(), 4);
orderPeople.setOrderNum(orderNum);
result=orderPeopleService.insertSelective(orderPeople);//订单详情表
//总订单记录表
OrderList order = new OrderList();
order.setOrdernumber(orderNum);
order.setCount(1L);
order.setProid(Byte.valueOf(pid));
order.setCreatedate(new Date());
order.setPrice(pro.getPrice());
order.setUserid(uinfo.getId());
order.setIspay(1);
order.setPaytype(3);
result=orderListService.insertSelective(order);

}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}

if(result > 0){
new AjaxUtil(request, response).JsonType("200", "导入excel完成", "", "product", "", "", "",true);
}else{
new AjaxUtil(request, response).JsonType("200", "导入excel失败", "", "product", "", "", "",true);
}
}catch (Exception e) {
e.printStackTrace();
}
}
public static String getCellStringValue(XSSFRow row, int i) {
XSSFCell cell = row.getCell(i);
if(cell==null){
return"";
}
return cell.getRichStringCellValue().getString();
}

public static double getCellNumberValue(XSSFRow row, int i) {
XSSFCell cell = row.getCell(i);
if(cell == null){
return 0;
}else{
return cell.getNumericCellValue();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java excel springmvc