wenjunlong88_easy
2019-03-13 00:35
采纳率: 100%
浏览 2.4k
已采纳

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条回答 默认 最新

相关推荐 更多相似问题