首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

使用spring boot将excel文件中的数据插入mysql

使用Spring Boot将Excel文件中的数据插入MySQL可以通过以下步骤实现:

  1. 首先,需要在Spring Boot项目中添加相关依赖。可以使用Maven或Gradle构建工具,在项目的pom.xml(或build.gradle)文件中添加以下依赖:
代码语言:txt
复制
<!-- Spring Boot Web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Apache POI for Excel manipulation -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>

<!-- MySQL Connector -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
  1. 创建一个Controller类,用于处理HTTP请求和Excel数据插入操作。可以使用@RestController注解标记该类,并使用@PostMapping注解标记处理POST请求的方法。
代码语言:txt
复制
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ExcelController {

    @PostMapping("/insertData")
    public String insertData(@RequestBody byte[] excelData) {
        // TODO: 解析Excel文件并将数据插入MySQL
        return "Data inserted successfully!";
    }
}
  1. insertData方法中,可以使用Apache POI库来解析Excel文件并将数据插入MySQL。可以使用HSSFWorkbookXSSFWorkbook类来读取Excel文件,根据文件格式选择适当的类。
代码语言:txt
复制
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@RestController
public class ExcelController {

    @PostMapping("/insertData")
    public String insertData(@RequestBody byte[] excelData) {
        try {
            // 将字节数组转换为输入流
            ByteArrayInputStream inputStream = new ByteArrayInputStream(excelData);

            // 创建工作簿对象
            Workbook workbook = new XSSFWorkbook(inputStream);

            // 获取第一个工作表
            Sheet sheet = workbook.getSheetAt(0);

            // 遍历行
            for (Row row : sheet) {
                // 读取每行的数据并插入MySQL
                String column1 = row.getCell(0).getStringCellValue();
                int column2 = (int) row.getCell(1).getNumericCellValue();

                insertRowDataIntoMySQL(column1, column2);
            }

            // 关闭工作簿和输入流
            workbook.close();
            inputStream.close();

            return "Data inserted successfully!";
        } catch (IOException | SQLException e) {
            e.printStackTrace();
            return "Error occurred while inserting data.";
        }
    }

    private void insertRowDataIntoMySQL(String column1, int column2) throws SQLException {
        // 连接MySQL数据库
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");

        // 准备插入数据的SQL语句
        String sql = "INSERT INTO mytable (column1, column2) VALUES (?, ?)";

        // 创建预编译语句对象
        PreparedStatement statement = connection.prepareStatement(sql);

        // 设置参数值
        statement.setString(1, column1);
        statement.setInt(2, column2);

        // 执行插入操作
        statement.executeUpdate();

        // 关闭连接和语句对象
        statement.close();
        connection.close();
    }
}

以上代码示例假设MySQL数据库位于本地主机(localhost),端口为3306,数据库名为mydatabase,用户名和密码需要根据实际情况进行修改。

  1. 启动Spring Boot应用程序,并使用HTTP POST请求将Excel文件的字节数组发送到/insertData端点。可以使用Postman或类似的工具进行测试。

注意:以上代码仅提供了基本的示例,实际应用中可能需要进行错误处理、数据验证和其他优化。另外,为了保证安全性,建议在实际生产环境中使用连接池管理数据库连接。

推荐的腾讯云相关产品和产品介绍链接地址:

  • 腾讯云数据库MySQL:https://cloud.tencent.com/product/cdb_mysql
  • 腾讯云云服务器(CVM):https://cloud.tencent.com/product/cvm
  • 腾讯云对象存储(COS):https://cloud.tencent.com/product/cos
  • 腾讯云云函数(SCF):https://cloud.tencent.com/product/scf
  • 腾讯云API网关(API Gateway):https://cloud.tencent.com/product/apigateway
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

6分56秒

使用python将excel与mysql数据导入导出

7分27秒

第十八章:Class文件结构/10-字节码数据保存到excel中的操作

7分14秒

Go 语言读写 Excel 文档

1.2K
7分5秒

MySQL数据闪回工具reverse_sql

4分11秒

05、mysql系列之命令、快捷窗口的使用

1分32秒

最新数码印刷-数字印刷-个性化印刷工作流程-教程

2分29秒

MySQL系列七之任务1【导入SQL文件,生成表格数据】

38秒

Lightroom Classic教程:如何在Mac Lightroom 中创建黑色电影效果

2分13秒

MySQL系列十之【监控管理】

16分8秒

Tspider分库分表的部署 - MySQL

3分59秒

06、mysql系列之模板窗口和平铺窗口的应用

18分41秒

041.go的结构体的json序列化

领券