Trả lại tệp excel từ bộ điều khiển phần còn lại của mùa xuân

Tôi đã cố gắng tạo một điểm cuối còn lại trong Spring Boot để đọc từ DB, tạo một tệp excel [Sử dụng Apache POI] được trả lại cho người dùng bằng cách sử dụng HttpServletResponse nhưng khi tôi gọi điều này, excel sẽ được tạo nhưng nó không tải xuống. Tôi đã có một số mã khác trước đó hoạt động tốt nhưng tôi đã vô tình xóa mã đó và bây giờ tôi bị kẹt. Bất kỳ trợ giúp / khách hàng tiềm năng nào đều được đánh giá cao

@RequestMapping[path = "/save", method = RequestMethod.GET]
public ResponseEntity saveToXls[@RequestParam String id, @RequestParam String appName, HttpServletResponse response] {
    AppInstance appInstance = appInstanceRepo.get[id];
    List downloadDetailList = downloadDAO.searchByInstanceId[id];
    //List downloadDetailList = appInstance.getDownloads[];
    System.out.print["LIST SIZE:" + downloadDetailList.size[]];
    String fileName = appName + " report";
    File myFile = new File[fileName + ".xls"];

    FileOutputStream fileOut;
    downloadDetailList.forEach[downloadDetail -> System.out.print[downloadDetail.getSid[]]];
    try {

        try [HSSFWorkbook workbook = new HSSFWorkbook[]] {
            HSSFSheet sheet = workbook.createSheet["lawix10"];
            HSSFRow rowhead = sheet.createRow[[short] 0];
            rowhead.createCell[[short] 0].setCellValue["SID"];
            rowhead.createCell[[short] 1].setCellValue["Download Time"];
            rowhead.createCell[[short] 2].setCellValue["OS Version"];
            int i = 0;

            for [DownloadDetail downloadDetail : downloadDetailList] {
                System.out.print["In loop -2"];
                HSSFRow row = sheet.createRow[[short] i];
                row.createCell[[short] 0].setCellValue[downloadDetail.getSid[]];
                row.createCell[[short] 1].setCellValue[downloadDetail.getDownloadTime[]];
                row.createCell[[short] 2].setCellValue[downloadDetail.getOsVersion[]];
                i++;
            }
            fileOut = new FileOutputStream[myFile];
            workbook.write[fileOut];
        }
        fileOut.close[];

        byte[] buffer = new byte[10240];
        response.addHeader["Content-disposition", "attachment; filename=test.xls"];
        response.setContentType["application/vnd.ms-excel"];
        try [
                InputStream input = new FileInputStream[myFile];
                OutputStream output = response.getOutputStream[];
        ] {
            for [int length = 0; [length = input.read[buffer]] > 0;] {
                output.write[buffer, 0, length];
            }
        }

        response.flushBuffer[];

    } catch [FileNotFoundException e1] {
        e1.printStackTrace[];
    } catch [IOException e1] {
        e1.printStackTrace[];
    }


    return null;
}

CHỈNH SỬA.
Tôi đã thử làm theo cách khác như bên dưới.

        try [InputStream is = new FileInputStream[myFile]] {
            response.addHeader["Content-disposition", "attachment; filename=test.xls"];
            response.setContentType["application/vnd.ms-excel"];
            IOUtils.copy[is, response.getOutputStream[]];

        }
    response.flushBuffer[];

Điều này dường như cũng không cắt nó

Có vẻ như bạn đang làm một số điều rất kỳ lạ xung quanh việc lấy kết quả ra và tôi khá chắc chắn rằng


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

4 không trả lại những gì bạn nghĩ

Tôi thực sự khuyên bạn nên thay đổi


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

5 để trả lại sổ làm việc, sau đó bạn có thể thực hiện đơn giản hơn nhiều

Workbook wb = excelService.exportEventsToCSV[];
response.setHeader["Content-disposition", "attachment; filename=test.xls"];
wb.write[ response.getOutputStream[] ];

Điều đó sẽ hoạt động tốt và cung cấp cho người dùng cuối thứ họ mong đợi

Tệp Excel là định dạng tệp bảng tính do Microsoft tạo để sử dụng với Microsoft Excel. Bạn có thể sử dụng tệp để tạo, xem, chỉnh sửa, phân tích dữ liệu, biểu đồ, ngân sách, v.v. Trong hướng dẫn này, tôi sẽ chỉ cho bạn cách sử dụng Spring Boot để tải xuống tệp Excel từ một bảng trong Cơ sở dữ liệu MySQL bằng Apache POI

