萬盛學電腦網

 萬盛學電腦網 >> Excel教程 >> excel綜合 >> Excel Vba讀取XML

Excel Vba讀取XML

  如下代碼示例的功能是,在Excel中,通過VBA代碼,讀取XML文件中的內容。

  Dim rst As ADODB.Recordset

  Dim stCon As String, stFile As String

  Dim i As Long, j As Long

  Set rst = New ADODB.Recordset

  stFile = "C:dzwebs.xml"

  stCon = "Provider=MSPersist;"

  With rst

  .CursorLocation = adUseClient

  .Open stFile, stCon, adOpenStatic, adLockReadOnly, adCmdFile

  Set .ActiveConnection = Nothing

  End With

  With ActiveSheet

  For j = 0 To i - 1

  .Cells(1, j + 1).Value = rst.Fields(j).Name

  Next j

  .Range("A2").CopyFromRecordset rst

  End With

  rst.Close

  Set rst = Nothing

copyright © 萬盛學電腦網 all rights reserved