首页 > 数据库 > SQL Server > 正文

详解数据库导出成excel的sql语句

2024-08-31 00:46:53
字体:
来源:转载
供稿:网友

有些软件,或者数据库支持将Excel中的内容导入到数据库中,然后可以乘胜sql语句,但是真正的想要达到自己的目的,还真的需要自己下一番功夫。而office中的好多东西都可以用到vbs,特别是excel这种有一定规则的软件。因此,我记录下部分用于演示的代码,待以后慢慢研究。

****************************************************************************************************************************************

代码一:其实是将sheet1中有一定规律的数据(数据库表名称以及后面跟着的该表的字段),映射到sheet2中。sheet2针对于sheet1来说,更像是一个目录。

****************************************************************************************************************************************

Option Explicit

'Author: LiSheng Feng (eBis)
Sub SyncIndex()
    Sheets(2).Cells.Clear
   
    Dim LinkCurrentRow As Long
    LinkCurrentRow = 1
   
    Dim CellString As String
    Dim LinkName As String
   
    Dim i As Integer
    For i = 2 To Sheets(1).Cells(1, 1).SpecialCells(xlCellTypeLastCell).Row
        If Sheets(1).Cells(i, 1) = "" Then
            CellString = UCase(Sheets(1).Cells(i, 2))
           
            If CellString <> "" Then
                If InStr(CellString, " VIEW ") = 0 Then
                    If Not (Left(CellString, 3) = "IX_" Or InStr(CellString, "IDX") > 0 Or InStr(CellString, "INDEX") > 0) Then
                        LinkName = Sheets(1).Cells(i, 3)
                       
                        If LinkName = "" Then
                            LinkName = CellString
                        End If
                       
                        Sheets(2).Hyperlinks.Add Anchor:=Sheets(2).Cells(LinkCurrentRow, 1), Address:="", _
                                    SubAddress:=Sheets(1).Name + "!B" + CStr(i), TextToDisplay:=LinkName
                       
                       
                        Sheets(2).Cells(LinkCurrentRow, 2) = UCase(Sheets(1).Cells(i, 2))
                        LinkCurrentRow = LinkCurrentRow + 1
                    End If
                End If
            End If
        End If
    Next i
   
    Sheets(2).Columns(1).AutoFit
    Sheets(2).Columns(2).AutoFit
   
    MsgBox "同步完成", vbOKOnly + vbInformation
End Sub

****************************************************************************************************************************************

代码二:将sheet1中多个数据库的表结构标称sql语句

****************************************************************************************************************************************
Sub createSql()
   
Sheets(5).Cells.Clear
   
    Dim LinkCurrentRow As Long
    LinkCurrentRow = 1
   
    Dim CellString As String
    Dim LinkName As String
   
    Dim i As Integer
   
    Dim sqlStr As String
   
    Dim tblName As String
   
    Dim tblStart As Boolean
    Dim tblCount As Integer
    Dim tblSql As String
   
    Dim fldName As String '字段名称
    Dim fldType As String '字段类型
   
   
    tblStart = False
    tblCount = 0
   
    For i = 2 To Sheets(1).Cells(1, 1).SpecialCells(xlCellTypeLastCell).Row
       
        If Sheets(1).Cells(i, 1) = "" Then  '表名
           
            If Sheets(1).Cells(i, 3) <> "" Then '剔除IDX
                If tblCount <> 0 Then
                    Sheets(5).Cells(tblCount + 1, 1) = Left(tblSql, Len(tblSql) - 2) & ") ON [PRIMARY]" '删除最有一个,和回车后增加语句
                    tblSql = ""
                End If
               
                tblCount = tblCount + 1
                tblName = Sheets(1).Cells(i, 2)
                tblSql = "Create TABLE dbo.[" & tblName & "]("
            End If
        Else    '字段名称
            'eg: "[ShipName_EN] [nvarchar](100) COLLATE Chinese_PRC_CI_AS NOT NULL,"
            fldName = "[" & Sheets(1).Cells(i, 2) & "]"
            fldType = GetFieldType(Sheets(1).Cells(i, 4))
               
            If fldName <> "" Then
                tblSql = tblSql & fldName & " " & fldType & "," & vbCr
            End If
        End If
       
       
       
    Next i

