使用VBA将xml文件转换为xlsx文件

时间:2016-07-26 10:46:15

标签: xml excel vba excel-vba xlsx

如何将 xml 文件转换为 xlsx

文件夹中存在的xml个文件,我想将xlsx个文件保存到其他文件夹中。

示例:

现有文件: C:\Users\John\Documents\xml\data.xml
转换为: C:\Users\John\Documents\xlsx\data.xlsx

2 个答案:

答案 0 :(得分:0)

这是你想要做的吗?

Option Explicit
Public Sub XML_To_xlsx()
    Dim FilePath As String
    Dim Book As Workbook

'   Load XML Data to New Workbook
    FilePath = "C:\Users\John\Documents\xml\data.xml"
    Set Book = Workbooks.OpenXML(FilePath)

   'Copy to active Worksheet
    Book.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets("Sheet1").Range("A1")

    'Close New Workbook
    Book.Close False
End Sub

答案 1 :(得分:0)

我做到了。

Public Sub ConvertXmlToXlsx()

Application.DisplayAlerts = False

Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object

xmlFolder = "C:\Users\John\Documents\xml\"
convFolder = "C:\Users\John\Documents\xls\"


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(xmlFolder)
For Each objFile In objFolder.Files
    If UCase(Right(objFile.Name, Len(XML))) = UCase(XML) Then
        NewFileName = convFolder & objFile.Name & "_conv.xlsx"

        Set ConvertThis = Workbooks.Open(objFolder & "\" & objFile.Name)
        ConvertThis.SaveAs Filename:=NewFileName, FileFormat:= _
        xlOpenXMLWorkbook
        ConvertThis.Close
    End If
Next objFile

End Sub