@java
最近公司有个需求,提早让数据库表结构在excel模板中设计,而后再建对应的数据库DB。字段少好说,无脑录入便可,可是,我遇到个100多个字段的,实在忍不了,最终入门了VBA编程,本身写脚本生成了sql语句。减小了须要无用重复劳动。mysql
首先学习下vba的基础。sql
alt + f11数据库
文件 - 选项 - 自定义功能区 - 勾选开发工具编程
录制宏数组
使用相对应用录制宏微信
双击某个sheet页, 把鼠标放在 窗口里面,点击工具栏的插入,选择过程,随便起个过程名,好比 class就生成了代码:函数
Public Sub class() End Sub
写个hello world程序:工具
Sub class() Dim name name = "Hello World" name = "胡老师好" MsgBox name End Sub
在工具栏中选择 选择当即窗口,和本地窗口。
oop
Sub class() Dim name name = "Hello World" Debug.Print name name = "胡老师好" MsgBox name Debug.Print name End Sub
注释有2种形式,一种是rem。一种是 ' (单引号)
variant 表明任意类型
single double decimal 表明 小数、
rem 表明注释关键字
Const 常量关键字
Sub class() Rem variant是任意类型 Dim name As Variant name = "Hello World" Rem debug Debug.Print name name = "胡老师好" Debug.Print name Rem 定义常量 Const num As Integer = 123 Debug.Print num End Sub
Sub class1() Dim x As Integer Rem static 是静态变量 Static y As Integer x = x + 1 y = y + 1 Debug.Print "x=" & x Debug.Print "y=" & y End Sub
Sub class3() Dim number As Integer number = 1 If number > 100 Then Debug.Print "优秀" ElseIf number > 95 Then Debug.Print "良好" Else Debug.Print "通常" End If End Sub
Sub class3() Dim number As String number = "匹配" If number > "匹配" Then Debug.Print "优秀" ElseIf number <> "匹配" Then Debug.Print "良好" End If End Sub
Sub class4() Dim number As String number = "匹配" Select Case number Case "匹配" Debug.Print "匹配" Case "不匹配" Debug.Print "不匹配" End Select End Sub
Sub class5() Dim count As Integer Dim if_f As Boolean count = 11 if_f = (count = 10) MsgBox if_f End Sub
Sub class() Rem 演示循环 Dim count As Integer For count = 1 To 10 Debug.Print count Next Debug.Print "count 循环结束以后的值是 " & count End Sub
循环控制单元格属性
Sub class() Rem 演示循环 Dim count As Integer For count = 2 To 10 If count Mod 2 = 0 Then Rem rang函数表明选中的某列单元格 Rem Interior表明单元格内部的对象 Range("T" & count).Interior.ColorIndex = 1 Else Range("T" & count).Interior.ColorIndex = 3 End If Next Debug.Print "count 循环结束以后的值是 " & count End Sub
循环控制单元格求和
Sub class() Rem 演示循环 Dim COUNT As Integer Dim score As Double For COUNT = 2 To 20 Rem cells的参数 第一个参数表明横行,第2个参数表明竖行 Cells(COUNT, 8) = "=sum(rc[-1]:rc[-6])" Next End Sub
Sub class() Rem 演示循环 Dim COUNT As Integer Dim score As Double For COUNT = 2 To 20 Rem cells的参数 第一个参数表明横行,第2个参数表明竖行 Cells(COUNT, 8) = "=sum(rc[-1]:rc[-6])" score = Cells(COUNT, 8) If score > 700 Then Cells(COUNT, 9) = "秀儿" ElseIf score > 650 Then Cells(COUNT, 9) = "良好" Else Cells(COUNT, 9) = "小垃圾" End If Next End Sub
Sub class() Rem do while 演示 Dim count As Integer count = 20 Do While count > 10 Debug.Print count count = count - 1 Debug.Print count Loop Rem do .. loop 条件 不演示了 Do Loop While count > 10 End Sub
使用for循环的时候退出用 exit for
Sub class1() Dim count As Integer For count = 1 To 10 If count = 5 Then Debug.Print "count 退出循环的值是: " & count Exit For End If Debug.Print count Next End Sub
使用fo while循环的时候退出用 exit do
Sub class1() Dim count As Integer Do While True count = count + 1 If count > 5 Then Debug.Print "此时退出循环的值是: " & count Exit Do End If Loop End Sub
Sub class() Dim arr(2) As Variant Dim i As Integer arr(0) = "小明" arr(1) = 2 arr(2) = True For i = 0 To 2 Debug.Print arr(i) Next End Sub
下标越界
Sub class() ' Const i As Integer = 10 ' Dim arr(i) As Variant Rem 能够指明数组的范围奥 起始开始限制了也是下标越界 Dim arr(2 To 5) As Variant arr(1) = 1 Debug.Print arr(1) End Sub
Sub class() Dim arr(2 To 5, 3 To 6) As Variant arr(2, 3) = 1 Debug.Print arr(2, 3) End Sub
遍历循环
Sub class() Dim arr(2 To 5, 3 To 6) As Variant arr(2, 3) = 1 'Debug.Print arr(2, 3) For x = 2 To 5 For y = 3 To 6 Debug.Print arr(2, 3) Next Next End Sub
2中方式,一种是range,一种是cells
range
cells
如今掌握了上面的基础知识,基本上能够知足咱们最开始的需求
解决思路:循环方式获取单元格中的内容,拼接成 sql 的建立脚本语句便可
。
Public Sub class() Rem 声明字段row的开始行号 Const startRow As Integer = 13 Rem 声明字段row的结束行号 Const endRow As Integer = 28 Rem 声明表名 Dim tableName As String tableName = Range("E" & 6) Rem 声明主键 Dim primaryKey As String primaryKey = Range("F" & 13) Rem 声明表名注释 Dim tableComment As String tableComment = Range("E" & 7) Rem 声明字段名对应列 英文序号 Dim filedMetaNo As String filedMetaNo = "F" Rem 声明字段注释对应对应列 英文序号 Dim commentMetaNo As String commentMetaNo = "C" Rem 声明字段备注 对应列的 英文序号 Dim comment2MetaNo As String comment2MetaNo = "U" Rem 声明类型 对应列的 英文序号 Dim typeMetaNo As String typeMetaNo = "G" Rem 声明字长 对应的列的英文序号 Dim lengthMetaNo As String lengthMetaNo = "H" Rem 最终要拼接的sql Dim sqlStr As String sqlStr = "CREATE TABLE " & Range("E" & 6) & Chr(13) sqlStr = sqlStr & "(" & Chr(13) For count = startRow To endRow Rem 拼接 字段名 sqlStr = sqlStr & Replace(Range(filedMetaNo & count).Text, " ", "") Rem 拼接 字段类型(字段长度) sqlStr = sqlStr & " " & Range(typeMetaNo & count) If IsEmpty(Range(lengthMetaNo & count)) = False Then sqlStr = sqlStr & "(" & Range(lengthMetaNo & count) & ") " End If Rem 若是是主键,设置NOT NULL COMMENT If primaryKey = Range(filedMetaNo & count) Then sqlStr = sqlStr & " NOT NULL COMMENT " Else Rem 拼接 DEFAULT NULL COMMENT '字段名称注释(字段备注)' sqlStr = sqlStr & " DEFAULT NULL COMMENT " End If sqlStr = sqlStr & "'" & Range(commentMetaNo & count) If IsEmpty(Range(comment2MetaNo & count)) = False Then sqlStr = sqlStr & "(" & Range(comment2MetaNo & count) & ")" End If sqlStr = sqlStr & "'" sqlStr = sqlStr & "," & Chr(13) Next sqlStr = sqlStr & "PRIMARY KEY (" & primaryKey & ")" & Chr(13) sqlStr = sqlStr & ")ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='" & tableComment & "'" & Chr(13) Debug.Print sqlStr End Sub
显示结果:
在navicat中运行
显示已经把mysql数据库的表结构导入数据库了。
脚本只须要改一个地方:
对应这里的行号:
便可。
以上便是用脚本生成的4张表效果图。
我的微信公众号:
搜索: 盛开de每一天
不定时推送相关文章,期待和你们一块儿成长!!
完
谢谢你们支持!