poi导出表结构以及字段注释生成excel -----------mysql数据库

poi导出表结构以及字段注释生成excel -----------mysql数据库

我们在写数据库文档时,经常需要导出数据库结构和注释,我写了一个简单版本,大家拿来就可以用。不能用请打死我,不过是mysql数据库的。费话不多少,上菜。
一、依赖

						<dependency>
							    <groupId>org.apache.poi</groupId>
							    <artifactId>poi</artifactId>
							    <version>3.12</version>
							</dependency>

二、上图
在这里插入图片描述
1、每个mysql都会自动带有一个information_schema库,我们点开 ,进入下面的CLOUMNS表,看到的数据如图,
carrant 使我们自己创建的数据,CLOUMNS表记录着我们每张表的表名,列明,可为空,备注,是否主键等等。所以我们只需要操作这张表就好了。
三、上代码,一气呵成,大家只需改成自己的数据库修改信息就可成功导出,具体如下
1.创建我们要导出的字段的实体类

package com.xzw.poiExcel;

public class TableInfo {

    /*
    *
    * 这里我们可以自行观察CLOUMNS 表里的列和数据 自行创建需要的列 。。我个人觉得这些就够了
    * */
    private String  COLUMN_NAME ;  //列明
    private String  COLUMN_TYPE ;   //数据类型
    private String  COLUMN_KEY;        //主键
    private String  COLUMN_COMMENT; //备注
    private String  IS_NULLABLE;    //是否为空
    private String  TABLE_NAME; //表名称

    public String getIS_NULLABLE() {
        return IS_NULLABLE;
    }

    public void setIS_NULLABLE(String IS_NULLABLE) {
        this.IS_NULLABLE = IS_NULLABLE;
    }

    public String getTABLE_NAME() {
        return TABLE_NAME;
    }

    public void setTABLE_NAME(String TABLE_NAME) {
        this.TABLE_NAME = TABLE_NAME;
    }

    public String getCOLUMN_NAME() {
        return COLUMN_NAME;
    }

    public void setCOLUMN_NAME(String COLUMN_NAME) {
        this.COLUMN_NAME = COLUMN_NAME;
    }

    public String getCOLUMN_TYPE() {
        return COLUMN_TYPE;
    }

    public void setCOLUMN_TYPE(String COLUMN_TYPE) {
        this.COLUMN_TYPE = COLUMN_TYPE;
    }

    public String getCOLUMN_KEY() {
        return COLUMN_KEY;
    }

    public void setCOLUMN_KEY(String COLUMN_KEY) {
        this.COLUMN_KEY = COLUMN_KEY;
    }

    public String getCOLUMN_COMMENT() {
        return COLUMN_COMMENT;
    }

    public void setCOLUMN_COMMENT(String COLUMN_COMMENT) {
        this.COLUMN_COMMENT = COLUMN_COMMENT;
    }
}

2、导出部分代码

package com.xzw.poiExcel;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.util.HSSFColor;


public class exportUtil {


        public static void main(String[] args) throws ClassNotFoundException, IOException, SQLException {

            hahaha();

        }



