poi导出excel

HSSFWorkbook wb = new HSSFWorkbook();
  HSSFSheet sheet = wb.createSheet("出单量统计");
  HSSFRow row = sheet.createRow((int) 0);
  HSSFCellStyle style = wb.createCellStyle();
  style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
  HSSFCell cell = row.createCell((short) 0);
  cell.setCellValue("人员代码");
  cell.setCellStyle(style);
  cell = row.createCell((short) 1);
  cell.setCellValue("姓名");
  cell.setCellStyle(style);
  cell = row.createCell((short) 2);
  cell.setCellValue("归属部门");
  cell.setCellStyle(style);
  cell = row.createCell((short) 3);
  cell.setCellValue("保单车险");
  cell.setCellStyle(style);
  List<ExportTotal> list = registerservice.exportTotal();
  for (int i = 0; i < list.size(); i++) {
   row = sheet.createRow((int) i + 1);
   sheet.autoSizeColumn((short) i, true);
   ExportTotal stu = (ExportTotal) list.get(i);
   row.createCell((short) 0).setCellValue(stu.getPersonCode());
   row.setRowStyle(style);
   row.createCell((short) 1).setCellValue(stu.getBooker());
   row.setRowStyle(style);
   row.createCell((short) 2).setCellValue(stu.getDept());
   row.setRowStyle(style);
   row.createCell((short) 3).setCellValue(stu.getPolicyAaa());
   }

  //以页面另存为的方式
   HttpServletResponse response = ServletActionContext.getResponse();
   String strFileName = "出单量统计系统.xls";
   strFileName=new String(strFileName.getBytes("gb2312"), "iso8859-1");
   response.setHeader("Content-Disposition", "attachment;filename="+strFileName+ "");
   response.setHeader("Connection", "close");
   response.setHeader("Content-Type", "application/vnd.ms-excel");

  // 将文件存到指定位置
  // try {
  // FileOutputStream fout = new FileOutputStream("G:/students.xls");
  // wb.write(fout);
  // fout.close();
  // } catch (Exception e) {
  // e.printStackTrace();
  // }
  

你可能感兴趣的:(poi导出excel)