最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot實現(xiàn)數(shù)據(jù)導(dǎo)入導(dǎo)出與報表生成的完整教程

 更新時間:2026年04月12日 10:03:33   作者:星辰徐哥  
本文將帶你掌握Spring Boot數(shù)據(jù)導(dǎo)入導(dǎo)出與報表生成的核心概念與使用方法,包括數(shù)據(jù)導(dǎo)入導(dǎo)出的定義與特點,幫你學(xué)會在實際開發(fā)中處理數(shù)據(jù)導(dǎo)入導(dǎo)出與報表生成問題

學(xué)習(xí)目標(biāo)與重點提示

學(xué)習(xí)目標(biāo):掌握Spring Boot數(shù)據(jù)導(dǎo)入導(dǎo)出與報表生成的核心概念與使用方法,包括數(shù)據(jù)導(dǎo)入導(dǎo)出的定義與特點、Spring Boot與數(shù)據(jù)導(dǎo)入導(dǎo)出的集成、Spring Boot與數(shù)據(jù)導(dǎo)入導(dǎo)出的配置、Spring Boot與報表生成的基本方法、Spring Boot的實際應(yīng)用場景,學(xué)會在實際開發(fā)中處理數(shù)據(jù)導(dǎo)入導(dǎo)出與報表生成問題。

重點:數(shù)據(jù)導(dǎo)入導(dǎo)出的定義與特點、Spring Boot與數(shù)據(jù)導(dǎo)入導(dǎo)出的集成、Spring Boot與數(shù)據(jù)導(dǎo)入導(dǎo)出的配置、Spring Boot與報表生成的基本方法Spring Boot的實際應(yīng)用場景。

數(shù)據(jù)導(dǎo)入導(dǎo)出概述

數(shù)據(jù)導(dǎo)入導(dǎo)出是Java開發(fā)中的重要組件。

數(shù)據(jù)導(dǎo)入導(dǎo)出的定義

定義:數(shù)據(jù)導(dǎo)入導(dǎo)出是指將數(shù)據(jù)從一個系統(tǒng)導(dǎo)入到另一個系統(tǒng),或從一個系統(tǒng)導(dǎo)出到另一個系統(tǒng)的過程。

作用

  • 實現(xiàn)數(shù)據(jù)的遷移。
  • 實現(xiàn)數(shù)據(jù)的備份。
  • 實現(xiàn)數(shù)據(jù)的共享。

常見的數(shù)據(jù)導(dǎo)入導(dǎo)出格式

  • CSV:Comma-Separated Values,逗號分隔值格式。
  • Excel:Microsoft Excel格式。
  • JSON:JavaScript Object Notation,JavaScript對象表示法格式。
  • XML:eXtensible Markup Language,可擴展標(biāo)記語言格式。

結(jié)論:數(shù)據(jù)導(dǎo)入導(dǎo)出是指將數(shù)據(jù)從一個系統(tǒng)導(dǎo)入到另一個系統(tǒng),或從一個系統(tǒng)導(dǎo)出到另一個系統(tǒng)的過程,作用是實現(xiàn)數(shù)據(jù)的遷移、備份、共享。

數(shù)據(jù)導(dǎo)入導(dǎo)出的特點

定義:數(shù)據(jù)導(dǎo)入導(dǎo)出的特點是指數(shù)據(jù)導(dǎo)入導(dǎo)出的特性。

特點

  • 易用性:數(shù)據(jù)導(dǎo)入導(dǎo)出提供易用的編程模型。
  • 高效性:數(shù)據(jù)導(dǎo)入導(dǎo)出提供高效的數(shù)據(jù)處理。
  • 可擴展性:數(shù)據(jù)導(dǎo)入導(dǎo)出可以擴展到多個應(yīng)用程序之間的數(shù)據(jù)通信。
  • 可靠性:數(shù)據(jù)導(dǎo)入導(dǎo)出提供可靠的數(shù)據(jù)傳輸。

結(jié)論:數(shù)據(jù)導(dǎo)入導(dǎo)出的特點包括易用性、高效性、可擴展性、可靠性。

Spring Boot與數(shù)據(jù)導(dǎo)入導(dǎo)出的集成

Spring Boot與數(shù)據(jù)導(dǎo)入導(dǎo)出的集成是Java開發(fā)中的重要內(nèi)容。

集成Apache POI的步驟

