第2篇:Mysql数据库表结构导出字段到Excel(多个sheet中)

java 复制代码
package com.rlcloud.risk.util;


import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Description : 连接Mysql,表结构导出到Excel文件中
 * @date 2024/3/26 10:47
 * @return
 * @auther xushuanglu
 */
public class MySQLGetTable {

    public static void main(String[] args) {

        String url = "jdbc:mysql://localhost:3306";
        String user = "root";
        String password = "123456";

        try {
            // 连接数据库
            Connection conn = DriverManager.getConnection(url, user, password);

            // 获取数据库元数据
            DatabaseMetaData meta = conn.getMetaData();

            // 从元数据中获取表信息
            ResultSet tables = meta.getTables("databaseName", null, "%", new String[] {"TABLE"});

            // 连接数据库
            Connection conn1 = DriverManager.getConnection(url, user, password);
            DatabaseMetaData metaData = conn1.getMetaData();


            // 创建Excel工作簿和工作表
            Workbook workbook = new XSSFWorkbook();

            // 遍历所有表
            while (tables.next()) {
                //第一步获取数据库表名
                String tableName = tables.getString("TABLE_NAME");
                System.out.println("Table Name: " + tableName);

                //一张表
                ResultSet resultSet = metaData.getColumns("databaseName", null, tableName, "%");

                //创建sheet
                Sheet sheet = workbook.createSheet(tableName);

                // 写入表头
                Row header = sheet.createRow(0);
                header.createCell(0).setCellValue("Name");
                header.createCell(1).setCellValue("Datatype");
                header.createCell(2).setCellValue("Comment");
                header.createCell(3).setCellValue("Null Option");
                header.createCell(4).setCellValue("Is PK");
                header.createCell(5).setCellValue("Is FK");
                header.createCell(5).setCellValue("tableName");

                int rowNum = 1;
                while (resultSet.next()) {

                    // 读取字段名和类型
                    String columnName = resultSet.getString("COLUMN_NAME");
                    String typeName = resultSet.getString("TYPE_NAME");
                    String columnSize = resultSet.getString("COLUMN_SIZE");
                    String remarks = resultSet.getString("REMARKS");
                    String tableName1 = resultSet.getString("TABLE_NAME");

                    // 写入数据到Excel
                    Row row = sheet.createRow(rowNum);
                    row.createCell(0).setCellValue(columnName);
                    row.createCell(1).setCellValue(typeName + "(" + columnSize + ")");
                    row.createCell(2).setCellValue(remarks);
                    row.createCell(3).setCellValue("NULL");
                    row.createCell(4).setCellValue("NO");
                    row.createCell(5).setCellValue("NO");
                    row.createCell(5).setCellValue(tableName1);

                    rowNum++;
                }

                resultSet.close();
            }

            // 关闭连接
            conn.close();

            // 关闭连接
            conn1.close();

            // 写入Excel文件
            FileOutputStream out = new FileOutputStream("exportExcel.xlsx");
            workbook.write(out);
            out.close();
            System.out.println("Excel文件已生成!");

        } catch (SQLException | FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

导出样例图:

相关推荐
2603_965148116 小时前
如何解析JSON数据?API返回的商品信息处理教程
开发语言·数据库·python·自动化·json·api
ltl6 小时前
RocksDB 经典故障排查:L0、compaction 与 write stall
数据库
xfhuangfu7 小时前
Oracle中建立到CDB和PDB的连接
数据库·oracle·rpc
小小龙学IT7 小时前
DuckDB 深度实战:用 C++ 在进程内跑一个「分析型数据库」
数据库·c++
NineData7 小时前
DTCC 2026 预告|NineData CEO& 创始人叶正盛:面向 AI Agent 的数据库 DevOps 与数据复制实践
数据库·人工智能·数据库开发·devops·ninedata·数据库技术·dtcc
J_bean8 小时前
MySQL 事务是否必须手动开启?
数据库·mysql·数据库事务·自动提交·手动提交
J_bean8 小时前
MySQL InnoDB 如何检测死锁、判定死锁、处理死锁
数据库·mysql·死锁·死锁检测·处理死锁·死锁判定
浪子明X9 小时前
从 MongoDB 文档到关系模型:构建可重跑、可对账的数据迁移流水线
数据库·mongodb·oracle
杜子不疼.9 小时前
国产数据库撑起固井软件自主化:金仓 × 中海油服「海恒 Cemsol」落地解析
数据库
隔窗听雨眠9 小时前
GBase 8s并发控制深度解析:封锁机制、隔离级别与死锁处理全攻略
服务器·数据库·oracle