Bài viết liên quan.
– Khởi động mùa xuân. Tải lên/Nhập dữ liệu tệp Excel vào Cơ sở dữ liệu MySQL
– Ví dụ về tải lên tệp nhiều phần của Spring Boot
– Cách tải lên nhiều tệp trong Java Spring Boot
– Upload/Import CSV file to MySQL Database in Spring Boot

Tệp CSV thay thế.
Ví dụ về tệp CSV Tải xuống Spring Boot từ Cơ sở dữ liệu

Triển khai.
– Triển khai ứng dụng Spring Boot trên AWS – Elastic Beanstalk
– Docker Compose. Ví dụ về Spring Boot và MySQL


nội dung

Spring Boot Rest API. Tải xuống tệp Excel

Giả sử rằng chúng ta có một bảng hướng dẫn trong cơ sở dữ liệu MySQL như thế này

Chúng ta sẽ tạo một Ứng dụng khởi động mùa xuân cung cấp API để tải xuống dữ liệu bảng MySQL dưới dạng tệp Excel với nội dung sau

Nếu bạn gửi yêu cầu tới


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

6, máy chủ sẽ trả lời phản hồi bằng tệp Excel hướng dẫn. xlsx chứa dữ liệu trong bảng MySQL

Làm thế nào để thực hiện việc này?
Bạn cần đặt tiêu đề HTTP.

"Content-disposition" : "attachment; filename=[yourFileName]" 
"Content-Type" : "application/vnd.ms-excel"

Chúng tôi sẽ áp dụng thông tin trên sau trong hướng dẫn này

Bạn cũng có thể tham khảo cách upload File Excel lên Spring Server & lưu trữ dữ liệu trong MySQL Database tại bài viết.
Khởi động mùa xuân. Tải lên/Nhập dữ liệu tệp Excel vào Cơ sở dữ liệu MySQL

Công nghệ

  • java8
  • Khởi động mùa xuân 2 [với Spring Web MVC]
  • maven 3. 6. 1
  • Apache POI 4. 1. 2

Cấu trúc dự án

Đây là thư mục dự án mà chúng ta sẽ xây dựng


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

0 cung cấp hàm ghi ra file Excel

– Lớp mô hình dữ liệu


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

1 tương ứng với các hướng dẫn về thực thể và bảng.

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

2 là một giao diện mở rộng

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

3 để duy trì dữ liệu.


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

4 sử dụng phương thức

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

0 và

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

2 tải dữ liệu ra file Excel


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

7 cuộc gọi

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

4 phương thức và xuất Rest API để tải xuống tệp Excel

– ứng dụng. thuộc tính chứa cấu hình cho Dữ liệu mùa xuân.
– pom. xml cho Khởi động mùa xuân, trình kết nối MySQL, phụ thuộc Apache POI.

Thiết lập khởi động mùa xuân Tải xuống dự án tệp Excel

Sử dụng công cụ web Spring hoặc công cụ phát triển của bạn [Spring Tool Suite, Eclipse, Intellij] để tạo dự án Spring Boot

Sau đó mở pom. xml và thêm các phụ thuộc này


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

Định cấu hình nguồn dữ liệu mùa xuân, JPA, Hibernate

Trong thư mục src/main/resource, mở ứng dụng. thuộc tính và viết những dòng này

        try [InputStream is = new FileInputStream[myFile]] {
            response.addHeader["Content-disposition", "attachment; filename=test.xls"];
            response.setContentType["application/vnd.ms-excel"];
            IOUtils.copy[is, response.getOutputStream[]];

        }
    response.flushBuffer[];
