Convert XLS to CSV on command line

后端 未结 15 2096
臣服心动
臣服心动 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:53

    Here is a version that will handle multiple files drag and dropped from windows. Based on the above works by

    Christian Lemer
    plang
    ScottF
    

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

    '* Usage: Drop .xl* files on me to export each sheet as CSV
    
    '* Global Settings and Variables
    Dim gSkip
    Set args = Wscript.Arguments
    
    For Each sFilename In args
        iErr = ExportExcelFileToCSV(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 ExportExcelFileToCSV(sFilename)
        '* Settings
        Dim oExcel, oFSO, oExcelFile
        Set oExcel = CreateObject("Excel.Application")
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        iCSV_Format = 6
    
        '* Set Up
        sExtension = oFSO.GetExtensionName(sFilename)
        if sExtension = "" then
            ExportExcelFileToCSV = 404
            Exit Function
        end if
        sTest = Mid(sExtension,1,2) '* first 2 letters of the extension, vb's missing a Like operator
        if not (sTest =  "xl") then
            if (PromptForSkip(sFilename,oExcel)) then
                ExportExcelFileToCSV = 10
                Exit Function
            end if
        End If
        sAbsoluteSource = oFSO.GetAbsolutePathName(sFilename)
        sAbsoluteDestination = Replace(sAbsoluteSource,sExtension,"{sheet}.csv")
    
        '* Do Work
        Set oExcelFile = oExcel.Workbooks.Open(sAbsoluteSource)
        For Each oSheet in oExcelFile.Sheets
            sThisDestination = Replace(sAbsoluteDestination,"{sheet}",oSheet.Name)
            oExcelFile.Sheets(oSheet.Name).Select
            oExcelFile.SaveAs sThisDestination, iCSV_Format
        Next
    
        '* Take Down
        oExcelFile.Close False
        oExcel.Quit
    
        ExportExcelFileToCSV = 0
        Exit Function
    End Function
    
    Function PromptForSkip(sFilename,oExcel)
        if not (VarType(gSkip) = vbEmpty) then
            PromptForSkip = gSkip
            Exit Function
        end if
    
        Dim oFSO
        Set oFSO = CreateObject("Scripting.FileSystemObject")
    
        sPrompt = vbCRLF & _
            "A filename was received that doesn't appear to be an Excel Document." & vbCRLF & _
            "Do you want to skip this and all other unrecognized files?  (Will only prompt this once)" & vbCRLF & _
            "" & vbCRLF & _
            "Yes    - Will skip all further files that don't have a .xl* extension" & vbCRLF & _
            "No     - Will pass the file to excel regardless of extension" & vbCRLF & _
            "Cancel - Abort any further conversions and exit this script" & vbCRLF & _
            "" & vbCRLF & _
            "The unrecognized file was:" & vbCRLF & _
            sFilename & vbCRLF & _
            "" & vbCRLF & _
            "The path returned by the system was:" & vbCRLF & _
            oFSO.GetAbsolutePathName(sFilename) & vbCRLF
    
        sTitle = "Unrecognized File Type Encountered"
    
        sResponse =  MsgBox (sPrompt,vbYesNoCancel,sTitle)
        Select Case sResponse
        Case vbYes
            gSkip = True
        Case vbNo
            gSkip = False
        Case vbCancel
            oExcel.Quit
            WScript.Quit(10)    '*  10 Is the error code I use to indicate there was a user abort (1 because wasn't successful, + 0 because the user chose to exit)
        End Select
    
        PromptForSkip = gSkip
        Exit Function
    End Function
    
    0 讨论(0)
  • 2020-11-22 12:55

    Scott F's answer is the best I have found on the internet. I did add on to his code to meet my needs. I added:

    On Error Resume Next <- To account for a missing xls files in my batch processing at the top. oBook.Application.Columns("A:J").NumberFormat = "@" <- Before the SaveAs line to make sure my data is saved formatted as text to keep excel from deleting leading zero's and eliminating commas in number strings in my data i.e. (1,200 to 1200). The column range should be adjusted to meet your neeeds (A:J).

    I also removed the Echo "done" to make it non interactive.

    I then added the script into a cmd batch file for processing automated data on an hourly basis via a task.

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

    You can do it with Alacon - command-line utility for Alasql database. It works with Node.js, so you need to install Node.js and then Alasql package.

    To convert Excel file to CVS (ot TSV) you can enter:

    > node alacon "SELECT * INTO CSV('mydata.csv', {headers:true}) FROM XLS('mydata.xls', {headers:true})"
    

    By default Alasql converts data from "Sheet1", but you can change it with parameters:

    {headers:false, sheetid: 'Sheet2', range: 'A1:C100'}
    

    Alacon supports other type of conversions (CSV, TSV, TXT, XLSX, XLS) and SQL language constructions (see User Manual for examples).

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