使用poi导出xls

代码:

    @RequestMapping("exportXls")
    public void exportXls(
        @ModelAttribute("operationReportVo") OperationReportVo operationReportVo,
        HttpServletRequest req, HttpServletResponse resp, HttpSession session) throws FileNotFoundException, IOException, ParseException {
        String roles = getRoles(operationReportVo.getRoles());
        Integer locationId = this.getCurrentUser(req).getLocationId();
        if (operationReportVo.getLocationIds() != null &&
            operationReportVo.getLocationIds().length > 0) {
            locationId = operationReportVo.getLocationIds()[0];
        }
        Date endTime = sdf.parse(sdfDay.format(operationReportVo.getEndTime()) + " 23:59:59");
        List<UserBehaviorVo> userBehaviorVos = userBehaviorService
            .getUserBehaviorVo(null, operationReportVo.getStartTime(),
                endTime, locationId, roles);
        
        String path = session.getServletContext().getRealPath("/") + "/WEB-INF/ppt_template/usetimes.xlsx";
        XSSFWorkbook workBook = new XSSFWorkbook(new FileInputStream(path));
        XSSFSheet sheet = workBook.getSheetAt(0);
        XSSFCellStyle style = sheet.getWorkbook().createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
       
        for (int it = 0; it < userBehaviorVos.size(); it++) {     
            XSSFRow row = sheet.createRow(it + 1);
            // 每一行创建12列
            for (int cellIndex = 0; cellIndex < 16; cellIndex++) {
                XSSFCell cell = row.createCell(cellIndex);
                cell.setCellStyle(style);
            }       
            int rowNum = it + 1;
            sheet.getRow(rowNum).getCell(0).setCellValue(userBehaviorVos.get(it).getMobileNo());
            sheet.getRow(rowNum).getCell(1).setCellValue(userBehaviorVos.get(it).getRealName());
            sheet.getRow(rowNum).getCell(2).setCellValue(userBehaviorVos.get(it).getSex());
            sheet.getRow(rowNum).getCell(3).setCellValue(userBehaviorVos.get(it).getRole());
            sheet.getRow(rowNum).getCell(4).setCellValue(userBehaviorVos.get(it).getArea());
            Integer loginTimes = userBehaviorVos.get(it).getLoginTimes();
            String isLogin = loginTimes > 0 ? "是" : "否";
            sheet.getRow(rowNum).getCell(5).setCellValue(isLogin);
            sheet.getRow(rowNum).getCell(6).setCellValue(loginTimes);
            sheet.getRow(rowNum).getCell(7).setCellValue(userBehaviorVos.get(it).getWorkLogTimes());
            sheet.getRow(rowNum).getCell(8).setCellValue(userBehaviorVos.get(it).getMessageTimes());
            sheet.getRow(rowNum).getCell(9).setCellValue(userBehaviorVos.get(it).getNoticeTimes());
            sheet.getRow(rowNum).getCell(10).setCellValue(userBehaviorVos.get(it).getAssignmentTimes());
            
            sheet.getRow(rowNum).getCell(11).setCellValue(userBehaviorVos.get(it).getAcceptAssignmentTimes());
            sheet.getRow(rowNum).getCell(12).setCellValue(userBehaviorVos.get(it).getCommitAssignmentTimes());
            sheet.getRow(rowNum).getCell(13).setCellValue(userBehaviorVos.get(it).getServerFarmers());
            sheet.getRow(rowNum).getCell(14).setCellValue(userBehaviorVos.get(it).getScores());
            
            sheet.getRow(rowNum).getCell(15).setCellValue(userBehaviorVos.get(it).getTotalTimes());   
        }
        // 2设置文件名
        String fileName = URLEncoder.encode("用户使用情况统计", "UTF-8");
        resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ");
        resp.setHeader("Content-Disposition", "attachment;" + " filename=" + fileName + ".xlsx");
        workBook.write(resp.getOutputStream());
    }

其中PPT模板在/WEB-INF/ppt_template/usetimes.xlsx




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值