4
  • Thuộc tính
    
    	org.springframework.boot
    	spring-boot-starter-data-jpa
    
    
    
    	org.springframework.boot
    	spring-boot-starter-web
    
    
    
    	org.apache.poi
    	poi-ooxml
    	4.1.2
    
    
    
    	mysql
    	mysql-connector-java
    	runtime
    
    
    9 &
            try [InputStream is = new FileInputStream[myFile]] {
                response.addHeader["Content-disposition", "attachment; filename=test.xls"];
                response.setContentType["application/vnd.ms-excel"];
                IOUtils.copy[is, response.getOutputStream[]];
    
            }
        response.flushBuffer[];
    
    40 giống như cài đặt cơ sở dữ liệu của bạn
  • Spring Boot sử dụng Hibernate để triển khai JPA, chúng tôi định cấu hình
            try [InputStream is = new FileInputStream[myFile]] {
                response.addHeader["Content-disposition", "attachment; filename=test.xls"];
                response.setContentType["application/vnd.ms-excel"];
                IOUtils.copy[is, response.getOutputStream[]];
    
            }
        response.flushBuffer[];
    
    41 cho cơ sở dữ liệu MySQL
  •         try [InputStream is = new FileInputStream[myFile]] {
                response.addHeader["Content-disposition", "attachment; filename=test.xls"];
                response.setContentType["application/vnd.ms-excel"];
                IOUtils.copy[is, response.getOutputStream[]];
    
            }
        response.flushBuffer[];
    
    42 được sử dụng để khởi tạo cơ sở dữ liệu. Ta đặt giá trị thành giá trị
            try [InputStream is = new FileInputStream[myFile]] {
                response.addHeader["Content-disposition", "attachment; filename=test.xls"];
                response.setContentType["application/vnd.ms-excel"];
                IOUtils.copy[is, response.getOutputStream[]];
    
            }
        response.flushBuffer[];
    
    43 để trong cơ sở dữ liệu tự động tạo bảng tương ứng với mô hình dữ liệu đã xác định. Mọi thay đổi đối với mô hình cũng sẽ kích hoạt cập nhật bảng. Đối với sản xuất, thuộc tính này phải là
            try [InputStream is = new FileInputStream[myFile]] {
                response.addHeader["Content-disposition", "attachment; filename=test.xls"];
                response.setContentType["application/vnd.ms-excel"];
                IOUtils.copy[is, response.getOutputStream[]];
    
            }
        response.flushBuffer[];
    
    44

Xác định mô hình dữ liệu

Mô hình dữ liệu của chúng tôi là Hướng dẫn với bốn trường. id, tiêu đề, mô tả, xuất bản.
Trong gói mô hình, chúng tôi định nghĩa lớp


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

1.

mô hình/Hướng dẫn. java

Workbook wb = excelService.exportEventsToCSV[];
response.setHeader["Content-disposition", "attachment; filename=test.xls"];
wb.write[ response.getOutputStream[] ];
2

– Chú thích

        try [InputStream is = new FileInputStream[myFile]] {
            response.addHeader["Content-disposition", "attachment; filename=test.xls"];
            response.setContentType["application/vnd.ms-excel"];
            IOUtils.copy[is, response.getOutputStream[]];

        }
    response.flushBuffer[];
46 chỉ ra rằng lớp này là một lớp Java liên tục.
– Chú thích
        try [InputStream is = new FileInputStream[myFile]] {
            response.addHeader["Content-disposition", "attachment; filename=test.xls"];
            response.setContentType["application/vnd.ms-excel"];
            IOUtils.copy[is, response.getOutputStream[]];

        }
    response.flushBuffer[];
47 cung cấp bảng ánh xạ thực thể này.
– Chú thích
        try [InputStream is = new FileInputStream[myFile]] {
            response.addHeader["Content-disposition", "attachment; filename=test.xls"];
            response.setContentType["application/vnd.ms-excel"];
            IOUtils.copy[is, response.getOutputStream[]];

        }
    response.flushBuffer[];
48 dành cho khóa chính.
– Chú thích
        try [InputStream is = new FileInputStream[myFile]] {
            response.addHeader["Content-disposition", "attachment; filename=test.xls"];
            response.setContentType["application/vnd.ms-excel"];
            IOUtils.copy[is, response.getOutputStream[]];

        }
    response.flushBuffer[];
49 được sử dụng để xác định cột trong cơ sở dữ liệu ánh xạ trường chú thích.

Tạo Data Repository để làm việc với Database

Hãy tạo một kho lưu trữ để tương tác với các Hướng dẫn từ cơ sở dữ liệu.
Trong gói kho lưu trữ, tạo giao diện


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

2 mở rộng

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

3.

kho lưu trữ/Hướng dẫnRepository. java

Workbook wb = excelService.exportEventsToCSV[];
response.setHeader["Content-disposition", "attachment; filename=test.xls"];
wb.write[ response.getOutputStream[] ];
9

Bây giờ chúng ta có thể sử dụng các phương thức Kho lưu trữ Jpa.

