Chinaunix首页 | 论坛 | 博客
  • 博客访问: 625888
  • 博文数量: 142
  • 博客积分: 116
  • 博客等级: 入伍新兵
  • 技术积分: 1445
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-28 08:37
文章分类

全部博文(142)

文章存档

2017年(7)

2016年(57)

2015年(48)

2014年(30)

我的朋友

分类: WINDOWS

2015-02-17 17:36:24

概要
通过word vba读取word文档,遍历文档中的Tables,
如果table的格式符合预想,则读取数据库定义信息。
根据定义信息生成建表sql语句,并写出到文件。


  1. Option Explicit

  2. Public Const DEFAULT_FILEPATH = "E:\test\"
  3. Public Const DEFAULT_SQL_OUTPUT_PATH = "E:\sql"
  4. Public Const MAX_FILE_COUNT = 5
  5. Public Const MAX_TABLES_IN_DOC = 20
  6. Public Const MAX_COLUMNS_IN_TABLE = 100

  7. Public Type t_columnInfo
  8.     columnName As String
  9.     columnID As String
  10.     columnType As String
  11.     columnIsPK As Boolean
  12.     columnNotNull As Boolean
  13.     columnComment As String
  14. End Type

  15. Public Type t_tableInfo
  16.     tableName As String
  17.     tableID As String
  18.     tableComment As String
  19.     columnCount As Integer
  20.     columns(MAX_COLUMNS_IN_TABLE) As t_columnInfo
  21. End Type

  22. Private Sub getDocInfo(d As Document)
  23.     Dim pagecnt As Integer
  24.     Dim linecnt As Integer
  25.     Dim tablecnt As Integer
  26.     pagecnt = d.BuiltInDocumentProperties(wdPropertyPages)
  27.     linecnt = d.BuiltInDocumentProperties(wdPropertyLines)
  28.     tablecnt = d.Tables.Count

  29.     Debug.Print d.Tables(1).Rows(3).Cells(2).Range.Text & " " & pagecnt & " " & linecnt & " " & tablecnt

  30. End Sub

  31. Private Sub testsub(s As String)
  32.     MsgBox s
  33. End Sub

  34. Private Function opendoc(filename As String)
  35.     On Error Resume Next
  36.     Set opendoc = Nothing
  37.     
  38.     Dim d As Document
  39.     Set d = Documents.Open(filename:=filename, ConfirmConversions:=False, ReadOnly:=True, AddToRecentFiles:=False, Visible:=False)
  40.     ''''''''''''''''''''''''''''''' do not use Visible:=False. it cause open doc menually but show nothing
  41.     If d Is Nothing Then
  42.         Debug.Print "error open " & filename
  43.         Exit Function
  44.     End If
  45.     
  46.     'Call showTable(d)
  47.     
  48.     Set opendoc = d

  49. End Function

  50. Private Sub closedoc(d As Document)
  51.     d.Close (wdDoNotSaveChanges)
  52. End Sub

  53. Private Function trimCRLF(ByVal s As String)
  54.     Dim l As Long
  55.                                     'Debug.Print "trimCRLF in: len=" & Len(s) & " s=" & s
  56.     If s = "" Then
  57.         trimCRLF = ""
  58.     End If
  59.     
  60.     l = Len(s)
  61.     Do While l > 0
  62.         If Mid(s, l, 1) = Chr(10) Or Mid(s, l, 1) = Chr(13) Or Mid(s, l, 1) = Chr(7) Then
  63.             s = Left(s, l - 1)
  64.             l = l - 1
  65.         Else
  66.             Exit Do
  67.         End If
  68.     Loop
  69.     trimCRLF = s
  70.                                     'Debug.Print "trimCRLF out: len=" & Len(s) & " s=" & s
  71. End Function

  72. Private Sub test_trimCRLF()
  73.     trimCRLF ("")
  74.     trimCRLF ("1")
  75.     trimCRLF ("2 two spaces follow ")
  76.     trimCRLF ("3 0D follow" & Chr(13))
  77.     trimCRLF ("4 0A follow" & Chr(10))
  78.     trimCRLF ("5 0D0A follow" & Chr(13) & Chr(10))
  79. End Sub

  80. Private Function checkTableFormat(t As Table)
  81.     On Error Resume Next

  82.     Dim msgStr As String
  83.     Dim rowcnt As Integer
  84.     Dim colcnt As Integer
  85.     Dim celltext As String
  86.     Dim chkTableOK As Boolean

  87.     rowcnt = t.Rows.Count
  88.     colcnt = t.columns.Count
  89.     
  90.     ' cell(1,1)
  91.     celltext = t.Rows(1).Cells(1).Range.Text
  92.     celltext = trimCRLF(celltext)
  93.     
  94.     ' check table format
  95.     ' 表名 xxx 编码 yyy
  96.     ' 描述 编码 类型 主键 必填 备注
  97.     chkTableOK = True
  98.     ' check. 6 columns
  99.     If colcnt <> 6 Then
  100.         If celltext = "项目" Or celltext = "名称" Or UCase(Left(celltext, 2)) = "NO" Then
  101.             ' skip page description
  102.         Else
  103.             Debug.Print "[error] table format incorrect. column count is " & colcnt
  104.         End If
  105.         chkTableOK = False
  106.     End If
  107.     
  108.     If chkTableOK = True Then
  109.         ' check. row1 col1 is 表名/表名描述
  110.         celltext = t.Rows(1).Cells(1).Range.Text
  111.         celltext = trimCRLF(celltext)
  112.         If celltext <> "表名" And celltext <> "表名描述" Then
  113.             Debug.Print "[error] table format incorrect. cell(1,1) is " & celltext
  114.             chkTableOK = False
  115.         End If
  116.     End If
  117.     
  118.     If chkTableOK = True Then
  119.         ' check. row1 col3 is 编码/表名
  120.         celltext = t.Rows(1).Cells(3).Range.Text
  121.         celltext = trimCRLF(celltext)
  122.         If celltext <> "编码" And celltext <> "表名" Then
  123.             Debug.Print "[error] table format incorrect. cell(1,3) is " & celltext
  124.             chkTableOK = False
  125.         End If
  126.     End If

  127.     If chkTableOK = True Then
  128.         ' check. row2 col1 is 描述
  129.         celltext = t.Rows(2).Cells(1).Range.Text
  130.         celltext = trimCRLF(celltext)
  131.         If celltext <> "描述" Then
  132.             Debug.Print "[error] table format incorrect. cell(2,1) is " & celltext
  133.             chkTableOK = False
  134.         End If
  135.     End If

  136.     If chkTableOK = True Then
  137.         ' check. row2 col2 is 编码
  138.         celltext = t.Rows(2).Cells(2).Range.Text
  139.         celltext = trimCRLF(celltext)
  140.         If celltext <> "编码" Then
  141.             Debug.Print "[error] table format incorrect. cell(2,2) is " & celltext
  142.             chkTableOK = False
  143.         End If
  144.     End If

  145.     If chkTableOK = True Then
  146.         ' check. row2 col3 is 类型 or 数据类型
  147.         celltext = t.Rows(2).Cells(3).Range.Text
  148.         celltext = trimCRLF(celltext)
  149.         If celltext <> "类型" And celltext <> "数据类型" Then
  150.             Debug.Print "[error] table format incorrect. cell(2,3) is " & celltext
  151.             chkTableOK = False
  152.         End If
  153.     End If

  154.     If chkTableOK = True Then
  155.         ' check. row2 col4 is 主键/是否主键
  156.         celltext = t.Rows(2).Cells(4).Range.Text
  157.         celltext = trimCRLF(celltext)
  158.         If celltext <> "主键" And celltext <> "是否主键" Then
  159.             Debug.Print "[error] table format incorrect. cell(2,4) is " & celltext
  160.             chkTableOK = False
  161.         End If
  162.     End If

  163.     If chkTableOK = True Then
  164.         ' check. row2 col5 is 必填/是否必填
  165.         celltext = t.Rows(2).Cells(5).Range.Text
  166.         celltext = trimCRLF(celltext)
  167.         If celltext <> "必填" And celltext <> "是否必填" Then
  168.             Debug.Print "[error] table format incorrect. cell(2,5) is " & celltext
  169.             chkTableOK = False
  170.         End If
  171.     End If

  172.     If chkTableOK = True Then
  173.         ' check. row2 col6 is 备注/必备
  174.         celltext = t.Rows(2).Cells(6).Range.Text
  175.         celltext = trimCRLF(celltext)
  176.         If celltext <> "备注" And celltext <> "必备" Then
  177.             Debug.Print "[error] table format incorrect. cell(2,6) is " & celltext
  178.             chkTableOK = False
  179.         End If
  180.     End If
  181.     
  182.     checkTableFormat = chkTableOK

  183. End Function

  184. Private Sub getTableInfo(t As Table, tableInfo As t_tableInfo)
  185.     On Error Resume Next

  186.     Dim msgStr As String
  187.     Dim rowcnt As Integer
  188.     Dim colcnt As Integer
  189.     Dim celltext As String
  190.     Dim chkTableOK As Boolean
  191.     Dim i As Integer
  192.     Dim indexColumn As Integer
  193.     
  194.     rowcnt = t.Rows.Count
  195.     colcnt = t.columns.Count
  196.     
  197.     chkTableOK = checkTableFormat(t)

  198.     If chkTableOK <> True Then
  199.         Exit Sub
  200.     End If
  201.     ' table name
  202.     celltext = t.Rows(1).Cells(2).Range.Text
  203.     celltext = trimCRLF(celltext)
  204.     tableInfo.tableName = celltext

  205.     ' table id
  206.     celltext = t.Rows(1).Cells(4).Range.Text
  207.     celltext = trimCRLF(celltext)
  208.     tableInfo.tableID = celltext
  209.                                  msgStr = " tablename:" & tableInfo.tableName
  210.                                 msgStr = msgStr & " tableID:" & tableInfo.tableID
  211.                                 msgStr = msgStr & " table has " & rowcnt & " rows"
  212.                                 Debug.Print msgStr
  213.     indexColumn = 0
  214.     ' column defination starts from row 3
  215.     For i = 3 To rowcnt
  216.         ' columnName
  217.         celltext = t.Rows(i).Cells(1).Range.Text
  218.         celltext = trimCRLF(celltext)
  219.         tableInfo.columns(indexColumn).columnName = celltext
  220.         ' columnID
  221.         celltext = t.Rows(i).Cells(2).Range.Text
  222.         celltext = trimCRLF(celltext)
  223.         tableInfo.columns(indexColumn).columnID = celltext
  224.         ' columnType
  225.         celltext = t.Rows(i).Cells(3).Range.Text
  226.         celltext = trimCRLF(celltext)
  227.         tableInfo.columns(indexColumn).columnType = celltext
  228.         ' columnIsPK
  229.         celltext = t.Rows(i).Cells(4).Range.Text
  230.         celltext = trimCRLF(celltext)
  231.         celltext = Trim(celltext)
  232.         'If UCase(celltext) <> "" And UCase(celltext) <> "T" And UCase(celltext) <> "TRUE" Then
  233.         ' Debug.Print "[error] columnIsPK is not in space/T/TRUE/F/FALSE. value is " & celltext
  234.         'End If
  235.         Select Case UCase(celltext)
  236.         Case "T", "TRUE"
  237.             tableInfo.columns(indexColumn).columnIsPK = True
  238.         Case "", "F", "FALSE"
  239.             tableInfo.columns(indexColumn).columnIsPK = False
  240.         Case Else
  241.             Debug.Print "[error] columnIsPK is not in empty/T/TRUE/F/FALSE. value is " & celltext
  242.             tableInfo.columns(indexColumn).columnIsPK = False
  243.         End Select
  244.         
  245.         ' columnNotNull
  246.         celltext = t.Rows(i).Cells(5).Range.Text
  247.         celltext = trimCRLF(celltext)
  248.         celltext = Trim(celltext)
  249.         Select Case UCase(celltext)
  250.         Case "T", "TRUE"
  251.             tableInfo.columns(indexColumn).columnNotNull = True
  252.         Case "", "F", "FALSE"
  253.             tableInfo.columns(indexColumn).columnNotNull = False
  254.         Case Else
  255.             Debug.Print "[error] columnNotNull is not in space/T/TRUE/F/FALSE. value is " & celltext
  256.             tableInfo.columns(indexColumn).columnNotNull = False
  257.         End Select
  258.         
  259.         ' columnComment
  260.         celltext = t.Rows(i).Cells(6).Range.Text
  261.         celltext = trimCRLF(celltext)
  262.         tableInfo.columns(indexColumn).columnComment = celltext
  263.         
  264.         indexColumn = indexColumn + 1
  265.                                 'Debug.Print " " & _
  266.                                             tableInfo.columns(i).columnName & Chr(9) & _
  267.                                             tableInfo.columns(i).columnID & Chr(9) & _
  268.                                             tableInfo.columns(i).columnType & Chr(9) & _
  269.                                             tableInfo.columns(i).columnIsPK & Chr(9) & _
  270.                                             tableInfo.columns(i).columnNotNull & Chr(9) & _
  271.                                             tableInfo.columns(i).columnComment
  272.     
  273.     Next
  274.     tableInfo.columnCount = indexColumn

  275. End Sub

  276. Private Function getTableInfoFromDoc(d As Document, arrTableInfo() As t_tableInfo)
  277.     On Error Resume Next
  278.     Dim tableCount As Integer
  279.     Dim indexTable As Integer
  280.     Dim i As Integer
  281.     Dim j As Integer
  282.     'Dim t As Table
  283.     
  284.     If d Is Nothing Then
  285.         Debug.Print "[error] getTableInfoFromDoc parameter is null"
  286.         Exit Function
  287.     End If
  288.     
  289.     tableCount = d.Tables.Count
  290.     Debug.Print " document " & d.Name & " has " & tableCount & " tables"

  291.     indexTable = 0
  292.     For i = 1 To tableCount
  293.         Dim tmpTableInfo As t_tableInfo
  294.         Call getTableInfo(d.Tables(i), tmpTableInfo)
  295.         If tmpTableInfo.tableName <> "" Then
  296.             ' do not add duplicate table to array
  297.             Dim isDup As Boolean
  298.             isDup = False
  299.             For j = 0 To indexTable
  300.                 If tmpTableInfo.tableID = arrTableInfo(j).tableID Then
  301.                     isDup = True
  302.                 End If
  303.             Next
  304.             If isDup = False Then
  305.                 arrTableInfo(indexTable) = tmpTableInfo
  306.                 indexTable = indexTable + 1
  307.             Else
  308.                 Debug.Print " duplicate table found " & tmpTableInfo.tableID
  309.             End If
  310.         End If
  311.     Next
  312.     getTableInfoFromDoc = indexTable
  313. End Function


  314. Private Sub creaSQL(tableInfo As t_tableInfo)
  315.     Dim i As Integer
  316.     Dim j As Integer
  317.     Dim colInf As t_columnInfo
  318.     Dim sqlstm_drop As String
  319.     Dim sqlstm As String
  320.     Dim pkstr As String
  321.     Const PkstrIninStr = " primary key("
  322.     Const PkstrIninLen = 16

  323.     sqlstm_drop = "drop table if exists " & tableInfo.tableID & ";" & vbCrLf
  324.                             Debug.Print " [" & i & "]" & " " & sqlstm_drop

  325.     pkstr = PkstrIninStr
  326.     sqlstm = "create table " & tableInfo.tableID & "(" & vbCrLf
  327.     For j = 0 To tableInfo.columnCount - 1
  328.         colInf = tableInfo.columns(j)
  329.         sqlstm = sqlstm & " " & colInf.columnID
  330.         sqlstm = sqlstm & " " & colInf.columnType
  331.         If colInf.columnNotNull = True Then
  332.             sqlstm = sqlstm & " not null"
  333.         End If
  334.         sqlstm = sqlstm & "," & vbCrLf
  335.         
  336.         If colInf.columnIsPK = True Then
  337.             If Len(pkstr) > PkstrIninLen Then
  338.                 pkstr = pkstr & ", "
  339.             End If
  340.             pkstr = pkstr & colInf.columnID
  341.         End If
  342.     Next
  343.     pkstr = pkstr & ")" & vbCrLf
  344.     sqlstm = sqlstm & pkstr & ");" & vbCrLf
  345.                             Debug.Print " [" & i & "]" & " " & sqlstm
  346.                             
  347.     outputSQL tableInfo.tableID, sqlstm_drop & sqlstm

  348. End Sub

  349. Sub outputSQLtoFile(filename As String, sqlstm As String)
  350.     Dim fileExist As Boolean
  351.              
  352.     ' check if file exist
  353.     If Dir(filename) <> "" Then
  354.         fileExist = True
  355.                                     Debug.Print " file already exist. skip this file " & filename
  356.     Else
  357.         fileExist = False
  358.                                     Debug.Print " outputSQLtoFile file " & filename
  359.         Open filename For Output As #1
  360.         Print #1, sqlstm
  361.         Close #1

  362.     End If
  363. End Sub

  364. Sub outputSQL(sqlname As String, sqlstm As String)
  365.     Dim tmpFilename As String
  366.     tmpFilename = DEFAULT_SQL_OUTPUT_PATH & "\" & sqlname & ".sql"
  367.     outputSQLtoFile tmpFilename, sqlstm
  368. End Sub

  369. Sub mainprocess()
  370.     On Error Resume Next
  371.     Dim d As Document
  372.     Dim arrTableInfo(MAX_TABLES_IN_DOC) As t_tableInfo
  373.     Dim arrayDocFile(MAX_FILE_COUNT) As String
  374.     Dim filePath As String
  375.     Dim filename As String
  376.     Dim i As Integer
  377.     Dim j As Integer
  378.     Dim tablecnt As Integer
  379.     
  380.     Debug.Print "mainprocess start"
  381.     'Application.ScreenUpdating = False
  382.     
  383.     ' get filenames
  384.     filePath = DEFAULT_FILEPATH
  385.     filename = Dir(filePath & "*.doc?")
  386.     i = 0
  387.     Do While filename <> "" And i < MAX_FILE_COUNT
  388.         filename = Dir
  389.                                             Debug.Print filePath & filename
  390.         If filename = "" Then
  391.             Exit Do
  392.         End If
  393.         arrayDocFile(i) = filePath & filename
  394.         i = i + 1
  395.     Loop

  396.     i = 0
  397.     Do While arrayDocFile(i) <> ""
  398.         Set d = opendoc(arrayDocFile(i))
  399.         ' clear array per doc
  400.         Erase arrTableInfo
  401.         tablecnt = 0
  402.         tablecnt = getTableInfoFromDoc(d, arrTableInfo)
  403.                                         'MsgBox "hello"
  404.                                         'getTableInfo ActiveDocument.Tables(1), tableInfo
  405.         closedoc d
  406.         Set d = Nothing
  407.         'Debug.Print "Documents.Count=" & Documents.Count
  408.         
  409.         For j = 0 To tablecnt - 1
  410.             creaSQL arrTableInfo(j)
  411.         Next
  412.         i = i + 1
  413.     Loop
  414.     'Application.ScreenUpdating = True
  415.     
  416.     Debug.Print "mainprocess end


阅读(1667) | 评论(0) | 转发(0) |
0

上一篇:c守护进程

下一篇:mysql笔记-忘记root密码

给主人留下些什么吧!~~