定義:集成Apache POI的步驟是指使用Spring Boot與Apache POI集成的方法。

步驟

  1. 創(chuàng)建Spring Boot項目。
  2. 添加所需的依賴。
  3. 創(chuàng)建數(shù)據(jù)訪問層。
  4. 創(chuàng)建業(yè)務(wù)層。
  5. 創(chuàng)建控制器類。
  6. 測試應(yīng)用。

示例:pom.xml文件中的依賴:

<dependencies>
    <!-- Web依賴 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Data JPA依賴 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <!-- H2數(shù)據(jù)庫依賴 -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- Apache POI依賴 -->
    <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>
    <!-- 測試依賴 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

application.properties文件中的配置:

# 服務(wù)器端口
server.port=8080
# 數(shù)據(jù)庫連接信息
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
# JPA配置
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
# H2數(shù)據(jù)庫控制臺
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

實體類:

import javax.persistence.*;

@Entity
@Table(name = "product")
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String productId;
    private String productName;
    private double price;
    private int sales;
    
    public Product() {
    }
    
    public Product(String productId, String productName, double price, int sales) {
        this.productId = productId;
        this.productName = productName;
        this.price = price;
        this.sales = sales;
    }
    
    // Getter和Setter方法
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getProductId() {
        return productId;
    }
    
    public void setProductId(String productId) {
        this.productId = productId;
    }
    
    public String getProductName() {
        return productName;
    }
    
    public void setProductName(String productName) {
        this.productName = productName;
    }
    
    public double getPrice() {
        return price;
    }
    
    public void setPrice(double price) {
        this.price = price;
    }
    
    public int getSales() {
        return sales;
    }
    
    public void setSales(int sales) {
        this.sales = sales;
    }
    
    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", productId='" + productId + '\'' +
                ", productName='" + productName + '\'' +
                ", price=" + price +
                ", sales=" + sales +
                '}';
    }
}

Repository接口:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
}

Service類:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class ProductService {
    @Autowired
    private ProductRepository productRepository;
    
    @Transactional
    public void importProducts(MultipartFile file) throws IOException {
        List<Product> products = new ArrayList<>();
        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        Sheet sheet = workbook.getSheetAt(0);
        Iterator<Row> iterator = sheet.iterator();
        
        if (iterator.hasNext()) {
            iterator.next(); // 跳過標(biāo)題行
        }
        
        while (iterator.hasNext()) {
            Row currentRow = iterator.next();
            Product product = new Product();
            product.setProductId(currentRow.getCell(0).getStringCellValue());
            product.setProductName(currentRow.getCell(1).getStringCellValue());
            product.setPrice(currentRow.getCell(2).getNumericCellValue());
            product.setSales((int) currentRow.getCell(3).getNumericCellValue());
            products.add(product);
        }
        
        productRepository.saveAll(products);
        workbook.close();
    }
    
    @Transactional(readOnly = true)
    public byte[] exportProducts() throws IOException {
        List<Product> products = productRepository.findAll();
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Products");
        Row headerRow = sheet.createRow(0);
        
        Cell headerCell0 = headerRow.createCell(0);
        headerCell0.setCellValue("Product ID");
        
        Cell headerCell1 = headerRow.createCell(1);
        headerCell1.setCellValue("Product Name");
        
        Cell headerCell2 = headerRow.createCell(2);
        headerCell2.setCellValue("Price");
        
        Cell headerCell3 = headerRow.createCell(3);
        headerCell3.setCellValue("Sales");
        
        int rowNum = 1;
        for (Product product : products) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(product.getProductId());
            row.createCell(1).setCellValue(product.getProductName());
            row.createCell(2).setCellValue(product.getPrice());
            row.createCell(3).setCellValue(product.getSales());
        }
        
        for (int i = 0; i < 4; i++) {
            sheet.autoSizeColumn(i);
        }
        
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();
        
        return outputStream.toByteArray();
    }
    
    @Transactional
    public Product addProduct(Product product) {
        return productRepository.save(product);
    }
    
    @Transactional(readOnly = true)
    public List<Product> getAllProducts() {
        return productRepository.findAll();
    }
}

控制器類:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/products")
public class ProductController {
    @Autowired
    private ProductService productService;
    
