使用poi解析excel

/**   * 解析Excel文件 :在此方法里只判断是否包含空行,不判断行中的必填元素,在下一级判断   */  public static List getExcelDataList(FileInputStream file) {   try {    InputStream is = new BufferedInputStream(file);    Workbook work;    work = Workbook.getWorkbook(is);    Sheet sheet = work.getSheet(0);    int rows = sheet.getRows();    int cols = sheet.getColumns();    List<Map> dataList = new ArrayList<Map>();    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

   // 第1行为标题列,从第2行开始导入 getCell(列, 行)    for (int i = 1; i < rows; i++) // 行    {     int flag = cols;     Map map = new HashMap();     for (int j = 0; j < cols; j++) // 行中的列     {      // 列名称      String colName = sheet.getCell(j, 0).getContents().trim();      // 列值      String colValue = sheet.getCell(j, i).getContents().trim();      Cell cc11 = ((jxl.Sheet) sheet).getCell(j, i);           if(cc11.getType()==CellType.NUMBER){       if(colValue.indexOf(",")>-1){        colValue = colValue.replace(",", "");       }            }      if(cc11.getType() == CellType.DATE)      {        DateCell datec11 = (DateCell)cc11;        Date strcc11  = datec11.getDate();        colValue = sdf.format(strcc11);      }

     if ("".equals(Convert.getValue(colValue))) {       flag--;      }      map.put(colName, colValue);     }     // 如果有空行,则直接退出     if (flag == 0) {      break;     }     dataList.add(map);    }    return dataList;   } catch (Exception e) {    // e.printStackTrace();   }   return null;  }

posted @ 2013-10-15 13:48  黄观鱼  阅读(305)  评论(0)    收藏  举报