        public static void hahaha() throws IOException, SQLException, ClassNotFoundException{
            // TODO Auto-generated method stub
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager.getConnection("jdbc:mysql://101.201.***.151:3306/information_schema"


                    ,"root" , "********" );


            String sql="select  COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_COMMENT, TABLE_NAME, COLUMN_KEY "
                    + " from  information_schema.columns "

                    + " where information_schema.columns.TABLE_NAME= ? "
                                                                      //carrant 要生成表所在数据库名称
                    + " and information_schema.columns.TABLE_SCHEMA ='carrant'";


            PreparedStatement prepareStatement = connection.prepareStatement(sql);

                                                        //表名称
            prepareStatement.setString(1, "car");  //即给sql赋值
            ResultSet rs = prepareStatement.executeQuery();
            
            //封装所有查出的数据
            List<TableInfo> list=new ArrayList<>();
            String s="";
            while(rs.next()){
                TableInfo tableInfo = new TableInfo();
                tableInfo.setCOLUMN_NAME(rs.getString(1));
                tableInfo.setCOLUMN_TYPE(rs.getString(2));
                tableInfo.setIS_NULLABLE(rs.getString(3));
                tableInfo.setCOLUMN_COMMENT(rs.getString(4));
                tableInfo.setTABLE_NAME(rs.getString(5));
                tableInfo.setCOLUMN_KEY(rs.getString(6));

                s=tableInfo.getTABLE_NAME();
                list.add(tableInfo);
            }


            //connection.close();
            String fileName="表"+s+"的表结构";
            HSSFWorkbook workbook = new HSSFWorkbook();


            //创建Excel工作表对象
            HSSFSheet sheet = workbook.createSheet();
            sheet.setColumnWidth(0, 3000);
            sheet.setColumnWidth(1, 5000);
            sheet.setColumnWidth(2, 4000);
            sheet.setColumnWidth(3, 2500);
            sheet.setColumnWidth(4, 3000);
            sheet.setColumnWidth(5, 6000);
            sheet.setColumnWidth(6, 6000);

            // 设置表头字体样式
            HSSFFont columnHeadFont = workbook.createFont();
            columnHeadFont.setFontName("宋体");
            columnHeadFont.setFontHeightInPoints((short) 10);
            columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

            // 列头的样式
            HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
            columnHeadStyle.setFont(columnHeadFont);
            // 左右居中
            columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            // 上下居中
            columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            columnHeadStyle.setLocked(true);
            columnHeadStyle.setWrapText(true);
            // 左边框的颜色
            columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);
            // 边框的大小
            columnHeadStyle.setBorderLeft((short) 1);
            // 右边框的颜色
            columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);
            // 边框的大小
            columnHeadStyle.setBorderRight((short) 1);
            // 设置单元格的边框为粗体
            columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            // 设置单元格的边框颜色
            columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index);
            // 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
            columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
            // 设置普通单元格字体样式
            HSSFFont font = workbook.createFont();
            font.setFontName("宋体");
            font.setFontHeightInPoints((short) 10);

            //创建Excel工作表第一行
            HSSFRow row0 = sheet.createRow(0);
            // 设置行高
            row0.setHeight((short) 750);
            //设置单元格内容
            //设置单元格字体样式     这里就是第一行的所有列的名称  例如 姓名 性别。。。。。
            HSSFCell cell = row0.createCell(0);
            cell.setCellValue("名称");
            cell.setCellStyle(columnHeadStyle);
            cell = row0.createCell(1);
            cell.setCellValue("数据类型");
            cell.setCellStyle(columnHeadStyle);
            cell = row0.createCell(2);
            cell.setCellValue("主键");
            cell.setCellStyle(columnHeadStyle);
            cell = row0.createCell(3);
            cell.setCellValue("注释");
            cell.setCellStyle(columnHeadStyle);


            // 循环写入数据
            for (int i = 0; i < list.size(); i++) {
                TableInfo tableInfo = list.get(i);


                HSSFRow row = sheet.createRow(i + 1);
                cell = row.createCell(0);
                cell.setCellValue(tableInfo.getCOLUMN_NAME());
                cell.setCellStyle(columnHeadStyle);

                cell = row.createCell(1);
                cell.setCellValue(tableInfo.getCOLUMN_TYPE());
                cell.setCellStyle(columnHeadStyle);

                //注释部分  我们可以根据查处的状态 例如1 2  赋予相应的值 如 删除 未删除

                /*cell = row.createCell(3);
                if(tableInfo.getCOLUMN_KEY().equals("PRI")){
                    cell.setCellValue("YES");
                }else{
                    cell.setCellValue("NO");
                }*/

                //主键
                cell.setCellStyle(columnHeadStyle);
                cell = row.createCell(2);
                if(tableInfo.getCOLUMN_KEY().equals("PRI")){
                    cell.setCellValue("YES");
                }else{
                    cell.setCellValue("NO");
                }
                cell.setCellStyle(columnHeadStyle);
                cell = row.createCell(3);
                cell.setCellValue(tableInfo.getCOLUMN_COMMENT());
                cell.setCellStyle(columnHeadStyle);
            }
            // String property = System.getProperty("user.home");
            // System.out.println(property);
            FileOutputStream os = new FileOutputStream("D://"+s+".xls");
            // 重置输出流
        /* fileOutputStream.reset();
        // 设定输出文件头
        fileOutputStream.setHeader("Content-disposition",
         "attachment; filename=" + new String(fileName.getBytes("GB2312"), "8859_1") + ".xls");
        // 定义输出类型
        response.setContentType("application/msexcel");*/
            workbook.write(os);
            os.flush();
            os.close();
        }
}

3 、去D盘,查看导出结果,在这里插入图片描述
嘻嘻,,点个赞吧

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值