java web导入excel表格

/**
  * excel导入学生基本信息
  */


 public String impStuBaseInfo(File stuFile, Student student)
   throws AppBaseException, ParseException {
  String result = "";
  List clientList = new ArrayList();
  InputStream is = null;
  try {
   is = new FileInputStream(stuFile);
  } catch (FileNotFoundException e) {
   result = "找不到指定的文件。";
   e.printStackTrace();
   return result;
  }
  // 工作薄
  Workbook rwb = null;
  try {
   rwb = Workbook.getWorkbook(is);
  } catch (Exception e) {
   result = "您提交的文件不是有效的Excel文档。";
   e.printStackTrace();
   return result;
  }
  Sheet organSheet1 = rwb.getSheet(0);
  // 基本信息
  result = this.buildStuBaseInfoFromSheet(clientList, organSheet1,
    student);
  if (!result.equals("")) {
   return result;
  }
  // 插入或修改
  try {
   insertStuBaseInfo(clientList);
  } catch (Exception e) {
   e.printStackTrace();
  }
  rwb.close();
  return result;
 }

 

//调用的方法

public String buildStuBaseInfoFromSheet(List clientList, Sheet sheet,
   Student student) {

  String result = "";
  int sheetRows = sheet.getRows();
  for (int i = 1; i < sheetRows; i++) {
   student = new Student();
   if (this.checkAllColumnsEmpty(sheet, i, 8) == true) {
    continue;
   }
   // 得到列
   Cell c0 = sheet.getCell(0, i);
   String content = null;
   content = c0.getContents();
   student.setId(Integer.parseInt(content));

   c0 = sheet.getCell(1, i);
   content = c0.getContents();
   student.setName(content);

   c0 = sheet.getCell(2, i);
   content = c0.getContents();
   student.setAge(Integer.parseInt(content));

   c0 = sheet.getCell(3, i);
   content = c0.getContents();
   System.out.println(content.toString());
   student.setSex(content);

   c0 = sheet.getCell(4, i);
   content = c0.getContents();
   student.setScore(Integer.parseInt(content));

   c0 = sheet.getCell(5, i);
   content = c0.getContents();
   student.setGrade(content);

   c0 = sheet.getCell(6, i);
   content = c0.getContents();
   student.setCnum(Integer.parseInt(content));

   c0 = sheet.getCell(7, i);
   content = c0.getContents();
   student.setCname(content);

   clientList.add(student);
  }

  return result;

 }

 

 

public void insertStuBaseInfo(List list) throws Exception {
  Iterator it = list.iterator();
  while (it.hasNext()) {
   Student model = (Student) it.next();
   insertStuBasicInfo(model);
  }

 }

 

 public void insertStuBasicInfo(Student model) {
  this.dao.getSqlMapClientTemplate().insert("addStudent", model);
  this.dao.getSqlMapClientTemplate().insert("addScore", model);
 }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值