vba实现我wiki上表格的自动生成

    *  第一行必须是表头
    * 可以识别加粗字体
    * 可以识别左中右水平对齐

' CreateWikiTable:将excel表格转化成符合MediaWiki格式的html文;可以用excel进行直观的编辑,生成后直接粘贴到wiki中使用
' 使用说明:表格宽度和长度结束标志为无内容的空行或者空列,第一行为表头

Sub CreateWikiTable()
     Dim iWidth As Integer
     Dim iLength As Integer
     Dim strWiki As String

     iWidth = GetFormWidth()
     iLength = GetFormLength()

     strWiki = GetWikiFormHead(iWidth)
     strWiki = strWiki + GetWikiFormBody(iWidth, iLength)
     strWiki = strWiki + GetWikiFormTail(iWidth)

     Debug.Print strWiki

End Sub

' 返回表头代码
Function GetWikiFormHead(iWidth As Integer) As String
     Dim i As Integer
     GetWikiFormHead = "{|border=" + Chr(34) + "1" + Chr(34) + " cellspacing=" + Chr(34) + "0" + Chr(34) + Chr(10)
     For i = 1 To iWidth
         GetWikiFormHead = GetWikiFormHead + "| " + GetFormatString(1, i) + Chr(10)
     Next i
End Function

' 返回表体代码
Function GetWikiFormBody(iWidth As Integer, iLength As Integer) As String
     Dim i As Integer
     Dim j As Integer

     GetWikiFormBody = ""
     For i = 2 To iLength
         GetWikiFormBody = GetWikiFormBody + "|-" + Chr(10)
         For j = 1 To iWidth
            GetWikiFormBody = GetWikiFormBody + "| " + GetFormatString(i, j) + Chr(10)
         Next j
     Next i
End Function

' 返回表格尾部wiki代码
Function GetWikiFormTail(iWidth As Integer) As String
     GetWikiFormTail = "|}"
End Function

' 获取内容串,根据excel格式自动加上加粗、中对齐和右对齐
Function GetFormatString(iRow As Integer, iCol As Integer) As String
     GetFormatString = Cells(iRow, iCol).Text
     If (Cells(iRow, iCol).Font.Bold = True) Then
         GetFormatString = "<b>" + GetFormatString + "</b>"
     End If

     If (Cells(iRow, iCol).HorizontalAlignment = xlCenter) Then
         GetFormatString = "align=" + Chr(34) + "center" + Chr(34) + " | " + GetFormatString
     ElseIf (Cells(iRow, iCol).HorizontalAlignment = xlRight) Then
         GetFormatString = "align=" + Chr(34) + "right" + Chr(34) + " | " + GetFormatString
     End If

End Function

' 返回表格宽度
Function GetFormWidth() As Integer
     GetFormWidth = 0
     Dim i As Integer

     i = 1
     While (Cells(1, i).Text <> "")
         i = i + 1
     Wend

     GetFormWidth = i - 1
End Function

' 返回表格高度
Function GetFormLength() As Integer
     GetFormLength = 0

     Dim i As Integer

     i = 1
     While (Cells(i, 1).Text <> "")
         i = i + 1
     Wend

     GetFormLength = i - 1
End Function

你可能感兴趣的:(html,Excel,J#,VBA)