我用jxl读取excel时发现,excel表格中的日期格式我用sheet1.getCell(10, i).getType()获取到的类型是Lable(表格中对应的单元格确定是日期类型),这个会是什么原因导致的呢?jxlexclelable

解决方案 »

  1.   

    sheet1.getCell(10, i).getType()取到的是这个单元格的类型,而不是单元格内值的类型。就像单元格可以是radio类型的,但是radio所代表的值也可以是日期。像这样 。20130101
           。20130102不知道我说明白没有
      

  2.   

    试试我这些代码,亲测能用
    public static ArrayList generateStationBugSql(File formFile)
    throws Exception {
    InputStream in = null;
    Workbook wb = null;
    ArrayList list = new ArrayList();

    try {
    if (formFile == null) {
    throw new Exception("文件为空!");
    } in = new FileInputStream(formFile);

    wb = Workbook.getWorkbook(in);

    Sheet sheet[] = wb.getSheets();
    if (sheet != null) {
    for (int i = 0; i < sheet.length; i++) {
    if (!sheet[i].getName().equalsIgnoreCase("User")) {
    throw new Exception("指定文件中不包含名称为User的sheet,请重新指定!");
    }
    for (int j = 1; j < sheet[i].getRows(); j++) {
    String[] valStr = new String[8];
    for (int k = 0; k < sheet[i].getColumns(); k++) {
    Cell cell = sheet[i].getCell(k, j);
    String content = "";
    if (cell.getType() == CellType.DATE) {
    DateCell dateCell = (DateCell) cell;
    java.util.Date importdate = dateCell.getDate();/**如果excel是日期格式的话需要减去8小时*/
    long eighthour = 8*60*60*1000;
    SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    /**在当前日期上减8小时*/
    long time = importdate.getTime()-eighthour; 
    java.util.Date date = new java.util.Date();
    date.setTime(time);
    content = simpledate.format(date); 
    } else {
    String tempcontent = (cell.getContents() == null ? ""
    : cell.getContents());
    content = tempcontent.trim().replace('\'', ' ');
    }
    valStr[k] = content;


    list.add(j-1,valStr);
    }
    }
    }

    return list;
    } catch (Exception e) {
    e.printStackTrace();
    throw e;
    } finally {
    if (wb != null) {
    wb.close();
    }
    if (in != null) {
    try {
    in.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    }