导出Excel(POI使用)(二)

调用方法如下


@RequestMapping(value = "/jira/exportExcelNew.do", method = RequestMethod.GET)
    @ResponseBody
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
        //获取数据
        List<BackLogForPdu> list = testBootService.getBacklog(10005);

        //excel标题
        String[] title = {"PDU", "Backlog数"};

        //excel文件名
        String fileName = "Backlog信息表" + System.currentTimeMillis() + ".xls";
        //sheet名
        String sheetName = "学生信息表";
        String[][] content = null;

        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            BackLogForPdu obj = list.get(i);
            content[i][0] = obj.getPname().toString();
            content[i][1] = obj.getBacklognum().toString();
        }

//创建HSSFWorkbook
        HSSFWorkbook wb = ExcelExport.getHSSFWorkbook(sheetName, title, content, null);

//响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

猜你喜欢

转载自blog.csdn.net/intelrain/article/details/80536843