第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样例:

相关推荐
lkbhua莱克瓦2416 小时前
基础-事务
开发语言·数据库·笔记·mysql·事务
luoluoal16 小时前
基于python的自然语言处理技术的话题文本分类的研究(源码+文档)
python·mysql·django·毕业设计·源码
weixin_4365250716 小时前
NestJS-TypeORM QueryBuilder 常用 SQL 写法
java·数据库·sql
Cosolar16 小时前
MySQL EXPLAIN 执行计划分析:能否查看 JOIN 关联顺序
数据库·后端·mysql
micromicrofat16 小时前
【MongoDB】WSL2访问宿主机的MongoDB
数据库·mongodb
两拆16 小时前
Redhat7.9安装部署Oracle 19C
数据库·oracle
TDengine (老段)17 小时前
TDengine 企业用户建表规模有多大?
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
FIT2CLOUD飞致云17 小时前
操作教程丨通过1Panel轻松安装和管理MySQL开源数据库
linux·运维·服务器·mysql·开源·1panel
byzh_rc17 小时前
[算法设计与分析-从入门到入土] 复杂算法
数据库·人工智能·算法·机器学习·支持向量机
白露与泡影18 小时前
详细描述一条 SQL 语句在 MySQL 中的执行过程。
数据库·sql·mysql