2013-10-29 228 views
0

我有一個VBScript將文件夾中的任何XML文件轉換爲XLS,然後刪除XML文件 - 一切正常。VBScript將XML轉換爲CSV

但是,我知道需要將XML轉換爲CSV而不是XLS。

我需要在腳本中更改以允許此操作?簡單地改變結果文件的擴展名顯然不起作用。

Dim xlApp, xlWkb, SourceFolder,TargetFolder,file 
Set xlApp = CreateObject("excel.application") 
Set fs = CreateObject("Scripting.FileSystemObject") 

Const xlNormal=1 

SourceFolder="c:\xml-to-xls\xml" 
TargetFolder="c:\xml-to-xls\xls" 

xlApp.Visible = false 

for each file in fs.GetFolder(SourceFolder).files 
    Set xlWkb = xlApp.Workbooks.Open(file) 
    BaseName= fs.getbasename(file) 
    FullTargetPath=TargetFolder & "\" & BaseName & ".xls" 
    xlWkb.SaveAs FullTargetPath, xlNormal 
    xlWkb.close 
next 

fs.DeleteFile("C:\xml-to-xls\xml\*.xml") 

Set xlWkb = Nothing 
Set xlApp = Nothing 
Set fs = Nothing 

感謝

+0

我已經從刪除所有評論代碼。絕對沒有必要這樣評論:'xlWkb.close'close workbook' - 代碼*已經說過*。你實質上是在編寫你的程序兩次 - 這是無稽之談。不要這樣做。 – Tomalak

回答

2

更新爲每點評:謝謝你們

Const xlCSV = 6 
xlWkb.SaveAs FullTargetPath, xlCSV, , , , , , 2 
xlWbk.Saved = True 
xlWkb.close 
+2

'xlCSV'在VBScript中不可用作爲名稱,您需要事先執行'Const xlCSV = 6'。 – Tomalak

+0

把那個放在SaveAs行之前? – chenks

+0

我會把它放在另一個'Const'定義之後。 – Tomalak

2

感謝你們......這裏是最終的腳本

Dim xlApp, xlWkb, SourceFolder,TargetFolder,file 
Set xlApp = CreateObject("excel.application") 
Set fs = CreateObject("Scripting.FileSystemObject") 

Const xlNormal=1 
Const xlCSV=6 

SourceFolder="c:\xml-to-xls\xml" 
TargetFolder="c:\xml-to-xls\xls" 

xlApp.Visible = false 

for each file in fs.GetFolder(SourceFolder).files 
    Set xlWkb = xlApp.Workbooks.Open(file) 
    BaseName= fs.getbasename(file) 
    FullTargetPath=TargetFolder & "\" & BaseName & ".csv" 
    xlWkb.SaveAs FullTargetPath, xlCSV, , , , , , 2 
    xlWkb.Saved = True 
    xlWkb.close 
    file.Delete 
next 

Set xlWkb = Nothing 
Set xlApp = Nothing 
Set fs = Nothing