是否有人对Java操作Excel比较熟悉的求赐教?

解决方案 »

  1.   

    jxl、poi都可以操作,google、百度..............
      

  2.   

    jxl好像只支持excel2003,而且不维护了。
    poi是apache旗下的开源项目,excel2003和2007都支持,而且还在维护
      

  3.   

    jxl.jar吧,使用起来方便简单,POI,有点复杂。比较麻烦。
      

  4.   

    jxl插件比较好用,网上有好多实例,可以找一下http://download.csdn.net/detail/VCXIAOHEI/2164189
      

  5.   

    下面是我们项目中用到的,我自己写的读取excel,是用poi,excel版本不同,读取会有一点区别,我标注了,lz可以参考下public List<Map<String, Object>> loadChitData(File excelFile,
    boolean isDelete) throws BusinessException {
    if (!excelFile.exists()) {
    throw new BusinessException("要读取的文件不存在");
    } List<Map<String, Object>> chits = new ArrayList<Map<String, Object>>();
    InputStream stream = null;
    boolean errFlag = false;
    try {
    stream = new FileInputStream(excelFile);
    Map<String, Integer> orgs = getOrgs(); // 所有部门
    Workbook wb = null;
    if(excelFile.getName().endsWith("xls")){
    wb = new HSSFWorkbook(stream);   //读取2003
    }else{
    wb = new XSSFWorkbook(stream);    //读取2007
    }
    if (wb.getNumberOfSheets() == 0) {
    throw new BusinessException("要导入的Excel文件中不存在数据");
    }
    Sheet sheet = wb.getSheetAt(0);

    Set<String> numSet = new HashSet<String>();
    int rowIndex = 1;
        boolean isNext = true;
      while(isNext){
    int lineNo = rowIndex+1;
    Row row = sheet.getRow(rowIndex);

    Map<String, Object> chit = new HashMap<String, Object>();
    if (row.getCell(0) == null || row.getCell(0).getStringCellValue().equals("")) {
    throw new BusinessException("导入的代金券第" + lineNo + "行名称为空");
    } else {
    String name = row.getCell(0).getStringCellValue();
    chit.put("name", name);
    }
    if (row.getCell(1) == null || row.getCell(1).getStringCellValue().equals("")) {
    throw new BusinessException("导入的代金券第" + lineNo + "行条码为空");
    }else{
    String num = row.getCell(1).getStringCellValue();
    numSet.add(num);
    chit.put("num", num); // 代金券面值
    }

    if (row.getCell(2) == null || row.getCell(2).getNumericCellValue() == 0.0) {
    throw new BusinessException("导入的代金券第" + lineNo + "行面值为空");
    } else {
    String faceValue = row.getCell(2).getNumericCellValue()+"";
    BigDecimal DecFaceValue = new BigDecimal(faceValue);
    chit.put("faceValue", DecFaceValue);
    } // 所属部门编号
    String orgName = row.getCell(3)!=null?row.getCell(3).getStringCellValue():"";

    if (orgs.containsKey(orgName)) {
    int orgId = orgs.get(orgName);
    chit.put("orgId", orgId);
    } else {
    chit.put("orgId", null);
    } chit.put("orgName",orgName); String indate = dateFormat(row.getCell(4).getDateCellValue());
    if (indate == null) {
    throw new BusinessException("导入的代金券第" + lineNo + "行有效期为空");
    } else {

    chit.put("indate", indate);
    }
    String onsetDate = dateFormat(row.getCell(5).getDateCellValue());
    if (onsetDate == null) {
    throw new BusinessException("导入的代金券第" + lineNo + "行生效时间为空");
    } else {

    chit.put("onsetDate", onsetDate);
    }
    chit.put("re", row.getCell(6)!=null?row.getCell(6).getStringCellValue():"");
    chits.add(chit);
    rowIndex = rowIndex + 1;
    row = sheet.getRow(rowIndex);
    if(row == null){
    isNext = false;
    rowIndex = rowIndex - 1;
    }
    } if (rowIndex > numSet.size()) {
    throw new BusinessException("导入代金券条码不能重复");
    }
    } catch (BusinessException e) {
    errFlag = true;
    throw e;
    } catch (Exception e) {
    errFlag = true;
    throw new BusinessException(e.getMessage());
    } finally {
    if (isDelete || errFlag) {
    // 关闭文件流
    try {
    if (stream != null) {
    stream.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    } excelFile.delete();
    }
    } return chits;
    }
      

  6.   

    jxl吧,就用过jxl,网上例子很多,找个简单的例子很容易学会用的
      

  7.   

    我个人表示  还是学习poi 比较好点!!!!!
      

  8.   

    jxl比较好用一点,不过就是已经不再维护了,没有后继版本了,对于excel来说,最高只能支持到2003
    poi是apache的开源项目,以OO为原则设计,并且持续维护,也支持最新版本的excel格式相对来说,还是比较推荐poi,jxl已经过时