Convert XLS to CSV on command line

后端 未结 15 2095
臣服心动
臣服心动 2020-11-22 12:11

How could I convert an XLS file to a CSV file on the windows command line.

The machine has Microsoft Office 2000 installed. I\'m open to installing OpenOffice if it\

相关标签:
15条回答
  • 2020-11-22 12:42

    :: For UTF-8 works for Microsoft Office 2016 and higher!

    Try this code:

    if WScript.Arguments.Count < 2 Then
        WScript.Echo "Please specify the source and the destination files. Usage: ExcelToCsv <xls/xlsx source file> <csv destination file>"
        Wscript.Quit
    End If
    
    csv_format = 62
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
    dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))
    
    
    Dim oExcel
    Set oExcel = CreateObject("Excel.Application")
    
    Dim oBook
    Set oBook = oExcel.Workbooks.Open(src_file)
    
    oBook.SaveAs dest_file, csv_format
    
    oBook.Close False
    oExcel.Quit
    
    0 讨论(0)
  • 2020-11-22 12:43

    Open Notepad, create a file called XlsToCsv.vbs and paste this in:

    if WScript.Arguments.Count < 2 Then
        WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
        Wscript.Quit
    End If
    Dim oExcel
    Set oExcel = CreateObject("Excel.Application")
    Dim oBook
    Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
    oBook.SaveAs WScript.Arguments.Item(1), 6
    oBook.Close False
    oExcel.Quit
    WScript.Echo "Done"
    

    Then from a command line, go to the folder you saved the .vbs file in and run:

    XlsToCsv.vbs [sourcexlsFile].xls [destinationcsvfile].csv
    

    This requires Excel to be installed on the machine you are on though.

    0 讨论(0)
  • 2020-11-22 12:44

    Create a TXT file on your desktop named "xls2csv.vbs" and paste the code:

    Dim vExcel
    Dim vCSV
    Set vExcel = CreateObject("Excel.Application")
    Set vCSV = vExcel.Workbooks.Open(Wscript.Arguments.Item(0))
    vCSV.SaveAs WScript.Arguments.Item(0) & ".csv", 6
    vCSV.Close False
    vExcel.Quit
    

    Drag a XLS file to it (like "test.xls"). It will create a converted CSV file named "test.xls.csv". Then, rename it to "test.csv". Done.

    0 讨论(0)
  • 2020-11-22 12:45

    A slightly modified version of ScottF answer, which does not require absolute file paths:

    if WScript.Arguments.Count < 2 Then
        WScript.Echo "Please specify the source and the destination files. Usage: ExcelToCsv <xls/xlsx source file> <csv destination file>"
        Wscript.Quit
    End If
    
    csv_format = 6
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
    dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))
    
    Dim oExcel
    Set oExcel = CreateObject("Excel.Application")
    
    Dim oBook
    Set oBook = oExcel.Workbooks.Open(src_file)
    
    oBook.SaveAs dest_file, csv_format
    
    oBook.Close False
    oExcel.Quit
    

    I have renamed the script ExcelToCsv, since this script is not limited to xls at all. xlsx Works just fine, as we could expect.

    Tested with Office 2010.

    0 讨论(0)
  • 2020-11-22 12:49

    How about with PowerShell?

    Code should be looks like this, not tested though

    $xlCSV = 6
    $Excel = New-Object -Com Excel.Application 
    $Excel.visible = $False 
    $Excel.displayalerts=$False 
    $WorkBook = $Excel.Workbooks.Open("YOUDOC.XLS") 
    $Workbook.SaveAs("YOURDOC.csv",$xlCSV) 
    $Excel.quit()
    

    Here is a post explaining how to use it

    How Can I Use Windows PowerShell to Automate Microsoft Excel?

    0 讨论(0)
  • 2020-11-22 12:49

    All of these answers helped me construct the following script which will automatically convert XLS* files to CSV and vice versa, by dropping one or more files on the script (or via command line). Apologies for the janky formatting.

    ' https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line
    ' https://gist.github.com/tonyerskine/77250575b166bec997f33a679a0dfbe4
    
    ' https://stackoverflow.com/a/36804963/1037948
    '* Global Settings and Variables
    Set args = Wscript.Arguments
    
    For Each sFilename In args
        iErr = ConvertExcelFormat(sFilename)
        ' 0 for normal success
        ' 404 for file not found
        ' 10 for file skipped (or user abort if script returns 10)
    Next
    
    WScript.Quit(0)
    
    Function ConvertExcelFormat(srcFile)
    
        if IsEmpty(srcFile) OR srcFile = "" Then
            WScript.Echo "Error! Please specify at least one source path. Usage: " & WScript.ScriptName & " SourcePath.xls*|csv"
            ConvertExcelFormat = -1
            Exit Function
            'Wscript.Quit
        End If
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
    
        srcExt = objFSO.GetExtensionName(srcFile)
    
        ' the 6 is the constant for 'CSV' format, 51 is for 'xlsx'
        ' https://msdn.microsoft.com/en-us/vba/excel-vba/articles/xlfileformat-enumeration-excel
        ' https://www.rondebruin.nl/mac/mac020.htm
        Dim outputFormat, srcDest
    
        If LCase(Mid(srcExt, 1, 2)) = "xl" Then
            outputFormat = 6
            srcDest = "csv"
        Else
            outputFormat = 51
            srcDest = "xlsx"
        End If
    
        'srcFile = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
        srcFile = objFSO.GetAbsolutePathName(srcFile)
        destFile = Replace(srcFile, srcExt, srcDest)
    
        Dim oExcel
        Set oExcel = CreateObject("Excel.Application")
        Dim oBook
        Set oBook = oExcel.Workbooks.Open(srcFile)
        ' preserve formatting? https://stackoverflow.com/a/8658845/1037948
        'oBook.Application.Columns("A:J").NumberFormat = "@"
        oBook.SaveAs destFile, outputFormat
        oBook.Close False
        oExcel.Quit
        WScript.Echo "Conversion complete of '" & srcFile & "' to '" & objFSO.GetFileName(destFile) & "'"
    
    End Function
    
    0 讨论(0)
提交回复
热议问题