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

java 复制代码
package com.xx.util;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.sql.*;
import java.io.*;

public class DatabaseToExcel {

    public static void main(String[] args) throws Exception {

        // 数据库连接配置
        String url = "jdbc:mysql://localhost:3306";
        String user = "root";
        String password = "root";
        String tableName = "table_name";
        String databases = "database_name";

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

        // 获取表字段信息
        /**参数
        catalog
        一个包含目录名称的字符串 。
        架构
        一个包含架构名称模式的字符串 。
        table
        一个包含表名称模式的字符串。
        col
        一个包含列名称模式的字符串。
         */
        ResultSet resultSet = metaData.getColumns(databases, null, null, "%");


        // 创建Excel工作簿和工作表
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("TableInfo");

        // 写入表头
        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();

        // 写入Excel文件
        FileOutputStream out = new FileOutputStream("TableInfo.xlsx");
        workbook.write(out);
        out.close();

        System.out.println("Excel文件已生成!");
    }
}

导出Excel样例:

相关推荐
凌虚7 小时前
面向 MySQL 用户的 PostgreSQL 快速上手指南
数据库·后端·架构
五阿哥永琪7 小时前
MySQL中操作json的函数!
数据库·mysql·json
来者皆善7 小时前
了解Mysql优化吗?如何优化索引?
数据库·mysql
赤壁小虾9 小时前
【渲染流水线】[逐片元阶段]-[透明度测试]以UnityURP为例
java·前端·数据库
MC皮蛋侠客9 小时前
SQLAlchemy 系列(七):高级建模与高效写入——批量 DML、方言与扩展
数据库·python
奈斯先生Vector10 小时前
把 Midjourney 二次编辑做成生产系统:customId 能力令牌、Action Graph 与 WebUI 精修工作台
数据库·人工智能·架构·aigc·音视频·midjourney
Lethehong11 小时前
双擎并驱·全链路并行:KFS让TB级异构增量同步秒级到达
数据库
MC皮蛋侠客11 小时前
SQLAlchemy 系列(八):AsyncIO、并发与 Web 生命周期——让每个并发任务持有自己的 Session
数据库·python
一水11 小时前
Redis实战:一个AI工作流系统里的五个应用场景,从传参到限流的完整链路
数据库·redis·wpf
Ai拆代码的曹操11 小时前
MySQL GROUP BY 性能优化:执行计划分析 + 索引重构实战
mysql·性能优化·重构