设为首页 加入收藏

TOP

PDM与Excel利用VB脚本进行互导(六)
2015-11-21 02:05:01 来源: 作者: 【 】 浏览:2
Tags:PDM Excel 利用 脚本 进行
名" x1.Range(CELL_F+"1").Value = "列中文名称" x1.Range(CELL_G+"1").Value = "列注释" x1.Range(CELL_H+"1").Value = "数据类型" x1.Range(CELL_I+"1").Value = "主键" x1.Range(CELL_J+"1").Value = "是否为空" x1.Range(CELL_K+"1").Value = "默认值" '设置字体 x1.Columns(CELL_A+":"+CELL_K).SELECT WITH x1.Selection.Font .Name = "宋体" .Size = 10 END WITH '设置首行可过滤,背景颜色为灰色,字体粗体 x1.Range(CELL_A+"1:"+CELL_K+"1").SELECT x1.Selection.AutoFilter x1.Selection.Interior.ColorIndex = 15 x1.Selection.Font.Bold = TRUE '设定首行固定 x1.Range(CELL_A+"2").SELECT x1.ActiveWindow.FreezePanes = TRUE END SUB

?

4、Excel直接生成建库脚本的VB

在Excel中,主要通过如下的菜单找到写宏执行宏的地方:

文件-->选项-->自定义功能区-->自定义功能区(主选项卡)-->勾选"开发工具";然后到开发工具主菜单中,开发工具-->宏-->进行新建和执行。

From_Excel_model_generate_sql.txt

Sub create_all_sheet_sql()
Dim xlsheet

For Each xlsheet In ThisWorkbook.Worksheets
    Create_SQL xlsheet.Name, "F:\model\"
Next

End Sub
Sub Create_SQL(sheetName, outputPath)

Dim strPath As String
Dim RowCount As Integer
Dim xlsheet_src
Dim strSQL As String
Dim hasCreat As Integer

Dim strTable1 As String
Dim strTable As String
Dim strTableComm As String
Dim strField As String
Dim strFieldComm As String
Dim strType As String
Dim strKey As String

' 请根据实际情况修改下面3个值
'sheetName = "1-核心表"  '要生成SQL的Sheet页的名称
strPath = outputPath + sheetName + ".sql" '"d:\2001.sql"    '生成的SQL文件

Set xlsheet_src = ThisWorkbook.Worksheets(sheetName)
RowCount = xlsheet_src.UsedRange.Cells.Rows.Count '得到此Sheet的行数
hasCreat = 0

'生成表的建表语句
For i = 2 To RowCount + 1
    
    strTable1 = xlsheet_src.Range("C" + CStr(i)).Value
    
    If strTable <> strTable1 Then
        If hasCreat = 1 Then
            strSQL = ");"
            ret = sWriteFile(strSQL, strPath)
            strSQL = ""
            hasCreat = 0
        End If

        strTable = strTable1
        If (strTable <> "") Then
            strTableComm = xlsheet_src.Range("D" + CStr(i)).Value
            strSQL = "DROP TABLE " & strTable & ";" & vbCrLf & "CREATE TABLE " & strTable & "( " & " -- " & strTableComm
            ret = sWriteFile("", strPath)
            ret = sWriteFile(strSQL, strPath)
            intRow = 1
            hasCreat = 1
        End If
    End If

    If strTable <> "" Then
        strField = xlsheet_src.Range("E" + CStr(i)).Value
        strFieldComm = xlsheet_src.Range("F" + CStr(i)).Value
        strType = xlsheet_src.Range("H" + CStr(i)).Value
        If strField <> "" Then
            If intRow = 1 Then
                strSQL = "    " & strField & " " & strType & " -- " & strFieldComm
            Else
                strSQL = "   ," & strField & " " & strType & " -- " & strFieldComm
            End If
            ret = sWriteFile(strSQL, strPath)
            intRow = intRow + 1
        End If
    End If
    
    
Next

'生成表的comment语句
For i = 2 To RowCount
    
    strTable1 = xlsheet_src.Range("C" + CStr(i)).Value
    If strTable1 <> "" Then
        If strTable <> strTable1 Then
            strTable = strTable1
            strTableComm = xlsheet_src.Range("D" + CStr(i)).Value
            strSQL = "comment on table " & strTable & " is '" & strTableComm & "';"
            ret = sWriteFile("", strPath)
            ret = sWriteFile(strSQL, strPath)
            intRow = 1
            hasCreat = 1
        End If
    End If

    If strTable <> "" Then
        
        strField = xlsheet_src.Range("E" + CStr(i)).Value
        strFieldComm = xlsheet_src.Range("F" + CStr(i)).Value
        strType = xlsheet_src.Range("H" + CStr(i)).Value
        If strField <> "" Then
            strSQL = "comment on column " & strTable & "." & strField & " is '" & strFieldComm & "';"
            ret = sWriteFile(strSQL, strPath)
            intRow = intRow + 1
        End If
        
    End If

    
Next

End Sub


Function sWriteFile(strSQL As String, strFullFileName As String)
首页 上一页 3 4 5 6 下一页 尾页 6/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇nginx+memcached+tomcat集群sessi.. 下一篇物联网(IOT)数据库需求和当前技术..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: