public static List<ConsoleVo> readExcel(String fileName) { List<ConsoleVo> list = new ArrayList<ConsoleVo>(); try { Workbook book = Workbook.getWorkbook(new File(fileName)); Sheet[] sheets = book.getSheets(); for (Sheet s : sheets) { for (int i = 1; i < s.getRows(); i++) { ConsoleVo m = new ConsoleVo(); Cell[] cells = s.getRow(i); if (cells != null) { System.out.println(cells.length); m.setLoginName(StringUtil.getNotNullValueString(cells[0].getContents())); m.setUserName(StringUtil.getNotNullValueString(cells[1].getContents())); m.setDeptId(StringUtil.getNotNullValueString(cells[2].getContents())); m.setOrders(StringUtil.getNotNullValueString(cells[3].getContents())); list.add(m); } else { System.out.print("excel格式错误导入数据失败!"); return null; } } } book.close(); return list; } catch (Exception e) { e.printStackTrace(); } return null; }
public static void main(String args[]) { try { // 构建Workbook对象, 只读Workbook对象 // 直接从本地文件创建Workbook // 从输入流创建Workbook System.out.println("start load file-------------------------"); InputStream is = new FileInputStream("E:/account.xls"); // 创建输入 jxl.Workbook rwb = Workbook.getWorkbook(is); Sheet rs = rwb.getSheet(0); // 读取第一个sheet int colNum = rs.getColumns(); // 列数 int rowNum = rs.getRows(); // 行数 // System.out.println(colNum+" "+rowNum); // 创建account bean ApplicationContext ctx = new ClassPathXmlApplicationContext( new String[] { "spring/applicationContext-model-account.xml", "spring/applicationContext.xml" }); AccountService g = (AccountService) ctx.getBean("accountService"); for (int i = 1; i < rowNum; i++) { String no = rs.getCell(0, i).getContents(); String pass = rs.getCell(1, i).getContents(); String name = rs.getCell(2, i).getContents(); String maj = rs.getCell(3, i).getContents(); String grade = rs.getCell(4, i).getContents(); String cla = rs.getCell(5, i).getContents(); String idn = rs.getCell(6, i).getContents(); String email = rs.getCell(7, i).getContents(); String phone = rs.getCell(8, i).getContents(); String status = rs.getCell(9, i).getContents(); String role = rs.getCell(10, i).getContents(); ModelAccount ma = new ModelAccount(); ma.setAccountNo(no); ma.setAccountPassword(pass); ma.setAccountRealName(name); ma.setAccountMajority(maj); ma.setAccountGrade(grade); ma.setAccountClass(cla); ma.setAccountIdentification(idn); ma.setAccountEmail(email); ma.setAccountMobilePhone(phone); ma.setAccountStatus(0); ModelRoles ac = new ModelRoles(); ac.setRole_id(role); ma.setAccountRole(ac); g.saveOrUpdate(ma); // ma.setAccount_role(account_role) // System.out.println(no); } } catch (Exception e) { e.printStackTrace(); } }
/** @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { Workbook book = Workbook.getWorkbook(new File("测试.xls")); Sheet sheet = book.getSheet(0); int x = sheet.getRows(); int y = sheet.getColumns(); System.out.println("表格的列数为:" + x); System.out.println("表格的行数为:" + y); } catch (BiffException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }