如题,求用过的人回答,最好能给点小例子用法之类的

解决方案 »

  1.   


    // 创建EXCEL文件
    private void createExcel(List excelList) {
    FileOutputStream fos = null;
    try {
    String filePath = ServletActionContext.getRequest()
    .getRealPath("/")
    + "/" + fileName;
    File file = new File(filePath);
    if (!file.exists())
    file.getParentFile().mkdirs();
    fos = new FileOutputStream(filePath);
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    wb.setSheetName(0, "游戏人群统计");
    HSSFRow header = sheet.createRow(0);
    header.createCell(0).setCellValue("序号");
    header.createCell(1).setCellValue("分类");
    header.createCell(2).setCellValue("子分类");
    header.createCell(3).setCellValue("游戏名称");
    header.createCell(4).setCellValue("男性玩家数");
    header.createCell(5).setCellValue("女性玩家数");
    header.createCell(6).setCellValue("运行时间");
    if (excelList != null) {
    for (short i = 0; i < excelList.size(); i++) {
    GameCount gCount = (GameCount) excelList.get(i);
    if (gCount != null) {
    HSSFRow row = sheet.createRow(i + 1);
    row.createCell(0).setCellValue(i + 1);
    if (gCount.getCategories() != null)
    row.createCell(1).setCellValue(
    gCount.getCategories());
    else
    row.createCell(1).setCellValue("");
    if (gCount.getSubCategory() != null)
    row.createCell(2).setCellValue(
    gCount.getSubCategory());
    else
    row.createCell(2).setCellValue("");
    if (gCount.getGameName() != null) {
    row.createCell(3)
    .setCellValue(gCount.getGameName());
    } else {
    row.createCell(3).setCellValue("");
    }
    if (gCount.getMenRunCounts() != null) {
    row.createCell(4).setCellValue(
    gCount.getMenRunCounts());
    } else {
    row.createCell(4).setCellValue("");
    }
    if (gCount.getWomenRunCounts() != null) {
    row.createCell(5).setCellValue(
    gCount.getWomenRunCounts());
    } else {
    row.createCell(5).setCellValue("");
    }
    if(null != gCount.getRunTime()){
    row.createCell(6).setCellValue(sdf.format(gCount.getRunTime()));
    }else{
    row.createCell(6).setCellValue("");
    }
    }
    }
    }
    wb.write(fos);
    fos.close();
    } catch (Exception ex) {
    ex.printStackTrace();
    } finally {
    try {
    if (null != fos) {
    fos.close();
    }
    } catch (Exception ex) {
    }
    }
    }这是POI  对excel03版的创建 .xls  还支持07版以后 不过用的就不是这套类了
    你可以百度下 网上很多例子 不过你要注明什么版本
      

  2.   

    支持office新版本,可以到官网下载最新包
    http://poi.apache.org/
      

  3.   


    public static Map<String, Object> readExcel(File excel) throws Exception {
          InputStream in = new FileInputStream(excel);
          Workbook book = null;      try {
             book = new HSSFWorkbook(in);
          }
          catch(OutOfMemoryError e) {
             String msg = "The file " + excel.getName()
                   + " data is error, please delete invalid data!";
             if(log.isDebugEnabled()) {
                log.debug(msg);
             }         throw new Exception(msg);
          }
          catch(OfficeXmlFileException e) {
             in = new FileInputStream(excel);
             book = new XSSFWorkbook(in);
             //throw new Exception(e.getMessage());
          }      if(book == null || book.getNumberOfSheets() == 0) {
             return null;
          }      for(int sheetNum = 0; sheetNum < book.getNumberOfSheets(); sheetNum++) {
             Sheet sheet = book.getSheetAt(sheetNum);         if(sheet == null || sheet.getLastRowNum() == 0) {
                return null;
             }         return readExcelFile(sheet);
          }      return null;
       }public static Map<String, Object> readExcelFile(Sheet sheet) {
          TitleBean titleBean = new TitleBean();
          Map<String, Object> map = new LinkedHashMap<String, Object>();      if(sheet == null || sheet.getLastRowNum() == 0) {
             return map;
          }
          else {
             map.put("sheetName", sheet.getSheetName());
          }      for(int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
             Row row = sheet.getRow(rowIndex);         if(row == null || row.getLastCellNum() == 0) {
                continue;
             }         CommonBean bean = new CommonBean();         for(int cellIndex = 0; cellIndex < row.getLastCellNum(); cellIndex++) {
                Cell cell = row.getCell(cellIndex);
                cell = cell == null ? row.createCell(cellIndex) : cell;
                String val = "";            switch(cell.getCellType()) {
                   case HSSFCell.CELL_TYPE_STRING:
                      val = cell.getStringCellValue().trim();
                      break;
                   case HSSFCell.CELL_TYPE_NUMERIC:
                      if(HSSFDateUtil.isCellDateFormatted(cell)) {
                         SimpleDateFormat format = 
                            new SimpleDateFormat("yyyy-MM-dd");
                         val = format.format(
                            new Date(cell.getDateCellValue().getTime()));
                      }
                      else {
                         Double d = cell.getNumericCellValue();
                         //DecimalFormat df = new DecimalFormat();
                         //df.applyPattern("0");
                         val = String.valueOf(d);
                      }                  break;
                }            if(rowIndex == 0) {
                   titleBean.put(val);
                   //titleBean.put(cellIndex, val);               if(cellIndex == row.getLastCellNum() - 1) {
                      map.put("title", titleBean);
                   }               continue;
                }            if(cellIndex == 0) {
                   bean.setKey(val);
                }            bean.setValue(titleBean.get(cellIndex), val);
             }         if(bean.getKey() != null) {
                map.put(bean.getKey(), bean);
             }
          }      return map;
       }