旋转的钢笔 2017-01-17 06:49 采纳率: 54.5%
浏览 5532
已结题

java POI3.8 Excel 下载 发现不可读取内容 解决办法??

图片说明

图片说明

 /**
     * 导出历史记录
     */
    @SuppressWarnings({ "deprecation", "unchecked" })
    @RequestMapping("export-TrainHistoryRecord")
    @ResponseBody
    protected void buildExcelDocument(EmployeeTrainHistoryQuery query,ModelMap model,
            SXSSFWorkbook workbook, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        //这里使用SXSSFWorkbook对象,支持1048576条数据
        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();// new 

            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");
            String hoursNunber = requestContext.getMessage("hoursNunber");

            // FileOutputStream(filePath);
            List<EmployeeTrainHistoryModel> list = null;
             query.setNeedQueryAll(true);
            try {

//          if("0".equals(query.getTrainFlag())){
                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);
                         int len = list.size() < page_size ? list.size() : page_size;
                        //新建sheet
                         Sheet sheet = null;
                            sheet = workbook.createSheet(System.currentTimeMillis()
                                    + CourseCompany+m);
//                       for (int n = 0; n < len; n++) {
                            // 迭代数据
                            if (list != null && list.size() > 0) {

                                int rowNum = 1;
                                int max_row=300000;
                                int sheet_count=0;

                                for (int i = 0; i < list.size(); i++) {
                                    EmployeeTrainHistoryModel history=list.get(i);
                                    //如果当前行超过单页签可容纳最大行则换页签
//                                  if(i==0||i==(max_row*sheet_count+1)){


                                        sheet.setDefaultColumnWidth((short) 17);
                                        // 创建属于上面Sheet的Row,参数0可以是0~65535之间的任何一个,
                                        Row header = sheet.createRow(0); // 第0行

                                        // 产生标题列
                                        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]);
                                            }

                                    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);
                model.put("msg", "1");
                os.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

  • 写回答

2条回答 默认 最新

  • 关注

    版本对不上,本地安装的excel版本跟开发使用的版本不一致

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月24日

悬赏问题

  • ¥30 matlab解优化问题代码
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline