关闭

使用poi导出xls

196人阅读 评论(0) 收藏 举报
分类:

代码:

    @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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7886次
    • 积分:196
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:0篇
    • 译文:0篇
    • 评论:3条
    文章分类
    文章存档
    最新评论