数据导出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(); } }
阅读全文
0 0
- excel导入、导出数据
- Ilist导出Excel数据
- 数据导出excel文件
- Excel数据导入导出
- 数据导出至excel
- C#Excel数据导出
- C# Excel数据导出
- 导出页面数据Excel
- List导出数据Excel
- 数据导出到Excel
- Excel导出数据
- wimform 导出数据 excel
- sql数据导出Excel
- 数据导出Excel表
- 将数据导出EXCEL
- 数据导出Excel
- 万能数据导出Excel
- SQL数据 导出Excel
- 文章标题 HDU 2122: Ice_cream’s world III (最小生成树+kruskal)
- 登录过滤器
- 上传图片预览
- 算术编码
- 类的高级特性
- 数据导出excel
- python的全局变量和局部变量
- hdu 2024 C语言合法标识符
- Microsoft OneNote 2017 官方免费版
- Pycharm中使用virtualenv创建虚拟环境
- MySQL数据类型
- 图片+表单的上传
- 串口中怎样接收一个完整数据包的解析
- Bootstrap基础知识