End Sub


Function GetFieldType(s As String) As String
Dim ret As String
Dim idxlft, idxrgt As Integer


    If s <> "" Then
        idxlft = InStr(s, "(")
        idxrgt = InStr(s, ")")
       
        If (idxlft > 0) And (idxrgt > 0) Then
            ret = "[" & Mid(s, 1, idxlft - 1) & "]" & Mid(s, idxlft, Len(s) - idxlft + 1)
        Else
            ret = s
        End If
               
    End If
   
    GetFieldType = ret

End Function

****************************************************************************************************************************************

代码三:根据数据库表中的数据(不再是表结构,而是数据),生成sql语句并保存到文件中。

****************************************************************************************************************************************

'最大行数
Const MAX_NUM_ROW = 5000

'导出文件路径所在单元格
Const PATH_OUTPUT_ROW = 3
Const PATH_OUTPUT_COL = 3

'定义列常量
Const NAME_COL = 1
Const GENDER_COL = 2
Const PHONE_COL = 3
Const EMAIL_COL = 4

'读取数据开始行数
Const START_ROW = 5

'定义数据实体类
Private Type Tmplt
NAME As String
GENDER As String
PHONE As String
EMAIL As String
End Type

'行数变量
Dim noOfTmplts As Integer

'数据实体类数组
Dim TmpltArray(MAX_NUM_ROW) As Tmplt

'点击按钮触发事件
Private Sub CommandButton1_Click()
generateSQL
End Sub

'生成SQL
Private Sub generateSQL()
makedir
initData
writeToFile
End Sub

'构建文件输出路径
Private Sub makedir()
On Error Resume Next
MkDir Sheet1.Cells(PATH_OUTPUT_ROW, PATH_OUTPUT_COL)
End Sub

'读取Excel数据,填充实体类数组
Private Sub initData()

Erase TmpltArray
noOfTmplts = 0

Dim j As Integer

'循环读取Excel数据行
For j = START_ROW To MAX_NUM_ROW

TmpltArray(noOfTmplts).NAME = Sheet1.Cells(j, NAME_COL)
TmpltArray(noOfTmplts).GENDER = Sheet1.Cells(j, GENDER_COL)
TmpltArray(noOfTmplts).PHONE = Sheet1.Cells(j, PHONE_COL)
TmpltArray(noOfTmplts).EMAIL = Sheet1.Cells(j, EMAIL_COL)
noOfTmplts = noOfTmplts + 1

Next
End Sub

'读取实体类数组,生成SQL并写入文件
Private Sub writeToFile()

Dim lvOutputPath As String

'输出文件路径
lvOutputPath = Sheet1.Cells(PATH_OUTPUT_ROW, PATH_OUTPUT_COL)

If lvOutputPath = "" Then
MsgBox "没有找到输出文件路径!"
Exit Sub
End If

fileNum = FreeFile

'打开输出文件
Open lvOutputPath For Output As fileNum


Dim lvUserSql As String
Dim nameStr As String
Dim genderStr As String
Dim phoneStr As String
Dim emailStr As String

'循环生成SQL
For j = 0 To noOfTmplts - 1

nameStr = TmpltArray(j).NAME
genderStr = TmpltArray(j).GENDER
phoneStr = TmpltArray(j).PHONE
emailStr = TmpltArray(j).EMAIL

If nameStr <> "" Then
lvUserSql = "Insert into Students(name,gender,phone,email) values('" & nameStr & "','" & genderStr & "','" & phoneStr & "','" & emailStr & "');"
Print #fileNum, lvUserSql
End If
Next

Close fileNum

MsgBox "文件生成完成!"

Exit Sub

Err_Open_File:
Close lvFileNum

If Err.Number = 76 Then
'路径未找到
MsgBox Err.Description
Exit Sub

Else
MsgBox Err.Description
Exit Sub
End If


End Sub

****************************************************************************************************************************************

这些代码都是卸载宏里面的,到时候执行宏就可以了。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表