poi读取excel表格类型转换问题(java读取excel20072010版本)

poi读取excel表格类型转换问题(java读取excel2007--2010版本)  

 
 

一下代码只是类型判断部分的代码,如有需要自己进行封装,代码只是部分,其他自己发挥,自己可以提取转换类型的方法

private ArrayList read2007(){//读取2007和2010版本的Excel
  ArrayList rtn = new ArrayList(); //定义存放返还的集合的实体
  try {
   XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(file));//根据上传的file获取excel表格的工作空间
   XSSFSheet sheet = workbook.getSheetAt(0);//获得默认的sheet的页面
 // 值建对应
   XSSFRow row1 = sheet.getRow(1);//获取sheet页的第一行
 for (int i = 0; i < row1.getLastCellNum(); i++) {//变量第一行所有的列
  XSSFCell cell = row1.getCell(i);//得到第一行的每一列
  String value = cell.getRichStringCellValue().getString();//得到每一列的值不管是什么类型都转换成字符串
  String mapValue = (String) hashtable.get(value.trim());//这个是我们项目里面用的,取出所有对应标题的值
  if(mapValue!=null){
    hashtable.put(i, mapValue);//列名与值相对应进行存放
  }
   }

   for (int i = 2; i <= sheet.getLastRowNum(); i++) {//从第二行开始读,遍历到最后一行,2代表从第二行开始,第一 行为标题
    XSSFRow row = sheet.getRow(i);//得到每一行
    Object obj = classType.newInstance();
    for (int j = 0; j < row.getLastCellNum(); j++) {//得到每一行的每一列
     String mapvalue = (String) hashtable.get(j);//hashtable是定义的实体,从实体里取出与单元格相对应的值
     if(mapvalue==null){
      continue;
     }
     XSSFCell cell = row.getCell(j);//得到每一行的每一列
     Object cellValue = null;
     String[] valueArr = mapvalue.split(",");//截取从实体取出来的值
     // 根据excel中单元格内数字的属性,来用不同的方法取得有效值
     switch (cell.getCellType()) {
     case XSSFCell.CELL_TYPE_STRING:
      cellValue = cell.getRichStringCellValue().getString();
      if (((String)cellValue).trim().equals("")
        || ((String)cellValue).trim().length() <= 0) {
       cellValue = "";
      }
      cellValue = inputEncode((String)cellValue);
      break;
     case XSSFCell.CELL_TYPE_NUMERIC:
      if (valueArr.length == 2 && valueArr[1].equals("date")) {
       cellValue = cell.getDateCellValue();;
      } 
      if (valueArr.length == 2 && valueArr[1].equals("timestamp")) {
       Date date = cell.getDateCellValue();
       SimpleDateFormat format1= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       String time=format1.format(date);
       cellValue= Timestamp.valueOf(time);
      }else { // 如果长度为2,说明此列为默认字符串类型
       BigDecimal big = new BigDecimal(cell
         .getNumericCellValue());
       // cellValue =big.toEngineeringString();
       cellValue = big.toString();
      }
      break;
     case XSSFCell.CELL_TYPE_FORMULA:
      BigDecimal bigula = new BigDecimal(cell
        .getCachedFormulaResultType());
      // cellValue = bigula.toEngineeringString();
      cellValue = bigula.toString();
      break;
     case XSSFCell.CELL_TYPE_BLANK:
      cellValue = "";
      break;
     default:
      break;
     }
     String[] mapValueArr = mapvalue.split(","); // map值已‘,’隔开,第一个为属性,第二个为类型
     String mapValueType = "string"; // 默认为字符串类型
     if (mapValueArr.length == 2) {
      mapValueType = mapValueArr[1];
     }
     String firstChar = mapValueArr[0].substring(0, 1);
     String endString = mapValueArr[0].substring(1);
     String methodName = "set" + firstChar.toUpperCase()
       + endString;
     Method method = null;
     if (mapValueType.equals("string")) {
      method = classType.getMethod(methodName, String.class);
      method.invoke(obj, cellValue);
     } else if (mapValueType.equals("double")) {
      method = classType.getMethod(methodName, Double.TYPE);
      method.invoke(obj, Double.parseDouble((String)cellValue));
     } else if (mapValueType.equals("long")) {
      method = classType.getMethod(methodName, Long.TYPE);
      method.invoke(obj, Long.parseLong((String)cellValue));
     } else if (mapValueType.equals("date")) {
      method = classType.getMethod(methodName, Date.class);
      method.invoke(obj, cellValue);
     }
     else if (mapValueType.equals("timestamp")) {
      method = classType.getMethod(methodName, Timestamp.class);
      method.invoke(obj, cellValue);
     }
    }
    rtn.add(obj);
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return rtn;
 }

原文地址:https://www.cnblogs.com/itzone/p/3127768.html