1

我想将 excel 文件转换为制表符限制的 txt 文件(不带回车)。目前我正在使用将大量 excel 文件转换为 .txt 文件的脚本(在这个论坛中找到)。

脚本是

    ' @file:    xl2tab.vbs
' @author:  stephen brown - sb09d@fsu.edu
' @date:    2009-Dec-10
' 
' @description: mass convert excel files to tab-delimited files
'
' @usage:   place in top-level directory where excel files are contained and double-click.
'           script will recursively access all subdirectories and convert each excel file to
'           tab delimited file. All output will be in "output" folder, which retains structure
'           of original directories

Dim saveDirBase

set fso = CreateObject("Scripting.FileSystemObject")
set shell = CreateObject("WScript.Shell")
set objExcel = CreateObject("Excel.Application")

set top = fso.GetFolder(shell.CurrentDirectory)
saveDirBase = top & "\" & "output"

Sub TraverseFolders(path)
    set folder = fso.GetFolder(path)

    XL2Tab(folder)

    For each item in folder.SubFolders
        If item.Path <> saveDirBase Then
            Call TraverseFolders(item.Path)
        End If
    Next

    set folder = Nothing
End Sub

Sub XL2Tab(folder)
    Dim saveDir
    set files = folder.Files

    If folder.Name <> top.Name Then
        saveDir = saveDirBase & "\" & folder.Name
    Else
        saveDir = saveDirBase
    End If

    If fso.FolderExists(saveDir) = False Then
        fso.CreateFolder(saveDir)   
    End If

    For each file In files
        If file.Name <> Wscript.ScriptName Then
            objExcel.Application.DisplayAlerts = False
            Set objWorkbook = objExcel.Workbooks.open(folder.Path & "\" & file.Name)   
            objWorkbook.SaveAs saveDir & "\" & file.Name & ".txt", -4158    
            objWorkbook.close
            objExcel.Application.DisplayAlerts = True
        End If
    Next
End Sub

If fso.FolderExists(saveDirBase) = False Then
    fso.CreateFolder(saveDirBase)   
End If

Call TraverseFolders(top)

在转换之前,我想删除每个 excel 文件中的回车符。

请任何人指导我...!

4

1 回答 1

1

嗨,如果您在转换文件后尝试删除回车,请将以下过程添加到代码中

 sub RemoveCarriage(FileN)
 Const ForReading = 1
 Const ForWriting = 2
 Set objFSO = CreateObject("Scripting.FileSystemObject")
 Set objFile = objFSO.OpenTextFile(FileN, ForReading)
 strText = objFile.ReadAll
 objFile.Close
 strNewText = Replace(strText, chr(013) & chr(010), "") 
 ' chr(010) = line feed chr(013) = carriage return
 Set objFile = objFSO.OpenTextFile(FileN, ForWriting)
 objFile.WriteLine strNewText
 objFile.Close 
 End sub

在关闭工作簿 objWorkbook.close 之后调用过程的 forloop 内的模块

 RemoveCarriage(file.Name & ".txt")
于 2013-06-21T12:32:07.970 回答