/** * 导入Excel数据 * * @param file * @return * @throws FileNotFoundException * @throws SQLException * @throws BiffException */ public void insertXslInfo(FormFile file) throws FileNotFoundException, SQLException {
jxl.Workbook wb = null; // sql语句list List insertList = new ArrayList(); UserListDao dao = new UserListDao(); try { wb = Workbook.getWorkbook(file.getInputStream()); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 得到工作薄 jxl.Sheet st = wb.getSheet(0);// 得到工作薄中的第一个工作表 Cell cell = st.getCell(0, 0);// 得到工作表的第一个单元格,即A1 String content = cell.getContents();// getContents()将Cell中的字符转为字符串 // 得到行数 int rowCount = st.getRows();
for (int i = 1; i < rowCount; i++) { // 用户信息bean UserBean userbean = new UserBean(); // 得到某一行的单元格 Cell[] ce = st.getRow(i); // jxl.Cell[] ce= st.getRow(i); String rowno = ce[0].getContents().toString(); // 用户编号 String userno = ce[1].getContents().toString(); userbean.setUserno(userno); // 用户姓名 String username = ce[2].getContents().toString(); userbean.setUsername(username); // 性别 String sex = ce[3].getContents().toString(); userbean.setSex(sex); // 出生日期 String birth = ce[4].getContents().toString(); userbean.setBirthday(birth); // 创建时间 String createtime = ce[5].getContents().toString(); userbean.setCreatetime(createtime); // 照片 String image = ce[6].getContents().toString(); userbean.setImage(image); insertList.add(i - 1, userbean); } try { dao.insXls(insertList); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } }