| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

Excel VBA ワークシートをHTMLテーブル

提供: MyMemoWiki
ナビゲーションに移動 検索に移動

Excel VBA

Excel VBA ワークシートをHTMLテーブル

  1. Option Explicit
  2. Public Function MakeHtmlTable(table As Range, Optional headerType As String = "r", Optional headerSize As Integer = 1, Optional cls As String = "") As String
  3.  
  4. Dim buf As String
  5. Dim cl As Range
  6. Dim rowPos As Integer
  7. Dim isFirstRow As Boolean
  8. Dim celVal As String
  9. Dim colPos As Integer
  10. Dim isColHeader As Boolean
  11. Dim isRowHeader As Boolean
  12. Dim celTag As String
  13. If Trim(cls) <> "" Then
  14. cls = " class=""" & cls & """ "
  15. Else
  16. cls = " border=""1"" "
  17. End If
  18. isColHeader = InStr(headerType, "c") > 0
  19. isRowHeader = InStr(headerType, "r") > 0
  20. isFirstRow = True
  21. rowPos = -1
  22. buf = "<table " & cls & ">"
  23. For Each cl In table
  24. If rowPos <> cl.Row Then
  25. If Not isFirstRow Then
  26. buf = buf & "</tr>"
  27. End If
  28. buf = buf & "<tr>"
  29. isFirstRow = False
  30. colPos = 0
  31. End If
  32. celVal = EscapeHtmlSpecial(cl.text)
  33. If Trim(celVal) = "" Then
  34. celVal = " "
  35. End If
  36. rowPos = cl.Row
  37. If (isColHeader And headerSize > colPos) Or _
  38. (isRowHeader And headerSize > rowPos) Then
  39. celTag = "th"
  40. Else
  41. celTag = "td"
  42. End If
  43. buf = buf & EncloseTag(celVal, celTag)
  44. colPos = colPos + 1
  45. Next
  46. buf = buf & "</table>"
  47. MakeHtmlTable = buf
  48. End Function
  49. Public Function EncloseTag(value As String, tag As String)
  50. EncloseTag = "<" & tag & ">" & value & "</" & tag & ">"
  51. End Function
  52. Public Function EscapeHtmlSpecial(text As String) As String
  53. Dim buf As String
  54. Dim c As String
  55. Dim i As Integer
  56. For i = 1 To Len(text)
  57. c = Mid(text, i, 1)
  58. Select Case c
  59. Case "&"
  60. c = "&"
  61. Case "<"
  62. c = "<"
  63. Case ">"
  64. c = ">"
  65. Case """"
  66. c = "'"""
  67. End Select
  68. buf = buf & c
  69. Next
  70. EscapeHtmlSpecial = buf
  71. End Function
  72.  
  • 使用例
  1. =MakeHtmlTable(B2:C7, "r", 1, "wikitable")
  • 結果
プロパティ内容
axis(UIStackViewのみ) スタックビューの方向を定義し、垂直または水平のどちらかを指定します
orientationスタックビューの方向を指定します。(NSStackView のみ) スタックビューの方向を、垂直または水平に定義
distribution軸に沿ったビューのレイアウトを定義
alignment スタックビューの軸に垂直なビューのレイアウトを定義
spacing 隣接するビューの間のスペースを定義します。