字号:    

关于poi操作excel的一些东西

   <!>使用建立excel的全过程 

   //建立新HSSFWorkbook对象

    HSSFWorkbook wb = new HSSFWorkbook();

    //建立新的sheet对象
     HSSFSheet sheet = wb.createSheet("new sheet");
     
     //设置列宽度

     sheet.setColumnWidth((short) 4, (short) 10000);
     HSSFCellStyle cellStyle = wb.createCellStyle();

    
     // Create a row and put some cells in it. Rows are 0 based.
     //建立新行

     HSSFRow row = sheet.createRow((short)0);

     //建立列
     row.createCell((short)0).setCellStyle(cellStyle);
     row.createCell((short)0).setCellValue(1);

     //保存

      FileOutputStream fileOut = new FileOutputStream(path);
     wb.write(fileOut);

     fileOut.flush();
    fileOut.close();

<2>相关使用方法

//内部类Excel Entity inside Class
 public class ExcelEntity{
  
  //规则值 rules Value
  String rules;
  //规则所在行 the rowNum
  int rowNum;
  //规则所在列 the colNum
  int colNum;
  
  public String getRules() {
   return rules;
  }
  public void setRules(String rules) {
   this.rules = rules;
  }
  public int getRowNum() {
   return rowNum;
  }
  public void setRowNum(int rowNum) {
   this.rowNum = rowNum;
  }
  public int getColNum() {
   return colNum;
  }
  public void setColNum(int colNum) {
   this.colNum = colNum;
  }
  
 }

/**
   * 读取模板 read the template
   * @param templatePath   the template's Path
   * @return HSSFWorkbook return HSSFWorkbook's variables
   */
  public HSSFWorkbook readTemplate(String templatePath) {
   HSSFWorkbook result=null;
    try{
     POIFSFileSystem fs =new POIFSFileSystem(new FileInputStream(templatePath));
     result= new HSSFWorkbook(fs);
    }catch(Exception ex){
     ex.printStackTrace();
    }finally{
     return result;
    }
   }

  /**
   * 取得规则实体 get Excel Entity in the List
   * @param wb
   * @return list<ExcelEntity>
   */
  private List getRules(HSSFWorkbook wb){
   List resultList=new ArrayList();
   try{
     HSSFSheet sheet = wb.getSheetAt(0);
     int  rows  = sheet.getLastRowNum();
     for(int i=0;i<rows;i++){
       HSSFRow row   = sheet.getRow(i);
       int lastcell=row.getLastCellNum();
        for (int c = 0; c < lastcell; c++)
         {
          HSSFCell cell  = row.getCell((short) c);
          if(cell.getStringCellValue().indexOf("$")>-1){
           ExcelEntity excel=new ExcelEntity();
           excel.setRules(cell.getStringCellValue());
           excel.setColNum(c);
           excel.setRowNum(i);
         resultList.add(excel);
          }
         }
      }
    }catch(Exception ex){
     resultList=null;
    }finally{
     return resultList;
    }
   }
  
  /**
   * 替代数据 replace the DataBase
   * @param wb
   * @return
   */
  public HSSFWorkbook replace(HSSFWorkbook wb){
   HSSFWorkbook resultWorkbook;
   List rulesList=getRules(wb);
   for(int i=0;i<rulesList.size();i++){
    ExcelEntity excel=new ExcelEntity();
   try{
    excel=(ExcelEntity)rulesList.get(i);
    String rulesValue=excel.getRules();
    String rowNum=rulesValue.split("_")[1];
    String colNum=rulesValue.split("_")[2];
   
    /***
     * 测试替换数据
     */
     wb.getSheetAt(0).getRow(excel.getRowNum()).getCell((short)excel.getColNum()).setCellValue("从数据库取出rowid="+rowNum+",从数据库取出colid="+colNum);
    }catch(Exception ex){
     String rules=excel.getRules().split("_")[1];
     System.out.println(rules);
    }
   }
   resultWorkbook=wb;
   return resultWorkbook;
  }
    
  /**
   * 保存excel   sava the excel file on the path
   * @param wb      HSSFWorkbook
   * @param path  the save path
   * @return boolean  true is save is success/false is save is failure
   */
  public boolean saveExcel(HSSFWorkbook wb,String path){
     boolean isSuccess=false;
   try {
    FileOutputStream fileOut = new FileOutputStream(path);
     wb.write(fileOut);
     isSuccess=true;
     System.out.println("添加完成");
   } catch (Exception e) {
     isSuccess=false;
     System.out.println("添加失败");
    e.printStackTrace();
   }finally{
    return isSuccess;
   }
   }
  
  //把EXCEL输出到JSP上面
  public  void writeXLS(HttpServletResponse res,HSSFWorkbook excel) throws Exception {
       res.reset();
       res.setContentType("application/vnd.ms-excel");
       OutputStream finleOut = (OutputStream)res.getOutputStream();
       excel.write(finleOut);
       res.flushBuffer();
 
  }
  

分类:Jsp
?次阅读
 2008-05-06 17:17