`

org.apache.poi导出excel

 
阅读更多
OutputStream osResult = response.getOutputStream();
  
  response.reset();
  
  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
  String date=sdf.format(new Date());
  String filedisplay = date+".xls";//下载文件时显示的文件保存名称  
  
  filedisplay = java.net.URLEncoder.encode(filedisplay,"UTF-8");
  
  response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);  
  
  response.setContentType("application/msexcel");//设置为下载application/x-download  
  
  HSSFWorkbook wb = new HSSFWorkbook();   
  HSSFSheet sheet = wb.createSheet(date);
  
  HSSFCellStyle setBorder = wb.createCellStyle();
  setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    
  // 背景色的设定
  HSSFCellStyle sheetStyle = wb.createCellStyle();
  sheetStyle.setFillBackgroundColor(HSSFColor.LIGHT_ORANGE.index);
  // 前景色的设定
  sheetStyle.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index);
  // 填充模式
  sheetStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  
  HSSFRow row = sheet.createRow((short)0);
  //表格标题
  String[] excelTitle=getExcelTitle();
  for(int i=0;i<excelTitle.length;i++){
   HSSFCell cell = row.createCell((short)i);
   cell.setCellValue(excelTitle[i]);
   cell.setCellStyle(sheetStyle);
  }
  
  //数据行
  for(int i=0;i<list.size();i++){
   //创建行
   row = sheet.createRow((int)i+1);
   //填写列数据
   Object[] obj=(Object[])list.get(i);
   for(int j=0;j<obj.length;j++){
    HSSFCell cell = row.createCell((short) (j));
    cell.setCellValue(obj[j]==null?null:obj[j].toString());
   }
  }
  
  wb.write(osResult);
  osResult.flush();
  osResult.close();

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics