How to automate converting Excel xls files to Excel xml format?

后端 未结 6 1746
渐次进展
渐次进展 2020-12-19 13:05

I have about 200 Excel files that are in standard Excel 2003 format.

I need them all to be saved as Excel xml - basically the same as opening each file and choosing

相关标签:
6条回答
  • 2020-12-19 13:31

    Here is a routine that will convert all files in a single directory that have a .xls extension.

    It takes a straight forward approach. Any VBA code in a workbook is stripped out, the workbook is not saved with a .xlsm extension. Any incompatability warning are not dislayed, instead the changes are automatically accepted.

    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
    
    0 讨论(0)
  • 2020-12-19 13:32

    Sounds like a job for my favorite-most-underrated language of all time: VBScript!!

    Put this in a text file, and make the extension ".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
    

    I haven't tested this, but it should work

    0 讨论(0)
  • 2020-12-19 13:34
    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 讨论(0)
  • 2020-12-19 13:36

    You could adapt the code I posted here:

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

    It shows how to save as PDF (Word is shown in the blog, but if you download the solution, it has code for Excel and PPT).

    You need to find the function for saving as the new format instead of exporting (probably the easiest way is to record a macro of yourself doing it in Excel and then looking at the code).

    0 讨论(0)
  • 2020-12-19 13:42

    The simplest way is to record macro for one file and then manually edit macros to do such actions for files in folder using loop. In macro you can use standart VB functions to get all files in directory and to filter them. You can look http://www.xtremevbtalk.com/archive/index.php/t-247211.html for additional information.

    0 讨论(0)
  • 2020-12-19 13:47

    Open them all up, and then press ALT+F11 to get to macro editor and enter something like:

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

    And then press F5 to run it. May need some tweaking as I haven't tested it.

    0 讨论(0)
提交回复
热议问题