`
zengshaotao
  • 浏览: 756762 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java excel下载

    博客分类:
  • java
 
阅读更多

相对于上传,下载的功能会简单许多,也不用考虑太多客户端可能出现的异常。以下是功能代码概要,不能照搬使用:

 

public void download() throws Exception{
  
  
  //将查询到的数据写入到excel文件,提示下载
  OutputStream os = null;
  WritableWorkbook book =null;
  
  //防止出现乱码,这里需要进行特殊处理
  String customerName = URLDecoder.decode(this.potentialCustomerInfoVO.getCustomerName(), Constant.CHARCODE_UTF8);
  String certCode = URLDecoder.decode(this.potentialCustomerInfoVO.getCertiCode(), Constant.CHARCODE_UTF8);
  
  this.potentialCustomerInfoVO.setCustomerName(customerName);
  this.potentialCustomerInfoVO.setCertiCode(certCode);
  
  HttpServletResponse response = ServletActionContext.getResponse();
  
  //客户端看到的文件名称
  String displayName = "fileName.xls";
  response.setContentType("application/vnd.ms-excel");
  response.addHeader("Content-disposition","attachment;filename="+displayName);
  
  try {
   
   os = response.getOutputStream();
   book = Workbook.createWorkbook(os);
   
   //sheet内容的title
   String tmptitle = "客户车辆信息";
   //创建sheet名称
   WritableSheet wsheet = book.createSheet(tmptitle, 0);
   
   //第一列第一行 至 第七列第一行合并
   wsheet.mergeCells(0, 0, 7, 0);
   
   //设置第一行的宽度
//   wsheet.setRowView(0,40);设置后会隐藏
   
   //设置一级标题格式
   WritableFont font = new WritableFont(WritableFont.ARIAL,16,WritableFont.BOLD,
     false,UnderlineStyle.NO_UNDERLINE);
   
   WritableCellFormat wcfFC = new WritableCellFormat(font);
   
   //将水平对齐方式指定为居中
   wcfFC.setAlignment(jxl.format.Alignment.CENTRE);
   //将数值对齐方式指定为居中
   wcfFC.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
   
   //为excel添加带有格式的title
   wsheet.addCell(new Label(0,0,tmptitle,wcfFC));
   
   //设置二级标题的格式
   font = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,
     false,UnderlineStyle.NO_UNDERLINE);
   
   wcfFC = new WritableCellFormat(font);
   
   wsheet.addCell(new Label(0,1,"车主姓名",wcfFC));
   wsheet.addCell(new Label(1,1,"证件类型",wcfFC));
   wsheet.addCell(new Label(2,1,"证件号码",wcfFC));
   wsheet.addCell(new Label(3,1,"车牌号",wcfFC));
   wsheet.addCell(new Label(4,1,"车架号",wcfFC));
   wsheet.addCell(new Label(5,1,"发动机号",wcfFC));
   wsheet.addCell(new Label(6,1,"行驶证",wcfFC));
   wsheet.addCell(new Label(7,1,"车型代码",wcfFC));
   
   //调用service层,获得符合条件的数据
   List<PotentialCustomerInfoVO> exportList = potentialCustomerService.queryPotentialCustomers(potentialCustomerInfoVO);
   
   String value = "";
   String tempValue = "";
   //设置excel主题内容
   for(int i = 0 ;i < exportList.size() ;i++){
    
    PotentialCustomerInfoVO pcv = exportList.get(i);
    
    wsheet.addCell(new Label(0,i+2,pcv.getCustomerName()));
    
    value = pcv.getCertiType();
    
    if(Constant.CERTITYPE_VALUE_SFZ.equals(value)){
     tempValue = Constant.CERTITYPE_TEXT_SFZ;
    }else if(Constant.CERTITYPE_VALUE_JGZ.equals(value)){
     tempValue = Constant.CERTITYPE_TEXT_JGZ;
    }else if(Constant.CERTITYPE_VALUE_HZ.equals(value)){
     tempValue = Constant.CERTITYPE_TEXT_HZ;
    }else if(Constant.CERTITYPE_VALUE_QT.equals(value)){
     tempValue = Constant.CERTITYPE_TEXT_QT;
    }
    wsheet.addCell(new Label(1,i+2,tempValue));
    wsheet.addCell(new Label(2,i+2,pcv.getCertiCode()));
    wsheet.addCell(new Label(3,i+2,pcv.getLicenceNumber()));
    wsheet.addCell(new Label(4,i+2,pcv.getVin()));
    wsheet.addCell(new Label(5,i+2,pcv.getEngineNumber()));
    wsheet.addCell(new Label(6,i+2,pcv.getDriverNumber()));
    wsheet.addCell(new Label(7,i+2,pcv.getF_1()));
   }
   book.write();
   os.flush();
  } catch (IOException e) {
   e.printStackTrace();
   throw e;
  } finally{
   //这里的输出流需要关闭,否则输出的excel文件内容为空
   try{
    if(book != null){
     book.close();
    }
    if(os!=null){
     os.close();
    }
   }catch(Exception e){
    e.printStackTrace();
   }
  }
 }

 

下载时要注意,如果对于浏览器没有选项卡的情况,且页面上是通过href的链接形式,要注意不能是href="javascript:void(0)",否则没有提示下载框.如果是本页面提示下载,使用href="#"

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics