如何自动将Excel xls文件转换为Excel xml格式?

5

我有大约200个标准的Excel 2003格式文件。

我需要将它们全部保存为Excel xml格式——基本上就是打开每个文件,选择另存为...,然后选择保存类型:XML电子表格

你知道有什么简单的自动化任务的方法吗?

6个回答

8
这是一个将单个目录中所有扩展名为 .xls 的文件转换的常规程序。
它采用了一种简单直接的方法。工作簿中的任何 VBA 代码都会被剥离,工作簿不会以 .xlsm 扩展名保存。任何不兼容警告都不会显示,而是自动接受更改。
Sub Convert_xls_Files()

Dim strFile As String
Dim strPath As String

    With Application
        .EnableEvents = False
        .DisplayAlerts = False
        .ScreenUpdating = False
    End With
'Turn off events, alerts & screen updating

        strPath = "C:\temp\excel\"
        strFile = Dir(strPath & "*.xls")
'Change the path as required

    Do While strFile <> ""
        Workbooks.Open (strPath & strFile)
        strFile = Mid(strFile, 1, Len(strFile) - 4) & ".xlsx"
        ActiveWorkbook.SaveAs Filename:=strPath & strFile, FileFormat:=xlOpenXMLWorkbook
        ActiveWorkbook.Close True
        strFile = Dir
    Loop
'Opens the Workbook, set the file name, save in new format and close workbook

    With Application
        .EnableEvents = True
        .DisplayAlerts = True
        .ScreenUpdating = True
    End With
'Turn on events, alerts & screen updating

End Sub

1
谢谢Robert,它运行得非常好。我唯一改变的是FileFormat:=XlFileFormat.xlXMLSpreadsheet(我正在使用Excel 2003) - kristof

3

感谢Lou提供答案并纠正问题中的文件扩展名。如果需要更全面的解决方案,我可能会考虑您的建议。目前,对于VBA,我还可以接受。 - kristof

3

打开所有文件,然后按ALT+F11进入宏编辑器,输入如下内容:

Sub SaveAllAsXml()
    Dim wbk As Workbook
    For Each wbk In Application.Workbooks
        wbk.SaveAs FileFormat:=XlFileFormat.xlXMLSpreadsheet
    Next
End Sub

然后按F5运行它。可能需要进行一些微调,因为我还没有测试过。


1
听起来像是我有史以来最喜欢、最被低估的语言 VBScript 的工作!

将这段代码放入一个文本文件中,并将扩展名命名为 ".vbs":
set xlapp = CreateObject("Excel.Application")
set fso = CreateObject("scripting.filesystemobject")
set myfolder = fso.GetFolder("YOURFOLDERPATHHERE")
set myfiles = myfolder.Files
for each f in myfiles
   set mybook = xlapp.Workbooks.Open(f.Path)
   mybook.SaveAs f.Name & ".xml", 47
   mybook.Close
next

我还没有测试过,但它应该可以工作


0
Const xlXLSX = 51

REM 51 = xlOpenXMLWorkbook (without macro's in 2007-2013, xlsx)
REM 52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2013, xlsm)
REM 50 = xlExcel12 (Excel Binary Workbook in 2007-2013 with or without macro's, xlsb)
REM 56 = xlExcel8 (97-2003 format in Excel 2007-2013, xls)

dim args
dim file
dim sFile
set args=wscript.arguments

dim wshell
Set wshell = CreateObject("WScript.Shell")

Set objExcel = CreateObject("Excel.Application")

Set objWorkbook = objExcel.Workbooks.Open( wshell.CurrentDirectory&"\"&args(0))

objExcel.DisplayAlerts = FALSE

objExcel.Visible = FALSE

objWorkbook.SaveAs wshell.CurrentDirectory&"\"&args(1), xlXLSX

objExcel.Quit

Wscript.Quit

0

最简单的方法是为一个文件记录宏,然后手动编辑宏以使用循环执行文件夹中的操作。在宏中,您可以使用标准VB函数获取目录中的所有文件并对其进行过滤。您可以查看http://www.xtremevbtalk.com/archive/index.php/t-247211.html以获取更多信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接