Workbook wb = excelService.exportEventsToCSV[];
response.setHeader["Content-disposition", "attachment; filename=test.xls"];
wb.write[ response.getOutputStream[] ];
22,
Workbook wb = excelService.exportEventsToCSV[];
response.setHeader["Content-disposition", "attachment; filename=test.xls"];
wb.write[ response.getOutputStream[] ];
23,
Workbook wb = excelService.exportEventsToCSV[];
response.setHeader["Content-disposition", "attachment; filename=test.xls"];
wb.write[ response.getOutputStream[] ];
24,
Workbook wb = excelService.exportEventsToCSV[];
response.setHeader["Content-disposition", "attachment; filename=test.xls"];
wb.write[ response.getOutputStream[] ];
25,
Workbook wb = excelService.exportEventsToCSV[];
response.setHeader["Content-disposition", "attachment; filename=test.xls"];
wb.write[ response.getOutputStream[] ];
26,
Workbook wb = excelService.exportEventsToCSV[];
response.setHeader["Content-disposition", "attachment; filename=test.xls"];
wb.write[ response.getOutputStream[] ];
27,
Workbook wb = excelService.exportEventsToCSV[];
response.setHeader["Content-disposition", "attachment; filename=test.xls"];
wb.write[ response.getOutputStream[] ];
28… mà không thực hiện các phương pháp này

Số lượng hàng trong tệp Excel [cũng như bảng hướng dẫn] có thể lớn, vì vậy bạn có thể chỉ muốn lấy nhiều hàng cùng một lúc bằng cách sửa đổi Kho lưu trữ này để hoạt động với Phân trang, hướng dẫn có thể được tìm thấy tại.
Ví dụ về bộ lọc và phân trang khởi động mùa xuân. JPA mùa xuân, có thể phân trang

Triển khai lớp trợ giúp Excel

Chúng ta sẽ sử dụng các lớp Apache POI như. ________ 229, ________ 290, ________ 291, ________ 292.
Tôi xin tóm tắt các bước ghi ra file Excel.

  • tạo một
    Workbook wb = excelService.exportEventsToCSV[];
    response.setHeader["Content-disposition", "attachment; filename=test.xls"];
    wb.write[ response.getOutputStream[] ];
    
    29 mới
  • tạo một
    Workbook wb = excelService.exportEventsToCSV[];
    response.setHeader["Content-disposition", "attachment; filename=test.xls"];
    wb.write[ response.getOutputStream[] ];
    
    90 mới bằng cách sử dụng
    Workbook wb = excelService.exportEventsToCSV[];
    response.setHeader["Content-disposition", "attachment; filename=test.xls"];
    wb.write[ response.getOutputStream[] ];
    
    95
  • lặp qua danh sách
    Workbook wb = excelService.exportEventsToCSV[];
    response.setHeader["Content-disposition", "attachment; filename=test.xls"];
    wb.write[ response.getOutputStream[] ];
    
    96
  • đối với mỗi
    
    	org.springframework.boot
    	spring-boot-starter-data-jpa
    
    
    
    	org.springframework.boot
    	spring-boot-starter-web
    
    
    
    	org.apache.poi
    	poi-ooxml
    	4.1.2
    
    
    
    	mysql
    	mysql-connector-java
    	runtime
    
    
    1, hãy tạo một
    Workbook wb = excelService.exportEventsToCSV[];
    response.setHeader["Content-disposition", "attachment; filename=test.xls"];
    wb.write[ response.getOutputStream[] ];
    
    91 bằng cách sử dụng
    Workbook wb = excelService.exportEventsToCSV[];
    response.setHeader["Content-disposition", "attachment; filename=test.xls"];
    wb.write[ response.getOutputStream[] ];
    
    99
  • tạo
    Workbook wb = excelService.exportEventsToCSV[];
    response.setHeader["Content-disposition", "attachment; filename=test.xls"];
    wb.write[ response.getOutputStream[] ];
    
    92 bằng cách sử dụng
    
    	org.springframework.boot
    	spring-boot-starter-data-jpa
    
    
    
    	org.springframework.boot
    	spring-boot-starter-web
    
    
    
    	org.apache.poi
    	poi-ooxml
    	4.1.2
    
    
    
    	mysql
    	mysql-connector-java
    	runtime
    
    
    91, sau đó sử dụng
    
    	org.springframework.boot
    	spring-boot-starter-data-jpa
    
    
    
    	org.springframework.boot
    	spring-boot-starter-web
    
    
    
    	org.apache.poi
    	poi-ooxml
    	4.1.2
    
    
    
    	mysql
    	mysql-connector-java
    	runtime
    
    
    92
  • sử dụng
    
    	org.springframework.boot
    	spring-boot-starter-data-jpa
    
    
    
    	org.springframework.boot
    	spring-boot-starter-web
    
    
    
    	org.apache.poi
    	poi-ooxml
    	4.1.2
    
    
    
    	mysql
    	mysql-connector-java
    	runtime
    
    
    93 để viết ra
    
    	org.springframework.boot
    	spring-boot-starter-data-jpa
    
    
    
    	org.springframework.boot
    	spring-boot-starter-web
    
    
    
    	org.apache.poi
    	poi-ooxml
    	4.1.2
    
    
    
    	mysql
    	mysql-connector-java
    	runtime
    
    
    94

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

