2012-03-04 89 views
6

有没有办法将Excel表格保存为XML?我有XML Schema文件...和表格中的一些数据......并且我在Excel中有另存为XML文件选项,但是我可以将文件从VBA保存为XML吗?我想自动化一个流程,但我没有找到这个选项。谢谢!保存为XML与VBA?

+1

像这样的事情? http://stackoverflow.com/questions/2766760/how-to-generate-xml-from-an-excel-vba-macro – 2012-03-04 19:23:23

+0

那段代码转换表中的数据......这是好的,将是最终的解决方案,但我有XML模式文件..我想用Excel已有的选项来做所有事情...我会尝试记录宏以查看是否有一些有用的东西。 – 2012-03-04 19:27:38

+0

所以...我想要的是一个VBA代码,将加载xml scema ...将它做的事情,当我拖/放在桌子上...比使用另存为并将其另存为XML – 2012-03-04 19:28:46

回答

5

好ol宏录制救了我这段时间:))(我为什么没有使用它之前,我张贴在这里) 所以... 加载您有一个XML架构:

ActiveWorkbook.XmlMaps.Add("Book2.xml", "raport").Name _ 
     = "raport_Map" 

并将其保存为XML:

ActiveWorkbook.SaveAsXMLData Filename:="Book3.xml", _ 
     Map:=ActiveWorkbook.XmlMaps("raport_Map") 

谁曾想到,就这么简单?

+1

+1)Gr8 Job!当你自己发现某些东西的时候真的很有趣。是不是;) – 2012-03-04 19:59:27

+0

在阅读您的答案之前,我并没有意识到xml映射,并且想为这些与我相同的案例添加一条有用的信息:[在Excel 2010中创建XML映射架构](https ://msdn.microsoft.com/en-us/library/office/gg469857%28v=office.14%29.aspx?f = 255&MSPPError = -2147217396) – DrHaze 2016-01-15 13:29:53

0

此链接帮助我最 - >http://curiousmind.jlion.com/exceltotextfile

脚本的链接:

Sub MakeXML(iCaptionRow As Integer, iDataStartRow As Integer, sOutputFileName As String) 
    Dim Q As String 
    Q = Chr$(34) 

    Dim sXML As String 

    sXML = "<?xml version=" & Q & "1.0" & Q & " encoding=" & Q & "UTF-8" & Q & "?>" 
    sXML = sXML & "<rows>" 


    ''--determine count of columns 
    Dim iColCount As Integer 
    iColCount = 1 
    While Trim$(Cells(iCaptionRow, iColCount)) > "" 
     iColCount = iColCount + 1 
    Wend 

    Dim iRow As Integer 
    iRow = iDataStartRow 

    While Cells(iRow, 1) > "" 
     sXML = sXML & "<row id=" & Q & iRow & Q & ">" 

     For icol = 1 To iColCount - 1 
      sXML = sXML & "<" & Trim$(Cells(iCaptionRow, icol)) & ">" 
      sXML = sXML & Trim$(Cells(iRow, icol)) 
      sXML = sXML & "</" & Trim$(Cells(iCaptionRow, icol)) & ">" 
     Next 

     sXML = sXML & "</row>" 
     iRow = iRow + 1 
    Wend 
    sXML = sXML & "</rows>" 

    Dim nDestFile As Integer, sText As String 

    ''Close any open text files 
    Close 

    ''Get the number of the next free text file 
    nDestFile = FreeFile 

    ''Write the entire file to sText 
    Open sOutputFileName For Output As #nDestFile 
    Print #nDestFile, sXML 
    Close 
End Sub 

Sub test() 
    MakeXML 1, 2, "C:\Users\jlynds\output2.xml" 
End Sub 
+0

不幸的是,原始链接已关闭。但是你可以在这里访问2013年的捕获:https://web.archive.org/web/20130502005831/http://curiousmind.jlion.com/exceltotextfile – 2016-08-10 08:40:29