@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每一天
不定時推送相關文章,期待和你們一塊兒成長!!
完
謝謝你們支持!