    @PostMapping("/import")
    public ResponseEntity<String> importProducts(@RequestParam("file") MultipartFile file) {
        try {
            productService.importProducts(file);
            return ResponseEntity.ok("數(shù)據(jù)導(dǎo)入成功");
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("數(shù)據(jù)導(dǎo)入失?。? + e.getMessage());
        }
    }
    
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportProducts() {
        try {
            byte[] bytes = productService.exportProducts();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
            headers.setContentDispositionFormData("attachment", "products.xlsx");
            headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
            return ResponseEntity.ok().headers(headers).body(bytes);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(null);
        }
    }
    
    @PostMapping("/")
    public Product addProduct(@RequestBody Product product) {
        return productService.addProduct(product);
    }
    
    @GetMapping("/")
    public List<Product> getAllProducts() {
        return productService.getAllProducts();
    }
}

應(yīng)用啟動類:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductApplication.class, args);
    }
    
    @Autowired
    private ProductService productService;
    
    public void run(String... args) {
        // 初始化數(shù)據(jù)
        productService.addProduct(new Product("P001", "手機", 1000.0, 100));
        productService.addProduct(new Product("P002", "電腦", 5000.0, 50));
        productService.addProduct(new Product("P003", "電視", 3000.0, 80));
        productService.addProduct(new Product("P004", "手表", 500.0, 200));
        productService.addProduct(new Product("P005", "耳機", 300.0, 150));
    }
}

測試類:

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ProductApplicationTests {
    @LocalServerPort
    private int port;
    
    @Autowired
    private TestRestTemplate restTemplate;
    
    @Test
    void contextLoads() {
    }
    
    @Test
    void testAddProduct() {
        Product product = new Product("P006", "平板", 2000.0, 70);
        Product savedProduct = restTemplate.postForObject("http://localhost:" + port + "/api/products/", product, Product.class);
        assertThat(savedProduct).isNotNull();
        assertThat(savedProduct.getProductId()).isEqualTo("P006");
    }
    
    @Test
    void testGetAllProducts() {
        List<Product> products = restTemplate.getForObject("http://localhost:" + port + "/api/products/", List.class);
        assertThat(products).isNotNull();
        assertThat(products.size()).isGreaterThanOrEqualTo(5);
    }
    
    @Test
    void testImportProducts() throws IOException {
        File file = new File("src/test/resources/products.xlsx");
        InputStream inputStream = new FileInputStream(file);
        MultipartFile multipartFile = new CommonsMultipartFile(inputStream);
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", multipartFile);
        
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
        
        ResponseEntity<String> response = restTemplate.exchange("http://localhost:" + port + "/api/products/import", HttpMethod.POST, requestEntity, String.class);
        
        assertThat(response.getStatusCodeValue()).isEqualTo(200);
        assertThat(response.getBody()).contains("數(shù)據(jù)導(dǎo)入成功");
    }
    
    @Test
    void testExportProducts() {
        ResponseEntity<byte[]> response = restTemplate.getForEntity("http://localhost:" + port + "/api/products/export", byte[].class);
        assertThat(response.getStatusCodeValue()).isEqualTo(200);
        assertThat(response.getBody()).isNotNull();
        assertThat(response.getBody().length).isGreaterThan(0);
    }
}

結(jié)論:集成Apache POI的步驟包括創(chuàng)建Spring Boot項目、添加所需的依賴、創(chuàng)建數(shù)據(jù)訪問層、創(chuàng)建業(yè)務(wù)層、創(chuàng)建控制器類、測試應(yīng)用。

Spring Boot與報表生成的基本方法

Spring Boot與報表生成的基本方法包括使用Apache POI、使用JasperReports、使用Excel模板。

使用JasperReports

定義:使用JasperReports是指Spring Boot與報表生成集成的基本方法之一。

作用

  • 實現(xiàn)報表生成。
  • 提高應(yīng)用程序的性能。

示例:pom.xml文件中的依賴:

<dependencies>
    <!-- Web依賴 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Data JPA依賴 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <!-- H2數(shù)據(jù)庫依賴 -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- JasperReports依賴 -->
    <dependency>
        <groupId>net.sf.jasperreports</groupId>
        <artifactId>jasperreports</artifactId>
        <version>6.17.0</version>
    </dependency>
    <dependency>
        <groupId>net.sf.jasperreports</groupId>
        <artifactId>jasperreports-fonts</artifactId>
        <version>6.17.0</version>
    </dependency>
    <!-- 測試依賴 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Service類:

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportService {
    @Autowired
    private ProductRepository productRepository;
    
    @Transactional(readOnly = true)
    public byte[] generateProductReport() throws JRException {
        List<Product> products = productRepository.findAll();
        
        // 加載Jasper模板
        InputStream templateStream = getClass().getResourceAsStream("/reports/product-report.jasper");
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(templateStream);
        
        // 設(shè)置參數(shù)
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("Title", "產(chǎn)品銷售報表");
        
        // 設(shè)置數(shù)據(jù)源
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(products);
        
        // 生成報表
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
        
        // 導(dǎo)出報表為PDF
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
        
        return outputStream.toByteArray();
    }
}

控制器類:

import net.sf.jasperreports.engine.JRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/products")
public class ProductController {
    @Autowired
    private ProductService productService;
    
    @Autowired
    private ReportService reportService;
    
    @GetMapping("/report")
    public ResponseEntity<byte[]> generateProductReport() {
        try {
            byte[] bytes = reportService.generateProductReport();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.parseMediaType("application/pdf"));
            headers.setContentDispositionFormData("attachment", "product-report.pdf");
            headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
            return ResponseEntity.ok().headers(headers).body(bytes);
        } catch (JRException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(null);
        }
    }
    
    @PostMapping("/import")
    public ResponseEntity<String> importProducts(@RequestParam("file") MultipartFile file) {
        try {
            productService.importProducts(file);
            return ResponseEntity.ok("數(shù)據(jù)導(dǎo)入成功");
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("數(shù)據(jù)導(dǎo)入失?。? + e.getMessage());
        }
    }
    
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportProducts() {
        try {
            byte[] bytes = productService.exportProducts();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
            headers.setContentDispositionFormData("attachment", "products.xlsx");
            headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
            return ResponseEntity.ok().headers(headers).body(bytes);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(null);
        }
    }
    
    @PostMapping("/")
    public Product addProduct(@RequestBody Product product) {
        return productService.addProduct(product);
    }
    
    @GetMapping("/")
    public List<Product> getAllProducts() {
        return productService.getAllProducts();
    }
}

測試類:

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ProductApplicationTests {
    @LocalServerPort
    private int port;
    
    @Autowired
    private TestRestTemplate restTemplate;
    
    @Test
    void contextLoads() {
    }
    
    @Test
    void testAddProduct() {
        Product product = new Product("P006", "平板", 2000.0, 70);
        Product savedProduct = restTemplate.postForObject("http://localhost:" + port + "/api/products/", product, Product.class);
        assertThat(savedProduct).isNotNull();
        assertThat(savedProduct.getProductId()).isEqualTo("P006");
    }
    
    @Test
    void testGetAllProducts() {
        List<Product> products = restTemplate.getForObject("http://localhost:" + port + "/api/products/", List.class);
        assertThat(products).isNotNull();
        assertThat(products.size()).isGreaterThanOrEqualTo(5);
    }
    
    @Test
    void testImportProducts() throws IOException {
        File file = new File("src/test/resources/products.xlsx");
        InputStream inputStream = new FileInputStream(file);
        MultipartFile multipartFile = new CommonsMultipartFile(inputStream);
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", multipartFile);
        
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
        
        ResponseEntity<String> response = restTemplate.exchange("http://localhost:" + port + "/api/products/import", HttpMethod.POST, requestEntity, String.class);
        
        assertThat(response.getStatusCodeValue()).isEqualTo(200);
        assertThat(response.getBody()).contains("數(shù)據(jù)導(dǎo)入成功");
    }
    
    @Test
    void testExportProducts() {
        ResponseEntity<byte[]> response = restTemplate.getForEntity("http://localhost:" + port + "/api/products/export", byte[].class);
        assertThat(response.getStatusCodeValue()).isEqualTo(200);
        assertThat(response.getBody()).isNotNull();
        assertThat(response.getBody().length).isGreaterThan(0);
    }
    
    @Test
    void testGenerateProductReport() {
        ResponseEntity<byte[]> response = restTemplate.getForEntity("http://localhost:" + port + "/api/products/report", byte[].class);
        assertThat(response.getStatusCodeValue()).isEqualTo(200);
        assertThat(response.getBody()).isNotNull();
        assertThat(response.getBody().length).isGreaterThan(0);
    }
}

結(jié)論:使用JasperReports是指Spring Boot與報表生成集成的基本方法之一,作用是實現(xiàn)報表生成、提高應(yīng)用程序的性能。

Spring Boot的實際應(yīng)用場景

在實際開發(fā)中,Spring Boot數(shù)據(jù)導(dǎo)入導(dǎo)出與報表生成的應(yīng)用場景非常廣泛,如:

  • 實現(xiàn)產(chǎn)品信息的導(dǎo)入導(dǎo)出。
  • 實現(xiàn)用戶信息的導(dǎo)入導(dǎo)出。
  • 實現(xiàn)訂單信息的導(dǎo)入導(dǎo)出。
  • 實現(xiàn)產(chǎn)品銷售報表的生成。

示例

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductApplication.class, args);
    }
    
    @Autowired
    private ProductService productService;
    
    public void run(String... args) {
        // 初始化數(shù)據(jù)
        productService.addProduct(new Product("P001", "手機", 1000.0, 100));
        productService.addProduct(new Product("P002", "電腦", 5000.0, 50));
        productService.addProduct(new Product("P003", "電視", 3000.0, 80));
        productService.addProduct(new Product("P004", "手表", 500.0, 200));
        productService.addProduct(new Product("P005", "耳機", 300.0, 150));
    }
}

@Service
class ProductService {
    @Autowired
    private ProductRepository productRepository;
    
    @Transactional
    public void importProducts(MultipartFile file) throws IOException {
        List<Product> products = new ArrayList<>();
        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        Sheet sheet = workbook.getSheetAt(0);
        Iterator<Row> iterator = sheet.iterator();
        
        if (iterator.hasNext()) {
            iterator.next(); // 跳過標(biāo)題行
        }
        
        while (iterator.hasNext()) {
            Row currentRow = iterator.next();
            Product product = new Product();
            product.setProductId(currentRow.getCell(0).getStringCellValue());
            product.setProductName(currentRow.getCell(1).getStringCellValue());
            product.setPrice(currentRow.getCell(2).getNumericCellValue());
            product.setSales((int) currentRow.getCell(3).getNumericCellValue());
            products.add(product);
        }
        
        productRepository.saveAll(products);
        workbook.close();
    }
    
    @Transactional(readOnly = true)
    public byte[] exportProducts() throws IOException {
        List<Product> products = productRepository.findAll();
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Products");
        Row headerRow = sheet.createRow(0);
        
        Cell headerCell0 = headerRow.createCell(0);
        headerCell0.setCellValue("Product ID");
        
        Cell headerCell1 = headerRow.createCell(1);
        headerCell1.setCellValue("Product Name");
        
        Cell headerCell2 = headerRow.createCell(2);
        headerCell2.setCellValue("Price");
        
        Cell headerCell3 = headerRow.createCell(3);
        headerCell3.setCellValue("Sales");
        
        int rowNum = 1;
        for (Product product : products) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(product.getProductId());
            row.createCell(1).setCellValue(product.getProductName());
            row.createCell(2).setCellValue(product.getPrice());
            row.createCell(3).setCellValue(product.getSales());
        }
        
        for (int i = 0; i < 4; i++) {
            sheet.autoSizeColumn(i);
        }
        
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();
        
        return outputStream.toByteArray();
    }
    
    @Transactional
    public Product addProduct(Product product) {
        return productRepository.save(product);
    }
    
    @Transactional(readOnly = true)
    public List<Product> getAllProducts() {
        return productRepository.findAll();
    }
}

@RestController
@RequestMapping("/api/products")
class ProductController {
    @Autowired
    private ProductService productService;
    
    @Autowired
    private ReportService reportService;
    
    @GetMapping("/report")
    public ResponseEntity<byte[]> generateProductReport() {
        try {
            byte[] bytes = reportService.generateProductReport();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.parseMediaType("application/pdf"));
            headers.setContentDispositionFormData("attachment", "product-report.pdf");
            headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
            return ResponseEntity.ok().headers(headers).body(bytes);
        } catch (JRException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(null);
        }
    }
    
    @PostMapping("/import")
    public ResponseEntity<String> importProducts(@RequestParam("file") MultipartFile file) {
        try {
            productService.importProducts(file);
            return ResponseEntity.ok("數(shù)據(jù)導(dǎo)入成功");
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("數(shù)據(jù)導(dǎo)入失?。? + e.getMessage());
        }
    }
    
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportProducts() {
        try {
            byte[] bytes = productService.exportProducts();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
            headers.setContentDispositionFormData("attachment", "products.xlsx");
            headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
            return ResponseEntity.ok().headers(headers).body(bytes);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(null);
        }
    }
    
    @PostMapping("/")
    public Product addProduct(@RequestBody Product product) {
        return productService.addProduct(product);
    }
    
    @GetMapping("/")
    public List<Product> getAllProducts() {
        return productService.getAllProducts();
    }
}

