//excel 报表 @ResponseBody @RequestMapping("/excel") public Object excel() throws IOException { List<Users> list = userService.getAllUsers(); //第一步,创建一个workbook对应一个excel文件 HSSFWorkbook workbook = new HSSFWorkbook(); //第二部,在workbook中创建一个sheet对应excel中的sheet HSSFSheet sheet = workbook.createSheet("用户表"); //第三部,在sheet表中添加表头第0行,老版本的poi对sheet的行列有限制 HSSFRow row = sheet.createRow(0); //第四步,创建单元格,设置表头 HSSFCell cell = row.createCell(0); cell.setCellValue("用户昵称"); cell = row.createCell(1); cell.setCellValue("用户姓名"); cell = row.createCell(2); cell.setCellValue("用户年龄"); cell = row.createCell(3); cell.setCellValue("用户状态"); cell = row.createCell(4); cell.setCellValue("所属部门"); /* cell = row.createCell(4); cell.setCellValue("用户姓名"); cell = row.createCell(5); cell.setCellValue("用户姓名"); */ for (int i = 0; i < list.size(); i++) { HSSFRow row1 = sheet.createRow(i + 1); Users users1 = list.get(i); //创建单元格设值 row1.createCell(0).setCellValue(users1.getUserName()); row1.createCell(1).setCellValue(users1.getUserNickname()); row1.createCell(2).setCellValue(users1.getUserAge()); row1.createCell(3).setCellValue(users1.getUserStatus()); row1.createCell(4).setCellValue(users1.getDeptName()); } //将文件保存到指定的位置 File file=File.createTempFile("orderExcel",".xls"); file.deleteOnExit(); try { FileOutputStream fos = new FileOutputStream(file); workbook.write(fos); System.out.println(file); System.out.println("写入成功"); fos.close(); return 1; } catch (IOException e) { e.printStackTrace(); return 0; }
JAVA Excle报表
最新推荐文章于 2024-07-25 08:16:40 发布