开发者

Export xml from Excel

开发者 https://www.devze.com 2023-02-25 05:51 出处:网络
I want to export xml from the Excel table but the number of columns are not fixed. I think I need xsd

I want to export xml from the Excel table but the number of columns are not fixed. I think I need xsd Can anybody share the开发者_StackOverflow社区 xsd


This is how I learned to do it with VBA, verý simple and flexible...

Check:

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

in Sub kurssitToXML (Kurssi = Courses in Finnish ):

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消