@RequestMapping("/exportExcel") public void exportExcel(HttpServletResponse response) throws IOException { // 获取要导出的数据 List<Student> studentList = studentService.getAllStudent(); // 创建工作簿 @SuppressWarnings("resource") XSSFWorkbook workbook = new XSSFWorkbook(); // 创建工作表 XSSFSheet sheet = workbook.createSheet("学生信息表"); // 创建表头 XSSFRow row = sheet.createRow(0); row.createCell(0).setCellValue("ID"); 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("所属宿舍"); // 填充数据 for (int i = 0; i < studentList.size(); i++) { row = sheet.createRow(i + 1); row.createCell(0).setCellValue(studentList.get(i).getStuId()); row.createCell(1).setCellValue(studentList.get(i).getStuNo()); row.createCell(2).setCellValue(studentList.get(i).getStuName()); row.createCell(3).setCellValue(studentList.get(i).getStuSex()); row.createCell(4).setCellValue(studentList.get(i).getStuBirth()); row.createCell(5).setCellValue(studentList.get(i).getStuAddress()); row.createCell(6).setCellValue(studentList.get(i).getStuPhone()); row.createCell(7).setCellValue(studentList.get(i).getStuPhoto()); row.createCell(8).setCellValue(studentList.get(i).getStuRemark()); row.createCell(9).setCellValue(studentList.get(i).getStuDormitory()); } // 设置响应头信息 response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=student.xls"); // 输出Excel文件 workbook.write(response.getOutputStream()); }需要的依赖
时间: 2024-03-18 17:41:03 浏览: 19
该代码段需要以下依赖:
- poi-ooxml:用于创建和操作Office Open XML格式的文档,如XLSX、PPTX和DOCX等。
- poi-ooxml-schemas:用于访问ECMA Office Open XML格式的XSD文件。
- poi:用于操作Microsoft文档的Java库,如XLS、PPT、DOC等。
- servlet-api:Java Servlet API,用于处理HTTP请求和响应。
可以使用以下Maven依赖管理工具中的依赖项:
```
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
```