VBS - 将制表符分隔的CSV文件转换为XLSX

时间:2018-05-14 12:08:27

标签: excel csv vbscript

我之前发布了一个类似的问题,然后将CSV文件转换为xlsx。

这个问题可以在这里找到:

VBS - Loop through multiple .csv files in a folder and convert the files to .xlsx

代码:VBS

'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

问题是以上内容对我需要转换为xlsx的当前CSV文件无效..

从我所看到的,上面的代码以逗号分隔的方式读取CSV文件,但我当前使用的文件是制表符分隔的。

下面是我在excel中记录的宏,因为我对文件进行了数据导入,虽然我可以重用此代码在excel中进行转换,但我宁愿在此之前在脚本中执行此操作。 / p>

下面是代码输出和预期输出的屏幕截图:

第1行:当前输出

第2行:预期输出

enter image description here

代码:VBA

Sub Import_CSV()
'
' Import_CSV Macro
'

'
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;Location\test.csv", Destination _
        :=Range("$A$1"))
        .CommandType = 0
        .Name = "test"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
        1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

在此处找到答案:

https://www.experts-exchange.com/questions/29028367/CSV-to-Excel-specific-delimiter-Vbscript.html

Const xlTextQualifierDoubleQuote = 1
Const xlInsertDeleteCells = 1
Const xlDelimited = 1

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts= False

strCSVfile = "\input.csv"
strXLSfile = "\input.xlsx"

Set objWorkbook = objExcel.Workbooks.Add
Set objWorkSheet = objWorkbook.Worksheets(1)
With objWorkSheet.QueryTables.Add("TEXT;" & strCSVfile, objWorkSheet.Range("$A$1"))
    .Name = "input"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 437
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = False
    .TextFileOtherDelimiter = "#"
    .TextFileColumnDataTypes = Array(1, 1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh False
End With

'Save Spreadsheet, 51 = Excel 2007-2010
objWorkSheet.SaveAs strXLSfile, 51

'Release Lock on Spreadsheet
objExcel.Quit()
Set ObjExcel = Nothing

这适用于大多数分隔的' csv /文本文件。

我所做的就是将.TextFileOtherDelimiter = "#"更改为.TextFileOtherDelimiter = False,将.TextFileTabDelimiter = False更改为.TextFileTabDelimiter = True