2013-06-21 3 views
1

Excel 변환 파일을 탭 제한 txt 파일 (캐리지 리턴이없는 파일)로 변환하려고합니다. 현재 나는 대량 엑셀 파일을 .txt 파일로 변환하는 스크립트 (이 포럼에서 발견됨)를 사용하고 있습니다.excel 파일을 txt 파일로 변환 (캐리지 리턴이없는 경우)

스크립트는

' @file: xl2tab.vbs 
' @author: stephen brown - [email protected] 
' @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에서 캐리지 리턴을 제거하려면 변환하기 전에이다.

나를 안내 해주세요 ...!

+0

왜 캐리지 리턴을 제거 하시겠습니까? 모든 행을 한 행에 결합하고 싶습니까? 아니면 리눅스에서 사용하기 위해 파일을 내보내려고하십니까? –

+0

.txt 파일을 MySQL 데이터베이스에 업로드하려고합니다. 캐리지 리턴이 포함되어 있으면 파일을 Database에로드하지 않습니다. 참고 Excel 파일은 수동으로 입력되며 일부 셀 값에는 캐리지 리턴이 있습니다. – CarlJohn

+0

데이터를 가져 오는 방법에 문제가있을 수 있습니다. MySQL은 텍스트 데이터에서 캐리지 리턴과 관련된 문제가 없습니다. 반면에 캐리지 리턴과 텍스트를 연결하여 SQL 문을 작성하려고하면 결과 문이 올바르지 않습니다. 이것은 sql injection 공격과 비슷합니다.이 공격은 텍스트 데이터의 잘못된 문자로 인해 임의의 명령문이 실행됩니다. 언어에 따라 매개 변수화 된 쿼리를 사용하여 텍스트 데이터를 단순한 데이터로 전달할 수 있습니다. 도구를 사용하는 경우 원시 데이터 –

답변

1

안녕 당신은 통합 문서 objWorkbook.close

을 닫은 후 파일이

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 

호출 프로 시저의 forloop 내부의 모듈을 코딩하는 아래의 절차를 추가 변환 한 후 캐리지 리턴을 제거하려는 경우

RemoveCarriage(file.Name & ".txt") 
+0

을 사용하여 명령문을 작성하지 않는 다른 도구를 사용해야합니다. 완벽하게 작동하지만 캐리지 리턴 값이있는 셀 값은 큰 따옴표로 묶습니다. 따옴표를 제거하고 싶습니다. (내 샘플 파일을 여기에 첨부하는 방법을 모르겠다.) – CarlJohn

+1

바꾸기 기능에서 ""(큰 따옴표)를 chr (0)으로 바꾸어보십시오. – xtechkid

관련 문제