wenjunlong88_easy 2019-03-13 00:35 采纳率: 75%
浏览 2454
已采纳

java怎么实现读取数据库并生成excel表下载

页面是jsp做的,后台的controller如下所示,为什么我点击前端还是实现不了下载呢?

@RequestMapping(value = "regionInfoDload",method = RequestMethod.GET)
    public void infoDownLoad(Integer regionLevel, HttpServletRequest request, HttpServletResponse response, Model model) {
        System.out.println("从前台获得的regionLevel: " + regionLevel);
        List<TljsjkAjlj15> listTest = tljsjkAjlj15Service.conditionDownload(regionLevel, null,null);
        System.out.println("测试下载查询的数据总量: " + listTest.size());
        //1.创建excel的文档对象
        HSSFWorkbook wb = new HSSFWorkbook();
        //2.创建excel的表单
        HSSFSheet sheet = wb.createSheet("性别");
        //设置行的高度
        //sheet.setColumnWidth(i, 256*18);
        //3.excel字体
        HSSFFont font = wb.createFont();
        //4.excel样式
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平居中对齐
        HSSFRow row = sheet.createRow(1);
        HSSFCell cell = row.createCell(1);
        cell.setCellValue("姓名");
        cell = row.createCell(2);
        cell.setCellValue("身高预测值");
        System.out.println("======================100================");
        for(int i =2;i<listTest.size() + 1;i++) {
            row = sheet.createRow(i);
            cell = row.createCell(1);
            cell.setCellValue(listTest.get(i - 2).getJh() == null ? "":listTest.get(i - 2).getJh());
            cell = row.createCell(2);
            cell.setCellValue(listTest.get(i - 2).getYlmbqYc() == null ? 0: listTest.get(i - 2).getYlmbqYc());
        }
        System.out.println("======================200================");
        try {
            String fileName = new String("运营数据.xls".getBytes("utf-8"),"utf-8");
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println("======================300================");
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            wb.write(os);
            os.flush();
            Thread.sleep(10000);
            System.out.println("======================400================");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

从前端发送请求,controller中的的代码执行到:System.out.println("======================400================");这一句,为什么还是实现不了?
大家知道我出错了在哪里吗?抑或大家有什么别的解决方案吗?望请各位路过的大咖不吝赐教。跪谢!

  • 写回答

7条回答 默认 最新

  • 混沌太初 2019-03-13 17:36
    关注

    粗略看了下,,看着累,我贴上我的你分析下你的吧,架构SSM
    首先前端,自己搞个单击事件就O啦

    <a href="/employee/print" class="easyui-linkbutton" iconCls="icon-print" plain="true" data-cmd="print">导出</a>
    

    Controller层

    @RequestMapping("/print")
        @ResponseBody
        public void print( HttpServletResponse response) throws IOException {
            // 获取到所有的客服资源池对像
            EmployeeQuery employeeQuery = new EmployeeQuery();
            employeeQuery.setRows(Integer.MAX_VALUE);//设置为最大
            List<Employee> list = employeeService.findPageByQuery(employeeQuery).getRows();//getRows()中的rows是easyui的规定的一个参数也就是获取数据
            // 创建一个Excl表格对象
            HSSFWorkbook wb = new HSSFWorkbook();
            // 建立新的sheet对象(excel的表单)
            HSSFSheet sheet = wb.createSheet("员工Excl表格");
            // 在sheet里创建第二行
            HSSFRow row1 = sheet.createRow(0);
            // 创建第一行设置单元格内容
            row1.createCell(0).setCellValue("员工姓名");
            row1.createCell(1).setCellValue("员工密码");
            row1.createCell(2).setCellValue("职位");
            row1.createCell(3).setCellValue("手机");
            row1.createCell(4).setCellValue("邮件");
            row1.createCell(5).setCellValue("部门");
            row1.createCell(6).setCellValue("入职时间");
            row1.createCell(7).setCellValue("状态");
            row1.createCell(8).setCellValue("性别");
    
            for (int i = 0; i < list.size(); i++) {
                HSSFRow row = sheet.createRow(i + 1);
                Employee employee = list.get(i);
                // 创建单元格并设置单元格内容
                row.createCell(0).setCellValue(employee.getUsername());//员工姓名
                row.createCell(1).setCellValue(employee.getPassword());//员工密码
                row.createCell(2).setCellValue(employee.getPosition());//职位
                row.createCell(3).setCellValue(employee.getPhone());//手机
                row.createCell(4).setCellValue(employee.getEmail());//邮箱
                //部门
                String name = "";
                try {
                    name = employee.getDepartment().getName();
                } catch (Exception e) {
    
                }
                row.createCell(5).setCellValue(name);
                //入职时间
                String time = "";
                try {
                    Date inputTime = employee.getInputTime();
                    time = DateUtils.formatDateToString(inputTime, DateUtils.DATE_FORMAT_YMD);// DateUtils是我自己封装的一个时间工具类
                } catch (Exception e) {
    
                }
                row.createCell(6).setCellValue(time);
                //状态
                String status = "";
                try {
                    int num = employee.getStatus();
                    if (num == 1) {
                        status = "在职";
                    } else if (num == 2) {
                        status = "离职";
                    }
                } catch (Exception e) {
    
                }
                row.createCell(7).setCellValue(status);
                //性别
                String sex = "";
                try {
                    int num = employee.getSex();
                    if (num == 1) {
                        sex = "男";
                    } else if (num == 2) {
                        sex = "女";
                    }
                } catch (Exception e) {
    
                }
                row.createCell(8).setCellValue(sex);
            }
    
            // 输出Excel文件
            ServletOutputStream outputStream = response.getOutputStream();
            response.reset();
            response.setHeader("Content-disposition", "attachment; filename=details.xls");
            response.setContentType("application/msexcel");
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器