Excel导出
来源:互联网 发布:jdbc mysql原理 编辑:程序博客网 时间:2024/05/16 01:37
1.service层
/** * 导出Excel * @param fileName * @param titles * @param contents * @param resp * @throws Exception */ public void download_xls(String fileName, List<String> titles , List<PtRecord> contents, HttpServletResponse resp, HttpServletResponse response) throws Exception { try { /*********************浏览器弹出下载框***************************/ OutputStream out = resp.getOutputStream(); HSSFWorkbook workbook = new HSSFWorkbook();//创建exc文件 Date date=new Date(); DateFormat format=new SimpleDateFormat("yyyy-MM-dd"); String time=format.format(date); response.setContentType("application/x-download");//下面三行是关键代码,处理乱码问题 response.setCharacterEncoding("utf-8"); response.setHeader("Content-Disposition", "attachment;fileName="+new String(fileName.getBytes("gbk"), "iso8859-1")+time+".xls"); Sheet sheet = workbook.createSheet("信息"); Row row = sheet.createRow(0); Cell cell ; for(int i=0; i<titles.size(); i++){ //设置标题 cell = row.createCell(i); cell.setCellValue(titles.get(i)); } for(int i=0;i<contents.size();i++) { PtRecord ptRecord = contents.get(i); row = sheet.createRow(i + 1); // 合同备案编号 cell = row.createCell((short) 0); if(ptRecord.getRecordCode() == null){ cell.setCellValue("--"); }else { cell.setCellValue(ptRecord.getRecordCode()); } // 成交方式 cell = row.createCell((short) 1); if(ptRecord.getHousePublishType() == null){ cell.setCellValue("--"); }else { cell.setCellValue(ptRecord.getHousePublishType().getName()); } // 申请人 cell = row.createCell((short) 2); if(ptRecord.getRecordApplication() == null){ cell.setCellValue("--"); }else { cell.setCellValue(ptRecord.getRecordCode()); } // 申请时间 cell = row.createCell((short) 3); if(ptRecord.getApplicationAt() == null){ cell.setCellValue("--"); }else { cell.setCellValue(ptRecord.getApplicationAt()); } // 房源类型 cell = row.createCell((short) 4); if(ptRecord.getHouseType() == null){ cell.setCellValue("--"); }else { cell.setCellValue(ptRecord.getHouseType().getName()); } // 购房合同号 cell = row.createCell((short) 5); if(ptRecord.getContractNumber() == null){ cell.setCellValue("--"); }else { cell.setCellValue(ptRecord.getContractNumber()); } // 产权证(不动产证件号) cell = row.createCell((short) 6); if(ptRecord.getPropertyNumber() == null){ cell.setCellValue("--"); }else { cell.setCellValue(ptRecord.getPropertyNumber()); } // 房源编号 cell = row.createCell((short) 7); if(ptRecord.getHouseCode() == null){ cell.setCellValue("--"); }else { cell.setCellValue(ptRecord.getHouseCode()); } } workbook.write(out); out.close(); } catch (Exception e){ } }2.action层
@At("/excel")@Ok("void")public void excel(HttpServletResponse resp, HttpServletRequest request, HttpServletResponse response) throws Exception {List<String> titles = new ArrayList<String>();List<PtRecord> listContexts = bphcPtRecordService.fetchAll();String fileName="合同备案管理";titles.add("合同备案编号");titles.add("成交方式");titles.add("申请人");titles.add("申请时间");titles.add("房源类型");titles.add("购房合同号");titles.add("产权证(不动产证件号)");titles.add("房源编号");bphcPtRecordService.download_xls(fileName,titles,listContexts,resp,response);}
0 0
- 导出EXCEL
- Excel导出
- 导出Excel
- 导出EXCEL
- 导出Excel
- EXCEL导出
- 导出Excel
- 导出excel
- 导出excel
- EXCEL导出
- 导出excel
- 导出Excel
- Excel导出。
- 导出excel
- Excel 导出
- 导出excel
- 导出excel
- 导出excel
- redis 基础命令
- AFNetworking 状态栏小菊花
- 烟草及IT服务流程平台SQL等
- mudoo库Python翻译
- 重构
- Excel导出
- 过滤son中不需要的字段
- 【设计模式】六大原则之三(迪米特法则与合成/聚合复用原则)
- 基于HTML,以POST方式,检查注册用户名是否在数据库中已存在
- 使用xib创建控制器
- SRM 691 Undiv2
- PAT BASIC LEVEL 1033. 旧键盘打字(20)
- 芯片参考手册和数据手册的区别
- unity3d引擎程序员养成