【问题标题】:Export xml from Excel从 Excel 导出 xml
【发布时间】:2011-06-02 18:29:13
【问题描述】:

我想从 Excel 表中导出 xml 但列数不固定。 我想我需要xsd 谁能分享一下xsd

【问题讨论】:

    标签: xml excel xsd


    【解决方案1】:

    这就是我学会使用 VBA 的方法,非常简单和灵活......

    检查:

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

    在 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
    ...
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      相关资源
      最近更新 更多