代码之家  ›  专栏  ›  技术社区  ›  Eitel Dagnin

VBS-通过多个循环。csv文件,并将文件转换为。xlsx

  •  0
  • Eitel Dagnin  · 技术社区  · 8 年前

    我成功地将以下代码组合在一起:

    'Constants
    Const xlOpenXMLWorkbook = 51             '(without macro's in 2007-2016, xlsx)
    Const xlOpenXMLWorkbookMacroEnabled = 52 '(with or without macro's in 2007-2016, xlsm)
    Const xlExcel12 = 50                     '(Excel Binary Workbook in 2007-2016 with or without macro's, xlsb)
    Const xlExcel8 =56                       '(97-2003 format in Excel 2007-2016, xls)
    
    ' Extensions for old and new files
    strExcel = "xlsx"
    strCSV = "csv"
    
    ' Set up filesystem object for usage
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    ' Get folder name to process off the command line, make sure it's valid
    If (WScript.Arguments.Count > 0) Then
        strFolder = WScript.Arguments(0)
        If Not objFSO.FolderExists(strFolder) Then
            WScript.StdErr.WriteLine "Specified folder does not exist."
            WScript.Quit
        End If
    Else
        WScript.StdErr.WriteLine "No folder name specified to process."
        WScript.Quit
    End If
    
    ' Access the folder to process
    Set objFolder = objFSO.GetFolder(strFolder)
    
    ' Load Excel (hidden) for conversions
    Set objExcel = CreateObject("Excel.Application")
    objExcel.Visible = False
    objExcel.DisplayAlerts = False
    
    ' Process all files
    For Each objFile In objFolder.Files
        ' Get full path to file
        strPath = objFile.Path
        ' Only convert CSV files
        If LCase(objFSO.GetExtensionName(strPath)) = LCase(strCSV) Then
            ' Display to console each file being converted
            WScript.Echo "Converting """ & strPath & """"
            ' Load CSV into Excel and save as native Excel file
            Set objWorkbook = objExcel.Workbooks.Open(strPath, False, True)
            objWorkbook.SaveAs Replace(strPath, strCSV, strExcel), xlOpenXMLWorkbook
            objWorkbook.Close False
            Set objWorkbook = Nothing
        End If
    Next
    
    'Wrap up
    objExcel.Quit
    Set objExcel = Nothing
    Set objFSO = Nothing 
    

    不幸的是,我有3个问题:

    1. 我被指示按以下方式运行此操作:
      复制上面的代码并将其另存为 csv.vbs
      转到CMD并键入

      cscript csv.vbs "C:\Users\Eitel\Desktop\3rd Party\Work Folder"
      

      这是CSV文件所在的路径。

      我更喜欢通过单击/打开VBScript来执行代码。

    2. 我收到此错误:

      输入错误:找不到脚本文件“C:\Users\Eitel\csv.vbs”

      我去了 “C:\Users\Eitel\csv.vbs” 并粘贴 csv。vbs 文件位于此位置。我再次运行了命令,显示如下:

      “C:\Users\Eitel\Desktop\第三方\Work Folder\TestFile.CSV”
      C: \用户\Eitel\csv。vbs(44.9)Microsoft Excel:无法另存为该名称。文档以只读方式打开。

      我不知道这意味着什么,也不知道为什么会发生?

    3. 我注意到,虽然大多数文件都是。csv扩展名,一些文件扩展名显示为。CSV和一些是。csv。我想知道这是否会影响脚本的执行方式?

    1 回复  |  直到 8 年前
        1
  •  0
  •   Eitel Dagnin    8 年前

    以下是我需要的解决方案:

    链接: https://www.experts-exchange.com/questions/29088597/Change-Multiple-csv-files-into-xlsx-files.html?notificationFollowed=205599875

    'Constants 
    Const xlOpenXMLWorkbook = 51             '(without macro's in 2007-2016, xlsx)
    Const xlOpenXMLWorkbookMacroEnabled = 52 '(with or without macro's in 2007-2016, xlsm)
    Const xlExcel12 = 50                     '(Excel Binary Workbook in 2007-2016 with or without macro's, xlsb)
    Const xlExcel8 =56                       '(97-2003 format in Excel 2007-2016, xls)
    
    ' Extensions for old and new files
    strExcel = "xlsx"
    strCSV = "csv"
    strXLS = "xls"
    
    ' Set up filesystem object for usage
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    strFolder = "B:\EE\EE29088597\Files"
    
    ' Access the folder to process
    Set objFolder = objFSO.GetFolder(strFolder)
    
    ' Load Excel (hidden) for conversions
    Set objExcel = CreateObject("Excel.Application")
    objExcel.Visible = False
    objExcel.DisplayAlerts = False
    
    ' Process all files
    For Each objFile In objFolder.Files
        ' Get full path to file
        strPath = objFile.Path
        ' Only convert CSV files
        If LCase(objFSO.GetExtensionName(strPath)) = LCase(strCSV) Or LCase(objFSO.GetExtensionName(strPath)) = LCase(strXLS) Then
            ' Display to console each file being converted
            Wscript.Echo "Converting """ & strPath & """"
            ' Load CSV into Excel and save as native Excel file
            Set objWorkbook = objExcel.Workbooks.Open(strPath, False, True)
            strNewPath = objFSO.GetParentFolderName(strPath) & "\" & objFSO.GetBaseName(strPath) & "." & strExcel
            objWorkbook.SaveAs strNewPath, xlOpenXMLWorkbook
            objWorkbook.Close False
            Set objWorkbook = Nothing
        End If
    Next
    
    'Wrap up
    objExcel.Quit
    Set objExcel = Nothing
    Set objFSO = Nothing
    

    这将扫描目录中的任何xls或csv文件,并将其转换为xlsx文件。