// 測試類
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ProductApplicationTests {
    @LocalServerPort
    private int port;
    
    @Autowired
    private TestRestTemplate restTemplate;
    
    @Test
    void contextLoads() {
    }
    
    @Test
    void testAddProduct() {
        Product product = new Product("P006", "平板", 2000.0, 70);
        Product savedProduct = restTemplate.postForObject("http://localhost:" + port + "/api/products/", product, Product.class);
        assertThat(savedProduct).isNotNull();
        assertThat(savedProduct.getProductId()).isEqualTo("P006");
    }
    
    @Test
    void testGetAllProducts() {
        List<Product> products = restTemplate.getForObject("http://localhost:" + port + "/api/products/", List.class);
        assertThat(products).isNotNull();
        assertThat(products.size()).isGreaterThanOrEqualTo(5);
    }
    
    @Test
    void testImportProducts() throws IOException {
        File file = new File("src/test/resources/products.xlsx");
        InputStream inputStream = new FileInputStream(file);
        MultipartFile multipartFile = new CommonsMultipartFile(inputStream);
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", multipartFile);
        
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
        
        ResponseEntity<String> response = restTemplate.exchange("http://localhost:" + port + "/api/products/import", HttpMethod.POST, requestEntity, String.class);
        
        assertThat(response.getStatusCodeValue()).isEqualTo(200);
        assertThat(response.getBody()).contains("數(shù)據(jù)導(dǎo)入成功");
    }
    
    @Test
    void testExportProducts() {
        ResponseEntity<byte[]> response = restTemplate.getForEntity("http://localhost:" + port + "/api/products/export", byte[].class);
        assertThat(response.getStatusCodeValue()).isEqualTo(200);
        assertThat(response.getBody()).isNotNull();
        assertThat(response.getBody().length).isGreaterThan(0);
    }
    
    @Test
    void testGenerateProductReport() {
        ResponseEntity<byte[]> response = restTemplate.getForEntity("http://localhost:" + port + "/api/products/report", byte[].class);
        assertThat(response.getStatusCodeValue()).isEqualTo(200);
        assertThat(response.getBody()).isNotNull();
        assertThat(response.getBody().length).isGreaterThan(0);
    }
}

輸出結(jié)果

訪問http://localhost:8080/api/products/report:下載產(chǎn)品銷售報表(PDF格式)。
訪問http://localhost:8080/api/products/import:上傳產(chǎn)品信息Excel文件,導(dǎo)入數(shù)據(jù)。
訪問http://localhost:8080/api/products/export:下載產(chǎn)品信息Excel文件。

結(jié)論:在實際開發(fā)中,Spring Boot數(shù)據(jù)導(dǎo)入導(dǎo)出與報表生成的應(yīng)用場景非常廣泛,需要根據(jù)實際問題選擇合適的導(dǎo)入導(dǎo)出格式和報表生成工具。

總結(jié)

本章我們學(xué)習(xí)了Spring Boot數(shù)據(jù)導(dǎo)入導(dǎo)出與報表生成,包括數(shù)據(jù)導(dǎo)入導(dǎo)出的定義與特點、Spring Boot與數(shù)據(jù)導(dǎo)入導(dǎo)出的集成、Spring Boot與數(shù)據(jù)導(dǎo)入導(dǎo)出的配置、Spring Boot與報表生成的基本方法、Spring Boot的實際應(yīng)用場景,學(xué)會了在實際開發(fā)中處理數(shù)據(jù)導(dǎo)入導(dǎo)出與報表生成問題。其中,數(shù)據(jù)導(dǎo)入導(dǎo)出的定義與特點、Spring Boot與數(shù)據(jù)導(dǎo)入導(dǎo)出的集成、Spring Boot與數(shù)據(jù)導(dǎo)入導(dǎo)出的配置、Spring Boot與報表生成的基本方法、Spring Boot的實際應(yīng)用場景是本章的重點內(nèi)容。

