从Excel导出xml

时间:2011-04-15 10:57:02

标签: xml excel xsd

我想从Excel表格中导出xml 但列数不固定。 我想我需要xsd 任何人都可以共享xsd

1 个答案:

答案 0 :(得分:1)

这就是我学习使用VBA,简单灵活的方法......

检查:

    'lastCol = Range("a1").End(xlToRight).Column

in Sub kurssitToXML(Kurssi =芬兰语课程):

Sub kurssitToXML()
    Dim Filename As Variant
    Dim Rng As Range
    Dim r As Long, c As Long
    Dim dRetVal As Variant

    Worksheets("Kurssiluettelo").Activate
    'Set the range

    ' IS THIS WHAT YOU ARE LOOKING FOR?
    'lastCol = Range("a1").End(xlToRight).Column
    'lastRow = Cells(65536, lastCol).End(xlUp).Row
    'Rng = Range("a1", Cells(lastRow, lastCol))

    lastCol = Range("a1").End(xlToRight).Column
    lastRow = Range("a1").End(xlDown).Row
    Set Rng = Range("a1", Cells(lastRow, lastCol))

    '   Get a file name
    Filename = Application.GetSaveAsFilename( _
        InitialFileName:="d:\kurssit.xml", _
        fileFilter:="XML Files(*.xml), *.xml")
    If Filename = False Then Exit Sub

'   Open the text file
    Open Filename For Output As #1

'   Write the <xml> tags
    Print #1, "<?xml version=""1.0"" encoding=""ISO-8859-1"" standalone=""yes""?>"
    Print #1, "<KurssitList xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">"

'   Loop through the cells
    For r = 2 To Rng.Rows.Count
        Print #1, "<Kurssi>"
        For c = 1 To Rng.Columns.Count
            Print #1, "<" & Rng.Cells(1, c) & ">";
            If IsDate(Rng.Cells(r, c)) Then
                Print #1, Format(Rng.Cells(r, c), "dd.mm.yyyy");
            Else
                Print #1, Rng.Cells(r, c).Text;
            End If
            Print #1, "</" & Rng.Cells(1, c) & ">"
        Next c
        Print #1, "</Kurssi>"
    Next r
'   Close the table
    Print #1, "</KurssitList>"

'   Close the file
    Close #1
...

我希望这会有所帮助。