//导出 @RequestMapping("/exportExcel") public void exportExcel(HttpServletResponse response) throws IOException { try { //导出代码 /* response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("studentDto列表.xlsx", "UTF-8")); response.setHeader("Connection", "close"); response.setHeader("Content-Type", "application/octet-stream"); workbook.write(response.getOutputStream());*/ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //创建表头 String[] titles={"学生编号","身份证","姓名","年龄","性别","创建日期","班级名称"}; //创建Excel XSSFWorkbook workbook = new XSSFWorkbook(); //创建sheet页 XSSFSheet sheet = workbook.createSheet(); //创建row行 XSSFRow row = sheet.createRow(0); //循环填充首行 for (int i = 0; i < titles.length; i++) { row.createCell(i).setCellValue(titles[i]); } //循环填充核心数据 //核心数据 List<StudentDto> studentDtoList = studentService.findAll(); for (int j = 0; j < studentDtoList.size(); j++) { XSSFRow row1 = sheet.createRow(j + 1); StudentDto studentDto = studentDtoList.get(j); row1.createCell(0).setCellValue(studentDto.getId()); row1.createCell(1).setCellValue(studentDto.getCode()); row1.createCell(2).setCellValue(studentDto.getName()); row1.createCell(3).setCellValue(studentDto.getAge()); row1.createCell(4).setCellValue(studentDto.getSex()); row1.createCell(5).setCellValue(sdf.format(studentDto.getCreatedDate())); row1.createCell(6).setCellValue(studentDto.getCname()); } OutputStream os; File file = new File("D:\\" + "xxxx" + ".xlsx"); if (!file.exists()){ file.createNewFile(); System.out.println("创建文件路径为:"+file.getPath()); } os=new FileOutputStream(file); workbook.write(os); os.close(); } catch (IOException e) { e.printStackTrace(); } }
java导出代码
最新推荐文章于 2024-05-31 10:46:10 发布