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)