Chinaunix首页 | 论坛 | 博客
  • 博客访问: 119947
  • 博文数量: 24
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2016-11-22 14:58
个人简介

坚持,做最好的自己

文章分类

全部博文(24)

文章存档

2015年(2)

2014年(9)

2013年(13)

我的朋友

分类: WINDOWS

2013-04-19 10:36:15

第一个sheet建立按钮CommandButton1,
第二个sheet问配置表,第一列为用户,第二列为表名,第三列为列名,第四列为属性
第三四。。个sheet为表的配置,第一行为列名,以下为列中的数据,(sheet名为“用户.表名”)

导出文件在当前目录,文件名为TestFile.sql
  1. Public MyArray As Variant
  2. Private Sub CommandButton1_Click()

  3.     Dim gPath As String
  4.     Dim sFile As Object, Fso As Object
  5.     Dim ColInfo As Variant
  6.     Dim iRow As Integer
  7.     Dim iCol As Integer
  8.     Dim Sqlstr As String

  9.     gPath = Application.ActiveWorkbook.Path
  10.     Set Fso = CreateObject("Scripting.FileSystemObject")
  11.     Set sFile = Fso.CreateTextFile(gPath & "/TestFile.sql", True)

  12.     MyArray = Sheets("配置表定义").UsedRange

  13.     '循环sheet
  14.     For Each Shnow In Worksheets
  15.         collist = ""
  16.         sFile.WriteBlankLines (1) '写入一个空白行
  17.         'sheet名称包含"点"
  18.         If InStr(1, Shnow.Name, ".") <> 0 Then
  19.         '第一行的名称以及对应的属性
  20.         MaxCol = Shnow.Range("A1").End(xlToRight).Column
  21.         ReDim ColInfo(1 To 2, 1 To MaxCol)
  22.         For iCol = 1 To Shnow.Range("A1").End(xlToRight).Column
  23.             '取得列名
  24.             ColInfo(1, iCol) = Shnow.Cells(1, iCol).Value
  25.             collist = collist & "," & Shnow.Cells(1, iCol).Value
  26.         Next iCol
  27.         collist = "insert into " & Shnow.Name & "(" & Right(collist, Len(collist) - 1) & ") values"

  28.         '从单元格A2开始读取数据,到数据表结尾,写入到文本文件中
  29.         For iRow = 2 To Shnow.Range("A65536").End(xlUp).Row
  30.         '第一列不为空时
  31.         If Shnow.Cells(iRow, 1).Value <> "" Then
  32.             Sqlstr = ""
  33.             For iCol = LBound(ColInfo, 2) To UBound(ColInfo, 2) '从数组中读取数据并写入对应的单元格
  34.                 If Shnow.Cells(iRow, iCol).Value = "" Then
  35.                     Sqlstr = Sqlstr & "," & "null"
  36.                 ElseIf Tab_attribute(Shnow.Name & "." & Shnow.Cells(1, iCol).Value) = "CHAR" Then
  37.                     Sqlstr = Sqlstr & "," & "'" & Replace(Shnow.Cells(iRow, iCol).Value, "'", "''") & "'"
  38.                 ElseIf Tab_attribute(Shnow.Name & "." & Shnow.Cells(1, iCol).Value) = "NUMBER" Then
  39.                     Sqlstr = Sqlstr & "," & Shnow.Cells(iRow, iCol).Value
  40.                 ElseIf Tab_attribute(Shnow.Name & "." & Shnow.Cells(1, iCol).Value) = "DATE" Then
  41. ' Sqlstr = Sqlstr & "," & "'" & Shnow.Cells(iRow, iCol).Value & "'"
  42.                     Sqlstr = Sqlstr & "," & "to_date('" & CStr(Format(Shnow.Cells(iRow, iCol).Value, "yyyy-mm-dd hh:nn:ss")) & "','yyyy-mm-dd hh24:mi:ss')"

  43.                 End If

  44.             Next iCol
  45.             Sqlstr = "(" & Right(Sqlstr, Len(Sqlstr) - 1) & ");"
  46.             sFile.WriteLine (collist & Sqlstr)
  47.         End If
  48.         Next iRow

  49.         End If

  50.     Next

  51.     sFile.WriteBlankLines (1) '写入一个空白行
  52.     sFile.WriteLine ("commit;")
  53.     sFile.Close
  54.     Set sFile = Nothing
  55.     Set Fso = Nothing

  56. End Sub

  57. Function Tab_attribute(User_Tab_Col As String) As String

  58.     '第一行跳过
  59.     For iRow = 2 To UBound(MyArray, 1)
  60.         If UCase(Replace(MyArray(iRow, 1) & "." & MyArray(iRow, 2) & "." & MyArray(iRow, 3), " ", "")) = UCase(Replace(User_Tab_Col, " ", "")) Then
  61.             If InStr(1, UCase(MyArray(iRow, 4)), "CHAR") <> 0 Then
  62.                 Tab_attribute = "CHAR"
  63.             ElseIf InStr(1, UCase(MyArray(iRow, 4)), "DATE") <> 0 Then
  64.                 Tab_attribute = "DATE"
  65.             Else
  66.                 Tab_attribute = "NUMBER"
  67.             End If
  68.             Exit For
  69.         End If
  70.     Next iRow

  71. End Function

阅读(2209) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~