用Sping boot完成导出数据库的里面的数据把数据保存到表格中案例
以下是一个使用Spring Boot导出数据库数据并保存到表格中的示例:
-
首先,我们需要创建一个Spring Boot项目,可以使用Spring Initializr或者自己手动创建。
-
添加依赖:在pom.xml文件中添加以下依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency>这些依赖将帮助我们连接数据库和使用Apache POI库来生成Excel文件。
-
创建实体类:创建一个实体类,用于映射数据库表。例如,我们创建一个名为“User”的类,其中包含id,name和email字段:
@Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String email; // getters and setters } -
创建Repository:创建一个Repository接口,用于与数据库交互。例如,我们创建一个名为“UserRepository”的接口:
public interface UserRepository extends JpaRepository<User, Long> { } -
创建Service:创建一个Service类,用于实现导出数据到Excel文件的业务逻辑。例如,我们创建一个名为“UserService”的类:
@Service public class UserService { @Autowired private UserRepository userRepository; public ByteArrayInputStream exportUsersToExcel() throws IOException { String[] columns = {"ID", "Name", "Email"}; Workbook workbook = new XSSFWorkbook(); ByteArrayOutputStream out = new ByteArrayOutputStream(); Sheet sheet = workbook.createSheet("Users"); Font headerFont = workbook.createFont(); headerFont.setBold(true); headerFont.setFontHeightInPoints((short) 14); headerFont.setColor(IndexedColors.RED.getIndex()); CellStyle headerCellStyle = workbook.createCellStyle(); headerCellStyle.setFont(headerFont); Row headerRow = sheet.createRow(0); for (int i = 0; i < columns.length; i++) { Cell cell = headerRow.createCell(i); cell.setCellValue(columns[i]); cell.setCellStyle(headerCellStyle); } List<User> users = userRepository.findAll(); int rowNum = 1; for (User user : users) { Row row = sheet.createRow(rowNum++); row.createCell(0).setCellValue(user.getId()); row.createCell(1).setCellValue(user.getName()); row.createCell(2).setCellValue(user.getEmail()); } workbook.write(out); workbook.close(); return new ByteArrayInputStream(out.toByteArray()); } }在此示例中,我们使用Apache POI创建一个Excel工作簿,并将用户数据添加到工作表中。最后,我们将Excel文件写入ByteArrayOutputStream中,并将其作为ByteArrayInputStream返回。
-
创建Controller:创建一个Controller类,用于处理导出到Excel文件的请求。例如,我们创建一个名为“UserController”的类:
@RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users/export/excel") public ResponseEntity<ByteArrayInputStream> exportUsersToExcel() throws IOException { HttpHeaders headers = new HttpHeaders(); headers.add("Content-Disposition", "attachment; filename=users.xlsx"); return ResponseEntity.ok().headers(headers).body(userService.exportUsersToExcel()); } }在此示例中,我们使用@RestController注解创建一个RESTful API,该API将导出所有用户数据并将其保存到Excel文件中。
-
运行应用程序并访问API:使用浏览器或其他HTTP客户端向URL“/users/export/excel”发送GET请求,该请求将导出所有用户数据并将其保存到名为“users.xlsx”的Excel文件中。
以上是一个使用Spring Boot导出数据库数据并保存到表格中的示例。您可以根据自己的需要进行修改和扩展
原文地址: http://www.cveoy.top/t/topic/egMg 著作权归作者所有。请勿转载和采集!