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

xiaoxiao2021-02-28  60

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(); //创建sheet HSSFSheet 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.getMine ServletContext 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; } }

转载请注明原文地址: https://www.6miu.com/read-81234.html

最新回复(0)