简体   繁体   English

从Excel导出xml

[英]Export xml from Excel

I want to export xml from the Excel table but the number of columns are not fixed. 我想从Excel表中导出xml,但列数不固定。 I think I need xsd Can anybody share the xsd 我想我需要xsd任何人都可以共享xsd

This is how I learned to do it with VBA, verý simple and flexible... 这就是我学会了使用VBA做到这一点的方法,非常简单而灵活...

Check: 校验:

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

in Sub kurssitToXML (Kurssi = Courses in Finnish ): 在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
...

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM