zoukankan      html  css  js  c++  java
  • java读取excel的内容(可保存到数据库中)

    //**
    poi jar包

    //

    public
    class ReadExcel { @SuppressWarnings("static-access") private static String getValue(HSSFCell hssfCell) { if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) { // 返回布尔类型的值 return String.valueOf(hssfCell.getBooleanCellValue()); } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) { // 返回数值类型的值 return String.valueOf(hssfCell.getNumericCellValue()); } else { // 返回字符串类型的值 return String.valueOf(hssfCell.getStringCellValue()); } } public static List<Tuserinfo> readXls() throws IOException { InputStream is = new FileInputStream("d:/app/1.xls"); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); Tuserinfo bean = null; List<Tuserinfo> list = new ArrayList<Tuserinfo>(); // 循环工作表Sheet for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); if (hssfSheet == null) { continue; } // 循环行Row for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { HSSFRow hssfRow = hssfSheet.getRow(rowNum); if (hssfRow != null) { bean = new Tuserinfo(); HSSFCell name = hssfRow.getCell(0); HSSFCell no = hssfRow.getCell(1); HSSFCell age = hssfRow.getCell(2); HSSFCell phone = hssfRow.getCell(3); HSSFCell cid = hssfRow.getCell(4); bean.setName(getValue(name)); bean.setCardcode(getValue(no)); bean.setPhone(getValue(phone)); bean.setCardno(getValue(cid)); bean.setUid(UUID.randomUUID().toString()); list.add(bean); } } } return list; }
    //测试
    public static void main(String[] args) { try { int i = 0; for (Tuserinfo u : readXls()) { System.out.println(u.toString()); i++; } System.out.println(i); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
  • 相关阅读:
    zoj 3693, 卡精度
    zoj 3690, 计数 dp , 快速幂
    hdu 1496,枚举
    zoj 2399, 哈弗曼编码
    poj 2560,mst
    poj 2007, 乱搞,计算几何
    bnu 29064, 期望 水题
    img,bg
    垂直居中,定位的方法
    .reverse ,join,split区分
  • 原文地址:https://www.cnblogs.com/syscn/p/7741889.html
Copyright © 2011-2022 走看看