关于动态导出Excel
来源:互联网 发布:免费通达信看盘软件 编辑:程序博客网 时间:2024/06/18 18:20
导出Excel表用到的组件POI,动态的获取每个列数,然后将每个行数的值累加求和。代码如下
controller层代码
try { XSSFCell cell = null; String sheetName="督查统计数据总和"; String fileName =new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+sheetName + ".xlsx"; // 第一步,创建一个webbook,对应一个Excel文件 XSSFWorkbook xb = new XSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet XSSFSheet sheet = xb.createSheet(sheetName); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short XSSFRow row = sheet.createRow(0); // 第四步,创建单元格,并设置值表头 设置表头居中 XSSFCellStyle style = xb.createCellStyle(); style.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 List<SupdataSend> list=supdataSendService.getSupdatasendid(supdataSend.getId()); List<String> list1= new ArrayList<String>(); list1.add("部门名称"); list1.add("上报次数"); for (SupdataSend supdataSend1:list){ list1.add(supdataSend1.getDataName()); } for (int i = 0; i < list1.size(); i++) { cell = row.createCell(i); cell.setCellValue(list1.get(i)); cell.setCellStyle(style); } List<SupdataSend> sendList=supdataSendService.getListImport(supdataSend.getId()); for (int i=0;i<sendList.size();i++){ row = sheet.createRow(i+1); if (sendList.get(i).getReceiveId()!=null){ row.createCell(0).setCellValue(sendList.get(i).getReceiveId()); }else { row.createCell(0).setCellValue(""); } if (sendList.get(i).getReportNum()!=null){ row.createCell(1).setCellValue(sendList.get(i).getReportNum()); }else { row.createCell(1).setCellValue("0"); } for (int j=0;j<list.size();j++){
//根据行和列统计总数 Double sumvalue= supdataValSonService.getBydataName(list.get(j).getDataName(),sendList.get(i).getRemark()); if (sumvalue!=null){ row.createCell(j+2).setCellValue(sumvalue); }else { row.createCell(j+2).setCellValue("0"); } } }
try { this.setResponseHeader(response, fileName); OutputStream os = response.getOutputStream(); xb.write(os); os.flush(); os.close(); } catch (Exception e) { e.printStackTrace(); }} catch (Exception e) { addMessage(redirectAttributes, "导出督查统计记录失败!失败信息:" + e.getMessage());}
public void setResponseHeader(HttpServletResponse response, String fileName) { try { try { fileName = new String(fileName.getBytes(), "ISO8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } response.setContentType("application/octet-stream;charset=ISO8859-1"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName); response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); } catch (Exception ex) { ex.printStackTrace(); }}service层代码
public List<SupdataSend> getListImport(String id){ //List<SupdataField> fieldList=supdataFieldDao.getSupdataSendid(id); List<SupdataSend> list1= new ArrayList<SupdataSend>(); List<SupdataReceive> list=supdataReceiveDao.getSupdatasendId(id); for (SupdataReceive supdataReceive:list){ SupdataSend supdataSend= new SupdataSend(); supdataSend.setReceiveId(supdataReceive.getOfficeName()); supdataSend.setReportNum(supdataReceive.getReportNum()); //封装单位的ID supdataSend.setRemark(supdataReceive.getRemark()); //supdataSend.setList(list2); list1.add(supdataSend); } return list1;}
阅读全文
0 0
- 关于动态导出Excel
- 关于Excel导入导出
- 关于poi导出Excel
- 关于如何动态导出Excel表格——选择想要导出的字段
- PHP导出Excel动态文件名
- 关于asp.net导出Excel
- 关于导入 导出Excel文件
- 关于asp.net导出Excel
- 关于asp.net导出Excel
- 关于asp.net导出Excel
- 关于asp.net导出Excel
- 关于asp.net导出Excel
- 关于如何记忆导出Excel
- 关于GridView导出到excel!
- 关于导出excel路径问题
- 关于easyPOI导出Excel功能
- 关于POI导入导出Excel
- ASP.NET Excel导出(动态生成)解决方案
- 特殊符号不同浏览器之间的兼容性处理
- class小结
- Python导入模块的几种姿势(常规导入, 相对导入, 可选导入, 本地导入)
- Python Imaging Library: ImageColor Module(图像颜色模块)
- 洪水攻击原理
- 关于动态导出Excel
- 【概率&数学期望】A Dangerous Maze LightOJ
- 46. Permutations
- Qt + OpenGL + 离屏渲染
- 2017/10/26学习心得
- Python编程:从入门到实践的动手试一试答案(第二章)
- 经典题目-如何测试一个一次性纸杯
- MarkDown标记语言入门
- PAT1009 说反话