码迷,mamicode.com
首页 > 编程语言 > 详细

java 下载excel

时间:2014-08-24 10:22:22      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:java下载excel

@RequestMapping(value="ddd.do")
public void ddd(HttpServletResponse response){
 try{
  List<AsWp> list = asWpService.findAll();
  HSSFWorkbook wb = export(list);
  response.setContentType("application/vnd.ms-excel");
  response.setHeader("Content-disposition", "attachment;filename=student.xls");
  OutputStream ouputStream = response.getOutputStream();
  wb.write(ouputStream);
  ouputStream.flush();
  ouputStream.close();
 }catch (Exception e) {
  e.printStackTrace();
 }
}

private HSSFWorkbook export(List<AsWp> list){
 String[] title= {"资源编码","名称"};
 HSSFWorkbook wb = new HSSFWorkbook();
 HSSFSheet sheet = wb.createSheet("资产信息");
 HSSFRow row = sheet.createRow(0);
 HSSFCellStyle style = wb.createCellStyle();
 style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
 for(int i=0;i<title.length;i++){
  HSSFCell cell = row.createCell(i);
  cell.setCellValue(title[i]);
  cell.setCellStyle(style);
  sheet.autoSizeColumn(i);
 }
 
 for(int i=0;i<list.size();i++){
  row = sheet.createRow(i+1);
  AsWp aw = list.get(i);
  row.createCell(0).setCellValue(aw.getWpbm());
  row.createCell(1).setCellValue(aw.getWpmc());
 }
 return wb;
}

java 下载excel

标签:java下载excel

原文地址:http://zhangtaoha.blog.51cto.com/3403478/1543970

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!