分享

jxl _ excel 使用示例

 怀旧妞妞 2011-06-17
//模板路径
   filePath = this.getClass().getClassLoader().getResource("").toString().replace("/WEB-INF/classes/", "/report/cfd-receivesenddoc.xls").replace("file:/", "").replaceAll("/", "\\\\\\\\");
   //filePath = "d:/temp/kmgReport.xls";
   File   tempFile=new File(filePath);
   // 创建只读的Excel工作薄的对象  
   Workbook wb = Workbook.getWorkbook(tempFile);
   wworkbook = Workbook.createWorkbook(out,wb);
   wworkbook.getSheet(0);
   WritableSheet sheet = wworkbook.getSheet(0);
   SheetSettings ss = sheet.getSettings();
//设置表格y 锁定列
   ss.setHorizontalFreeze(4);
//设置表格x 锁定行
   ss.setVerticalFreeze(1);
 
   //设置表格内容样式
   WritableFont fontNormal = new WritableFont(WritableFont.createFont("宋体"),10,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK );
   WritableCellFormat cellFormat1 = new WritableCellFormat(fontNormal);
   cellFormat1.setAlignment(Alignment.CENTRE);
   cellFormat1.setVerticalAlignment(VerticalAlignment.TOP);
   cellFormat1.setWrap(true);
   //设置excel文件表格边框
   cellFormat1.setBorder(Border.ALL, BorderLineStyle.THIN);
     int i = 0 ;
    for(Map map : list){
        int col = 0 ;
    int row = 2 ;
     String vprojectName  = (String)map.get("projectName");
        //插入报表
     //项目名称
     writeCell(sheet,cellFormat1,vprojectName,col,i+row);
     //为了让表格画出边框添加默认空值
     //档号,原卷号等
     writeCell(sheet,cellFormat1,"",col+1,i+row);
      //案卷名称
//     如果是发文
     if(receiveSendType == 1){
      writeCell(sheet,cellFormat1,"收 "+vfromOrgName,col+3,i+row);
     }
     else if(receiveSendType == 2){
      writeCell(sheet,cellFormat1,"发 "+vtoOrgName,col+3,i+row);
     }    
     i++;
    }
wworkbook.write();
 
finally{
   try
   {
    if(wworkbook != null)
     wworkbook.close();
   }catch(Exception e) { e.printStackTrace(); }
  }
//-------------------------------整理数据格式---------------------------------------------------
private void writeCell(WritableSheet sheet,WritableCellFormat cellFormat,Object param,int iCol,int iRow){
  try {
   if (param!=null){
    if (param instanceof BigDecimal){
     BigDecimal bd = new BigDecimal(param.toString());
     Number number = new Number(iCol,iRow,bd.doubleValue());
     number.setCellFormat(cellFormat);
     sheet.addCell(number);
    }else if (param instanceof Integer){
     BigDecimal bd = new BigDecimal(param.toString());
     Number number = new Number(iCol,iRow,bd.intValue());
     number.setCellFormat(cellFormat);
     sheet.addCell(number);
    }else if (param instanceof Long){
     BigDecimal bd = new BigDecimal(param.toString());
     Number number = new Number(iCol,iRow,bd.longValue());
     number.setCellFormat(cellFormat);
     sheet.addCell(number);
    }else if (param instanceof Date){
     String strDate = ((Date) param).toString();
     String str = strDate.substring(2, strDate.length());
     str.replace("-", ".");
     Label label=new Label(iCol,iRow,str.replace("-", "."));
     label.setCellFormat(cellFormat);
     sheet.addCell(label);
    }else if (param instanceof String){
     Label label=new Label(iCol,iRow,((String)param).trim().toString());
     label.setCellFormat(cellFormat);
     sheet.addCell(label);
    }else{
     Label label=new Label(iCol,iRow,"");
     label.setCellFormat(cellFormat);
     sheet.addCell(label);
    }
   }else{
    Label label=new Label(iCol,iRow,"");
    label.setCellFormat(cellFormat);
    sheet.addCell(label);
   }
  } catch (Exception e) {
   System.out.println("------------格式这里有问题");
  }
 }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约