Ví dụ xuất excel

Trong ứng dụng này, chúng tôi có thể xuất dữ liệu sang tệp Excel từ cơ sở dữ liệu [cơ sở dữ liệu H2] trong Spring Boot.   . Chúng tôi đang sử dụng thư viện Thư viện Apache POI thư viện xuất excel

Nội dung chính Hiển thị

Định cấu hình Apache Poi


	org.apache.poi
	poi
	5.1.0


	org.apache.poi
	poi-ooxml
	5.1.0

Quá trình phát triển

1.  

2

3. Xác định kết nối cơ sở dữ liệu trong ứng dụng. tính chất

4. Create an class

5. Tạo một kho lưu trữ

6. Create service

7.  

8.  

9.  

10. Tạo CommandRinerunner trên lớp SpringBootApplication

11. Run project

1.  

2 [Chọn phụ thuộc Cơ sở dữ liệu Spring Web, Spring Data JPA và H2]

3. Xác định kết nối cơ sở dữ liệu trong ứng dụng. tính chất



   4.0.0
   
      org.springframework.boot
      spring-boot-starter-parent
      2.6.3
      
      
   
   com.example
   Export_Excel_Spring_Boot_Example
   0.0.1-SNAPSHOT
   Export_Excel_Spring_Boot_Example
   Demo project for Spring Boot
   
      11
   
   
      
         org.springframework.boot
         spring-boot-starter-data-jpa
      
      
         org.springframework.boot
         spring-boot-starter-web
      
      
         com.h2database
         h2
         runtime
      
      
         org.apache.poi
         poi
         5.1.0
      
      
         org.apache.poi
         poi-ooxml
         5.1.0
      
      
         org.springframework.boot
         spring-boot-starter-test
         test
      
   
   
      
         
            org.springframework.boot
            spring-boot-maven-plugin
         
      
   

3. Xác định kết nối cơ sở dữ liệu trong ứng dụng. tính chất

#H2 Database
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=admin
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto= update
server.port=8888
server.servlet.context-path= /demo

4. Create an class

Sinh viên. java

package com.example.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Student {
    @Id
    @GeneratedValue[strategy = GenerationType.IDENTITY]
    private long id;
    private String studentName;
    private String email;
    private String mobileNo;

    public Student[] {
    }
    public long getId[] {
        return id;
    }
    public void setId[long id] {
        this.id = id;
    }
    public String getStudentName[] {
        return studentName;
    }
    public void setStudentName[String studentName] {
        this.studentName = studentName;
    }
    public String getEmail[] {
        return email;
    }
    public void setEmail[String email] {
        this.email = email;
    }
    public String getMobileNo[] {
        return mobileNo;
    }
    public void setMobileNo[String mobileNo] {
        this.mobileNo = mobileNo;
    }
}

5. Tạo một kho lưu trữ

kho lưu trữ sinh viên. java

package com.example.repo;

import org.springframework.data.jpa.repository.JpaRepository;
import com.example.entity.Student;

public interface StudentRepos extends JpaRepository < Student, Long > {
}

6. Create service

Dịch vụ sinh viên. java

package com.example.service;

import java.util.List;
import com.example.entity.Student;

public interface StudentService {
    void addStudent[Student student];
    List < Student > getTheListStudent[];
}

Dịch vụ sinh viên Impl. java

package com.example.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.entity.Student;
import com.example.repo.StudentRepos;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentRepos studentRepo;

    @Override
    public void addStudent[Student student] {
        studentRepo.save[student];
    }
    @Override
    public List < Student > getTheListStudent[] {
        return studentRepo.findAll[];
    }
}

7.  

Trình tạo Excel. java

package com.example.util;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.example.entity.Student;

public class ExcelGenerator {

    private List < Student > studentList;
    private XSSFWorkbook workbook;
    private XSSFSheet sheet;

