数据导出excel

来源:互联网 发布:淘宝客服不回消息 编辑:程序博客网 时间:2024/05/29 09:15
    private void exportExcel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        System.out.println("kaishi--------------");        String class_id = (String) request.getSession().getAttribute("class_id");        HSSFWorkbook workbook = new HSSFWorkbook();        HSSFSheet sheet = workbook.createSheet(class_id);        sheet.setDefaultColumnWidth(15);        // 产生表格标题行        String[] header = {"学号", "姓名", "性别", "状态", "学分", "手机号", "详细地址", "QQ", "民族", "宿舍", "班级", "班级职位", "入学年份"};        HSSFRow headerRow = sheet.createRow(0);        for (int i = 0; i < header.length; i++) {            HSSFCell cell = headerRow.createCell(i);            cell.setCellValue(header[i]);        }        //把内容写入excel        List<Student> allStudent = new ClassDaoImpl().queryAllStudentDetail(class_id);        System.out.println(allStudent.toString());        int index = 0;        String state = "";        for (int i = 0; i < allStudent.size(); i++) {            if (allStudent.get(i).getStudent_state() == 1) state = "在读";            else if (allStudent.get(i).getStudent_state() == 1) state = "休学";            else if (allStudent.get(i).getStudent_state() == 1) state = "退学";            else if (allStudent.get(i).getStudent_state() == 1) state = "毕业";            HSSFRow row = sheet.createRow(++index);            String[] bodyRow = {allStudent.get(i).getStudent_id(),                allStudent.get(i).getStudent_name(), allStudent.get(i).getStudent_sex(), state,                String.valueOf(allStudent.get(i).getStudent_credit()), allStudent.get(i).getStudent_phone(), allStudent.get(i).getStudent_address(),                allStudent.get(i).getStudent_qq(), allStudent.get(i).getStudent_nation(), allStudent.get(i).getStudent_dormitory_id(),                allStudent.get(i).getStudent_class_id(), allStudent.get(i).getStudent_duty(), allStudent.get(i).getStudent_enter_year()};            for (int j = 0; j < bodyRow.length; j++) {                HSSFCell cell = row.createCell(j);                cell.setCellValue(bodyRow[j]);            }        }        //下载提示框 设置response参数,可以打开下载页面        ByteArrayOutputStream os = new ByteArrayOutputStream();        try {            workbook.write(os);        } catch (IOException e) {            e.printStackTrace();        }        byte[] content = os.toByteArray();        InputStream is = new ByteArrayInputStream(content);        response.reset();        response.setContentType("application/vnd.ms-excel;charset=utf-8");        response.setHeader("Content-Disposition", "attachment;filename="+ new String(("班级信息表" + ".xls").getBytes(), "iso-8859-1"));        ServletOutputStream out = response.getOutputStream();        BufferedInputStream bis = null;        BufferedOutputStream bos = null;        try {            bis = new BufferedInputStream(is);            bos = new BufferedOutputStream(out);            byte[] buff = new byte[2048];            int bytesRead;            // Simple read/write loop.            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {                bos.write(buff, 0, bytesRead);            }        } catch (final IOException e) {            throw e;        } finally {            if (bis != null)                bis.close();            if (bos != null)                bos.close();        }    }