以上就是SpringBoot實現(xiàn)數(shù)據(jù)導(dǎo)入導(dǎo)出與報表生成的完整教程的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot數(shù)據(jù)導(dǎo)入導(dǎo)出的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java 如何繞過迭代器遍歷時的數(shù)據(jù)修改異常

    Java 如何繞過迭代器遍歷時的數(shù)據(jù)修改異常

    這篇文章主要介紹了Java 繞過迭代器遍歷時的數(shù)據(jù)修改異常的方法,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-02-02
  • IDEA切換JDK版本超詳細(xì)操作步驟記錄

    IDEA切換JDK版本超詳細(xì)操作步驟記錄

    在我們項目開發(fā)的過程中可能會遇到JDK版本過高或者過低導(dǎo)致一些程序無法啟動,不兼容的問題,所以我們需要切換JDK的版本號,這篇文章主要給大家介紹了關(guān)于IDEA切換JDK版本的超詳細(xì)操作步驟,需要的朋友可以參考下
    2024-03-03
  • 詳解Java 中泛型的實現(xiàn)原理

    詳解Java 中泛型的實現(xiàn)原理

    這篇文章主要介紹了詳解Java 中泛型的實現(xiàn)原理,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-03-03
  • Java數(shù)據(jù)結(jié)構(gòu)之圖的兩種搜索算法詳解

    Java數(shù)據(jù)結(jié)構(gòu)之圖的兩種搜索算法詳解

    在很多情況下,我們需要遍歷圖,得到圖的一些性質(zhì)。有關(guān)圖的搜索,最經(jīng)典的算法有深度優(yōu)先搜索和廣度優(yōu)先搜索,接下來我們分別講解這兩種搜索算法,需要的可以參考一下
    2022-11-11
  • 關(guān)于Java語法糖以及語法糖的原理和用法

    關(guān)于Java語法糖以及語法糖的原理和用法

    這篇文章主要介紹了關(guān)于Java什么是語法糖以及語法糖的種類,也稱糖衣語法,是由英國計算機學(xué)家?Peter.J.Landin?發(fā)明的一個術(shù)語,指在計算機語言中添加的某種語法,這種語法對語言的功能并沒有影響,但是更方便程序員使用,需要的朋友可以參考下
    2023-05-05
  • SpringRunner和SpringJUnit4ClassRunner的區(qū)別及說明

    SpringRunner和SpringJUnit4ClassRunner的區(qū)別及說明

    這篇文章主要介紹了SpringRunner和SpringJUnit4ClassRunner的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Java高頻面試題之海量數(shù)據(jù)處理分析

    Java高頻面試題之海量數(shù)據(jù)處理分析

    海量信息處理日益成為當(dāng)前程序員筆試面試中一個新的亮點。硬件擴容是難滿足海量數(shù)據(jù)處理需要的,如何利用現(xiàn)有條件進行海量信息處理?本文就來為大家解答一下
    2022-10-10
  • 基于TCP通信丟包原因總結(jié)(推薦)

    基于TCP通信丟包原因總結(jié)(推薦)

    下面小編就為大家?guī)硪黄赥CP通信丟包原因總結(jié)(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 詳解如何在Spring中為@Value注解設(shè)置默認(rèn)值

    詳解如何在Spring中為@Value注解設(shè)置默認(rèn)值

    在Spring開發(fā)中,我們經(jīng)常會遇到需要從配置文件中讀取屬性的情況,@Value注解是Spring提供的一種便捷方式,能夠讓我們輕松地將配置文件中的屬性注入到Spring Bean中,
    2024-10-10
  • SpringMVC結(jié)合Jcrop實現(xiàn)圖片裁剪

    SpringMVC結(jié)合Jcrop實現(xiàn)圖片裁剪

    這篇文章主要介紹了SpringMVC結(jié)合Jcrop實現(xiàn)圖片裁剪的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12

最新評論

磐石市| 格尔木市| 新巴尔虎左旗| 阿巴嘎旗| 武陟县| 日土县| 海盐县| 德江县| 乃东县| 黑龙江省| 疏附县| 启东市| 固阳县| 浑源县| 北宁市| 霍州市| 乐都县| 哈密市| 泾川县| 祁东县| 望奎县| 五台县| 乌兰县| 长沙县| 余江县| 尚义县| 潜江市| 吴桥县| 宜宾县| 鹤岗市| 惠水县| 镇赉县| 河池市| 元氏县| 布拖县| 章丘市| 汾阳市| 临澧县| 白沙| 金溪县| 和龙市|