    public ExcelGenerator[List < Student > studentList] {
        this.studentList = studentList;
        workbook = new XSSFWorkbook[];
    }
    private void writeHeader[] {
        sheet = workbook.createSheet["Student"];
        Row row = sheet.createRow[0];
        CellStyle style = workbook.createCellStyle[];
        XSSFFont font = workbook.createFont[];
        font.setBold[true];
        font.setFontHeight[16];
        style.setFont[font];
        createCell[row, 0, "ID", style];
        createCell[row, 1, "Student Name", style];
        createCell[row, 2, "Email", style];
        createCell[row, 3, "Mobile No.", style];
    }
    private void createCell[Row row, int columnCount, Object valueOfCell, CellStyle style] {
        sheet.autoSizeColumn[columnCount];
        Cell cell = row.createCell[columnCount];
        if [valueOfCell instanceof Integer] {
            cell.setCellValue[[Integer] valueOfCell];
        } else if [valueOfCell instanceof Long] {
            cell.setCellValue[[Long] valueOfCell];
        } else if [valueOfCell instanceof String] {
            cell.setCellValue[[String] valueOfCell];
        } else {
            cell.setCellValue[[Boolean] valueOfCell];
        }
        cell.setCellStyle[style];
    }
    private void write[] {
        int rowCount = 1;
        CellStyle style = workbook.createCellStyle[];
        XSSFFont font = workbook.createFont[];
        font.setFontHeight[14];
        style.setFont[font];
        for [Student record: studentList] {
            Row row = sheet.createRow[rowCount++];
            int columnCount = 0;
            createCell[row, columnCount++, record.getId[], style];
            createCell[row, columnCount++, record.getStudentName[], style];
            createCell[row, columnCount++, record.getEmail[], style];
            createCell[row, columnCount++, record.getMobileNo[], style];
        }
    }
    public void generateExcelFile[HttpServletResponse response] throws IOException {
        writeHeader[];
        write[];
        ServletOutputStream outputStream = response.getOutputStream[];
        workbook.write[outputStream];
        workbook.close[];
        outputStream.close[];
    }
}

8.  

9.  

10. Tạo CommandRinerunner trên lớp SpringBootApplication

11. Run project

2

sinh viênđiều khiển. java

________số 8

Maven phụ thuộc

mục lục. html



  
    Export Excel File
  
  
    

Click on the download button for exporting data into the excel file.

Download the Excel File

10. Tạo CommandRinerunner trên lớp SpringBootApplication



   4.0.0
   
      org.springframework.boot
      spring-boot-starter-parent
      2.6.3
      
      
   
   com.example
   Export_Excel_Spring_Boot_Example
   0.0.1-SNAPSHOT
   Export_Excel_Spring_Boot_Example
   Demo project for Spring Boot
   
      11
   
   
      
         org.springframework.boot
         spring-boot-starter-data-jpa
      
      
         org.springframework.boot
         spring-boot-starter-web
      
      
         com.h2database
         h2
         runtime
      
      
         org.apache.poi
         poi
         5.1.0
      
      
         org.apache.poi
         poi-ooxml
         5.1.0
      
      
         org.springframework.boot
         spring-boot-starter-test
         test
      
   
   
      
         
            org.springframework.boot
            spring-boot-maven-plugin
         
      
   
0

11. Run project

2http. //máy chủ cục bộ. 8888/demo/h2-console/" để kiểm tra bảng được tạo và dữ liệu có được chèn đúng cách hay không.

Maven phụ thuộc

7. Tạo lớp tạo tệp Excel

→ XSSFWorkbook đang tạo một cửa sổ làm việc là tệp excel của chúng tôi

→ Chúng tôi đang tạo một thể hiện của XSSFWorkbook sau đó chúng tôi đang gọi phương thức "createdesheet []" để tạo tệp excel có tên

→ Chúng tôi đang tạo các hàng và ô của một tờ Excel

→ Sau đó, chúng tôi đang viết dữ liệu vào bảng Excel

Làm cách nào để trả lại tệp excel dưới dạng phản hồi khi khởi động mùa xuân?

Cách tạo báo cáo Excel trong API REST khởi động Spring với Apache Poi và Kotlin. .

Bước 1. Add input password to device.

Bước 2. Create the models.

Bước 3. Chuẩn bị các loại tế bào.

Bước 4. Tạo lớp báo cáo dịch vụ.

Bước 5. Thực hiện REST ReportControll.

Bước 6. Kiểm tra mọi thứ với người gửi thư

Làm cách nào để tạo mùa xuân tập tin khởi động trong Excel?

Dữ liệu xuất khẩu khởi động mùa xuân vào ví dụ Excel. .

Mã của các lớp có thể thực hiện và giao diện kho lưu trữ.

Tuyên bố phụ thuộc cho thư viện Excel.

Mã cho các lớp dịch vụ.

Mã xuất khẩu excel.

Phương thức xử lý mã trong lớp điều khiển.

Thêm liên kết Xuất Excel trong trang xem.

Kiểm tra xuất bản và tải xuống tệp Excel

Làm cách nào để xuất dữ liệu vào Excel trong MVC khởi động mùa xuân?

9. 1 Bước#1. Thêm phụ thuộc Apache POI vào pom. xml

9. 2 Bước#2. Tạo một lớp để thực hiện chức năng xuất Excel

9. 3 Bước#3. Tạo phương thức trong bộ điều khiển để chấp nhận các yêu cầu xuất Excel

9. 4 Bước#4. Sửa đổi trang giao diện người dùng để gửi yêu cầu xuất Excel

Làm cách nào để xuất dữ liệu sang XLSX?

Xuất sang Excel - Chỉ dữ liệu [. .

Trong thanh công cụ ở đầu cửa sổ, nhấp vào nút Xuất và chọn Microsoft Excel - Chỉ dữ liệu [. XLS,.

Chủ Đề