python读取excel自动化生成sql建表语句和java实体类字段

1、首先准备一个excel文件:

 

idtypename
idint学号
namestring姓名
ageint年龄
sexstring性别
weightdecimal(20,4)体重
scoredecimal(20,4)分数

2、直接生成java字段和注释:

import pandas as pd

df = pd.read_excel('test.xlsx', sheet_name='Sheet1')


for i in range(len(df.values)):
    data_type = ''
    if df['type'][i] == 'bigint':
        data_type = "Long"
    elif df['type'][i] == 'int':
        data_type = "Integer"
    elif df['type'][i] == 'string':
        data_type = "String"
    elif df['type'][i] == 'decimal(20,4)':
        data_type = "Double"

    strs = "/**\n* " + df['name'][i] + "\n*/\n"
    str_entity = strs + 'private' + ' ' + data_type + ' ' + df['id'][i] + ';'

    print(str_entity)


3、生成建表语句:

import pandas as pd

df = pd.read_excel('test.xlsx', sheet_name='Sheet1')

str1 = 'create table student' + ' ' + '(' + '\n'
str2 = ")"
for i in range(len(df.values)):
    data_type = ''
    if df['type'][i] == 'bigint':
        data_type = "bigint(20)"
    elif df['type'][i] == 'int':
        data_type = "int(11)"
    elif df['type'][i] == 'string':
        data_type = "varchar(20)"
    elif df['type'][i] == 'decimal(20,4)':
        data_type = "decimal(20,4)"
    str = '`' + df['id'][i] + '`' + ' ' + data_type + ' ' + 'NOT NULL COMMENT' + ' ' + "'" + df['name'][i] + "'" + ','
    str1 = str1 + str + '\n'
# 删除末尾换行符和","
str1 = str1[:-2]
str1 = str1 + str2 + ';'
print(str1)


结果:

可以根据实际需求增加主键或其它约束。

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值