package net.guoguoda.admin.act.controller; import java.io.OutputStream; import java.util.Date; import java.util.List; import net.guoguoda.act.model.ActBase; import net.guoguoda.act.model.ActGGDRRegister; import net.guoguoda.admin.common.controller.BaseController; import org.apache.commons.lang.StringUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import com.jfinal.ext.route.ControllerBind; import com.wecode.framework.ext.jfinal.controller.DateRange; @ControllerBind(controllerKey="这里是前段访问控制器的路径") public class ExcelController extends BaseController{ public void index(){ //这里是获取数据Excel数据 createExcel(dataList,ActBase.dao.loadById(act_id).getStr("name"));//dataList就是获取的数据 renderNull(); // renderJson(JsonResult.success().toJson()); } private void createExcel(List<ActGGDRRegister> record,String name){ // 第一步,创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet("优惠券串码信息"); sheet.setColumnWidth(0,5000); sheet.setColumnWidth(1,5000); sheet.setColumnWidth(2,5000); sheet.setColumnWidth(3,5000); sheet.setColumnWidth(4,5000); sheet.setColumnWidth(5,5000); sheet.setColumnWidth(6,5000); sheet.setColumnWidth(7,5000); sheet.setColumnWidth(8,5000); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short HSSFRow row = sheet.createRow((int) 0); // 第四步,创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 HSSFCell cell = row.createCell(0); cell.setCellValue("姓名"); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue("微信昵称"); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue("手机号"); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue("地址"); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue("参与时间"); cell.setCellStyle(style); cell = row.createCell(5); cell.setCellValue("实际投票量"); cell.setCellStyle(style); cell = row.createCell(6); cell.setCellValue("虚拟投票量"); cell.setCellStyle(style); cell = row.createCell(7); cell.setCellValue("状态"); cell.setCellStyle(style); cell = row.createCell(8); cell.setCellValue("图片提交"); cell.setCellStyle(style); //第五步,写入实体数据 实际应用中这些数据从数据库得到, for (int i = 0; i < record.size(); i++) { ActGGDRRegister register = record.get(i); row = sheet.createRow((int) i + 1); // 第四步,创建单元格,并设置值 row.createCell(0).setCellValue(register.getStr("name")); row.createCell(1).setCellValue(register.getStr("card_no")); row.createCell(2).setCellValue(register.getStr("phone")); row.createCell(3).setCellValue(register.getStr("address")); row.createCell(4).setCellValue(register.getDate("create_time").toString()); row.createCell(5).setCellValue(register.getInt("votes")); row.createCell(6).setCellValue(register.getInt("virtual_votes")); row.createCell(7).setCellValue(register.getStatusDesc()); row.createCell(8).setCellValue(register.getPicNumber()+"("+register.getPassedPicNumber()+")"); } try { getResponse().reset(); // 非常重要 getResponse().setContentType("application/vnd.ms-excel"); String fileName = name+"-用户报名信息表"+".xls"; getResponse().setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes(),"iso-8859-1")); //创建输出流对象 OutputStream outStream = getResponse().getOutputStream(); wb.write(outStream); outStream.close(); }catch (Exception e){ e.printStackTrace(); } } }