9

Trong gói trợ giúp, chúng tôi tạo lớp


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

95 với phương thức

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

96 để xử lý danh sách Hướng dẫn, trả về

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

97 cho tệp Excel sau

Đây là mã đầy đủ của người trợ giúp/ExcelHelper. java


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

7

Tạo dịch vụ tệp Excel

Lớp


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

4 sẽ được chú thích bằng chú thích

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

99, nó sử dụng phương pháp

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

0 và

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

2 trong

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

72 để đọc dữ liệu từ cơ sở dữ liệu và trả về

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

97

dịch vụ/ExcelDịch vụ. java


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

4

Tạo bộ điều khiển để tải xuống tệp Excel

Trong gói bộ điều khiển, chúng tôi tạo lớp


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

7 cho Rest Apis.

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

75 dùng để định cấu hình nguồn gốc được phép.
– Chú thích

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

76 cho biết đây là bộ điều khiển.
– Phương thức

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

77getFile[].
– Chúng tôi cũng sử dụng

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

78 để đưa việc triển khai đậu

	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

4 vào biến cục bộ.

bộ điều khiển/ExcelController. java


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

1

Chạy ứng dụng Spring Boot Tải xuống tệp Excel

Chạy ứng dụng Spring Boot bằng lệnh.


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

40

Sự kết luận

Hôm nay chúng ta đã xây dựng Rest API bằng Spring Boot để tải xuống tệp Excel chứa dữ liệu bảng trong cơ sở dữ liệu MySQL

Chúng ta cũng xem cách sử dụng Apache POI để ghi dữ liệu vào Excel Sheet,


	org.springframework.boot
	spring-boot-starter-data-jpa



	org.springframework.boot
	spring-boot-starter-web



	org.apache.poi
	poi-ooxml
	4.1.2



	mysql
	mysql-connector-java
	runtime

3 để truy xuất các mục trong bảng cơ sở dữ liệu mà không cần mã soạn sẵn

Làm cách nào để tạo tệp Excel bằng Spring Boot?

Giữ Eclipse IDE sẵn sàng
Tạo Dự án khởi động khởi động mùa xuân cho ví dụ về xuất dữ liệu sang trang tính excel [Chọn các phụ thuộc Cơ sở dữ liệu Spring Web, Spring Data JPA và H2].
Xác định kết nối cơ sở dữ liệu trong ứng dụng. đặc tính. .
Tạo lớp thực thể. .
Tạo một kho lưu trữ. .
Tạo dịch vụ. .
Tạo lớp Trình tạo tệp Excel

Làm cách nào để tải xuống Excel trong Spring MVC?

Trong hướng dẫn này, nó trình bày cách sử dụng lớp AbstractExcelView trong ứng dụng Spring MVC để xuất dữ liệu sang tệp Excel để tải xuống. .
POI của Apache. Lấy thư viện Apache POI để tạo file excel. .
Bộ điều khiển. .
Trừu TượngExcelXem. .
Cấu hình mùa xuân. .

Làm cách nào tôi có thể tải xuống tệp Excel bằng phản hồi API góc?

7 câu trả lời .
Bạn cần đặt tiêu đề phản hồi { responseType. 'blob'} trong yêu cầu
Bạn cần nhập đúng Loại MIME khi bạn đang tạo tệp blob. [f. e. ứng dụng/octet-stream hoặc ứng dụng/vnd. openxmlformats-officedocument. bảng tínhml. tờ giấy]

Làm cách nào để đọc dữ liệu từ trang tính Excel và chèn vào bảng cơ sở dữ liệu trong Spring Batch?

Nếu bạn muốn đọc dữ liệu đầu vào của công việc Spring Batch từ bảng tính Excel, bạn phải thêm các phụ thuộc Spring Batch Excel và Apache POI vào đường dẫn lớp. If you want to read the input data of your batch job by using Spring Batch Excel and Apache POI, you have to use the PoiItemReader class.

Chủ Đề