导出Excel分页

https://www.cnblogs.com/wangjianguang/p/7112391.html


@SuppressWarnings({ "deprecation", "unchecked" })
    @RequestMapping("export-TrainHistoryRecord")
    @ResponseBody
    protected void buildExcelDocument(EmployeeTrainHistoryQuery query,ModelMap model,
            SXSSFWorkbook workbook, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {
            response.reset();
            // 获得国际化语言
            RequestContext requestContext = new RequestContext(request);
            String CourseCompany = requestContext
                    .getMessage("manage-student-trainRecods");
            response.setContentType("APPLICATION/vnd.ms-excel;charset=UTF-8");
            // 注意,如果去掉下面一行代码中的attachment; 那么也会使IE自动打开文件。
            response.setHeader(
                    "Content-Disposition",
                    "attachment; filename="
                            + java.net.URLEncoder.encode(
                                    DateUtil.getExportDate() + ".xlsx", "UTF-8"));//Excel 扩展名指定为xlsx  SXSSFWorkbook对象只支持xlsx格式
            OutputStream os = response.getOutputStream();
            CellStyle style = workbook.createCellStyle();
            // 设置样式
            style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);//设置单元格着色
            style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  //设置单元格填充样式
            style.setBorderBottom(HSSFCellStyle.BORDER_THIN);//设置下边框
            style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//设置左边框
            style.setBorderRight(HSSFCellStyle.BORDER_THIN);//设置右边框
            style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 居中
            //获取国际化文件
            String employeeCode = requestContext.getMessage("employeeCode");
            String employeeName = requestContext.getMessage("employeeName");
            String orgName = requestContext.getMessage("orgName");
            String startDate = requestContext.getMessage("start.date");
            String endDate = requestContext.getMessage("end.date");
            String courseCode = requestContext.getMessage("courseCode");
            String courseName = requestContext.getMessage("courseName");
            String sessionName = requestContext.getMessage("sessionName");

            List<EmployeeTrainHistoryModel> list = null;
            try {
                            //查询数据库中共有多少条数据
                            query.setTotalItem(employeeTrainHistoryService.fetchCountEmployeeTrainHistoryByQuery(query));
                
                    int page_size = 100000;// 定义每页数据数量
                    int list_count =query.getTotalItem();
                    //总数量除以每页显示条数等于页数
                    int export_times = list_count % page_size > 0 ? list_count / page_size
                            + 1 : list_count / page_size;
                     //循环获取产生每页数据
                    for (int m = 0; m < export_times; m++) {
                        query.setNeedQueryAll(false);
                        query.setPageSize(100000);//每页显示多少条数据
                        query.setCurrentPage(m+1);//设置第几页
                         list=employeeTrainHistoryService.getEmployeeTrainHistoryByQuery(query);
                        //新建sheet
                         Sheet sheet = null;
                            sheet = workbook.createSheet(System.currentTimeMillis()
                                    + CourseCompany+m);
                            // 创建属于上面Sheet的Row,参数0可以是0~65535之间的任何一个,
                            Row header = sheet.createRow(0); // 第0行
                            // 产生标题列,每个sheet页产生一个标题
                             Cell cell;
                            String[] headerArr = new String[] { employeeCode, employeeName,
                                    orgName, startDate, endDate, courseCode, courseName, sessionName,
                                    hoursNunber };
                            for (int j = 0; j < headerArr.length; j++) {
                                cell = header.createCell((short) j);
                                cell.setCellStyle(style);
                                cell.setCellValue(headerArr[j]);
                            }
                            // 迭代数据
                             if (list != null && list.size() > 0) {
                                 int rowNum = 1;
                                 for (int i = 0; i < list.size(); i++) {
                                     EmployeeTrainHistoryModel history=list.get(i);
                                         sheet.setDefaultColumnWidth((short) 17);
                                     Row row = sheet.createRow(rowNum++);
                                     row.createCell((short) 0).setCellValue(
                                             history.getEmployeeCode());
                                     row.createCell((short) 1).setCellValue(
                                             history.getEmployeeName());
                                     row.createCell((short) 2)
                                             .setCellValue(history.getOrgName());
                                     if (history.getTrainBeginTime() != null) {
                                         row.createCell((short) 3).setCellValue(
                                                 DateUtil.toString(history.getTrainBeginTime()));
                                     } else {
                                         row.createCell((short) 3).setCellValue("");
                                     }
                                     if (history.getTrainEndTime() != null) {
                                         row.createCell((short) 4).setCellValue(
                                                 DateUtil.toString(history.getTrainEndTime()));
                                     } else {
                                         row.createCell((short) 4).setCellValue("");
                                     }
                                     row.createCell((short) 5).setCellValue(
                                             history.getCourseCode());
                                     row.createCell((short) 6).setCellValue(
                                             history.getCourseName());
                                     row.createCell((short) 7).setCellValue(
                                             history.getSessionName());
                                     if (history.getHoursNumber() != null)
                                         row.createCell((short) 8).setCellValue(
                                                 history.getHoursNumber().toString());
                                 }
                             }
                             
                             list.clear();
                         }                            
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                workbook.write(os);
                os.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值