java查询结果后并下载输出到excel(OutputStream)

来源:互联网 发布:linux sleep 精度 编辑:程序博客网 时间:2024/06/07 01:30

1、首先查询出所有的数据,使用spring data jpa查询

public Page<Employee> pageQuery(PageRequest pageRequest) {return employeeDao.findAll(pageRequest);}
2、查询出来后导出,设置文件名、文件类型、输出流及文件格式

@Action(value="employeeAction_download")public String download(){try {Page<Employee> list =  facadeService.getEmployeeService().pageQuery(getPageRequest());//使用poi下载文件HSSFWorkbook workbook = new HSSFWorkbook();//创建sheetHSSFSheet sheet1 = workbook.createSheet("分区信息一");//创建row信息HSSFRow row = sheet1.createRow(0);//创建单元格头标row.createCell(0).setCellValue("编号");row.createCell(1).setCellValue("姓名");row.createCell(2).setCellValue("性别");row.createCell(3).setCellValue("入职日期");row.createCell(4).setCellValue("出生年月");row.createCell(5).setCellValue("户籍所在地");row.createCell(6).setCellValue("毕业院校");row.createCell(7).setCellValue("职务");row.createCell(8).setCellValue("社保账号");row.createCell(9).setCellValue("公积金账号");row.createCell(10).setCellValue("电话号码");row.createCell(11).setCellValue("是否离职");//获取数据if (list != null && list.getSize() != 0) {for (Employee e : list) {int lastRowNum = sheet1.getLastRowNum();HSSFRow lastRow = sheet1.createRow(lastRowNum + 1);lastRow.createCell(0).setCellValue(e.getId());lastRow.createCell(1).setCellValue(e.getName());lastRow.createCell(2).setCellValue(e.getSex());lastRow.createCell(3).setCellValue(e.getEntryday());lastRow.createCell(4).setCellValue(e.getBirthday());lastRow.createCell(5).setCellValue(e.getRegister());lastRow.createCell(6).setCellValue(e.getPosition());lastRow.createCell(7).setCellValue(e.getSchool());lastRow.createCell(8).setCellValue(e.getSocialsecurity());lastRow.createCell(9).setCellValue(e.getPublicfund());lastRow.createCell(10).setCellValue(e.getTelephone());lastRow.createCell(11).setCellValue(e.getDeltag());}}//response文件流HttpServletResponse response = ServletActionContext.getResponse();//设置文件名String filename = "中喜员工信息.xls";//设置文件输出头response.setHeader("Content-Disposition", "attachment;filename="+MyFileUtils.encodeDownloadFilename(filename, getRequest().getHeader("user-agent")));//设置文件类型servletAction.getMineServletContext servletContext = ServletActionContext.getServletContext();response.setContentType(servletContext.getMimeType(filename));//下载输出流workbook.write(response.getOutputStream());} catch (IOException e) {e.printStackTrace();}//下载不需要页面跳转return NONE;}
3、在这里出现的一个问题是我设置的每页显示10行数据,导出来的数据也只有10行

//获取分页对象  接受页面的页码和每页显示记录protected int pageNum;//页码protected int pageSize = 10;//每页显示数量public void getPageNum(int pageNum){this.pageNum = pageNum;}public void getPageSize(int pageSize){this.pageSize = pageSize;}

4、为解决这个问题我采用的是输出文件前重新定义一个查询数据的页面设定,代码冗余了,但是问题可以解决,欢迎留言提出新的解决方案~~~

5、对于各个不同浏览器的文字编码不同,创建工具类

public class MyFileUtils {/** * 下载文件时,针对不同浏览器,进行附件名的编码 * @param filename下载文件名 * @param agent客户端浏览器 * @return 编码后的下载附件名 * @throws IOException */public static String encodeDownloadFilename(String filename, String agent) throws IOException {if (agent.contains("Firefox")) { // 火狐浏览器filename = "=?UTF-8?B?" + new BASE64Encoder().encode(filename.getBytes("utf-8")) + "?=";filename = filename.replaceAll("\r\n", "");} else { // IE及其他浏览器filename = URLEncoder.encode(filename, "utf-8");filename = filename.replace("+", " ");}return filename;}}


原创粉丝点击