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

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

我需要将它们全部保存为Excel xml – 基本上与打开每个文件并select另存为…然后select另存为types: XML Spreadsheet

你会知道任何简单的自动化任务吗?

这是一个例程,将转换具有.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 

你可以调整我在这里发布的代码:

http://www.atalasoft.com/cs/blogs/loufranco/archive/2008/04/01/loading-office-documents-in-net.aspx

它显示了如何保存为PDF(Word在博客中显示,但是如果您下载解决scheme,它的代码为Excel和PPT)。

你需要find保存为新格式的函数,而不是导出(可能最简单的方法是logging一下你自己在Excel中的macros,然后看看代码)。

打开它们,然后按ALT + F11进入macros编辑器并input如下所示:

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

然后按F5运行它。 可能需要一些调整,因为我没有testing它。

听起来像是我最受人们最低估的语言: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 

我没有testing过这个,但是应该可以

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

 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