2012-02-09 49 views
2

我有一個VBScript代碼片段其中我XLS和XLSX文件轉換成CSV文件。但是,我希望每個單元格都用分號而不是逗號分隔。在我的電腦上,列表分隔符被設置爲分號而不是逗號,所以當我打開一個excel窗口並保存爲csv時,它以分號分隔。但是,我的VBScript生成一個用逗號分隔的csv文件。我在網上找到了代碼片斷,因爲我不太瞭解VBScript(我主要是Java程序員)。我如何更改代碼片段以分號而不是逗號分隔csv文件?轉換XLS到csv使用VBScript和單獨用分號

if WScript.Arguments.Count < 2 Then 
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv" 
Wscript.Quit 
End If 
Dim oExcel 
Set oExcel = CreateObject("Excel.Application") 
Dim oBook 
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0)) 
oBook.SaveAs WScript.Arguments.Item(1), 6 
oBook.Close False 
oExcel.Quit 
WScript.Echo "Done" 

回答

9

你可以保持您的原始腳本,只需要給一個參數來指示本地設置必須申請。這將我的CSV保存爲一個;分離器

if WScript.Arguments.Count < 2 Then 
    WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv" 
    Wscript.Quit 
End If 
Dim oExcel 
Set oExcel = CreateObject("Excel.Application") 
oExcel.DisplayAlerts = FALSE 'to avoid prompts 
Dim oBook, local 
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0)) 
local = true 
call oBook.SaveAs(WScript.Arguments.Item(1), 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, local) 'this changed 
oBook.Close False 
oExcel.Quit 
WScript.Echo "Done" 
0

您可以用FSO對象重新打開該文件,然後對逗號字符執行Replace()。

Const OpenAsDefault = -2 
Const FailIfNotExist = 0 
Const ForReading = 1 
Const ForWriting = 2 

Set oFSO = CreateObject("Scripting.FileSystemObject") 
Set fCSVFile = _ 
    oFSO.OpenTextFile("C:\path\file.csv", ForReading, FailIfNotExist, OpenAsDefault) 

sFileContents = fCSVFile.ReadAll 
fCSVFile.Close 
sFileContents = Replace(sFileContents, ",",";")) 

Set fCSVFile = oFSO.OpenTextFile("C:\path\file.csv", ForWriting, True) 
fCSVFile.Write(sFileContents) 
fCSVFile.Close 
+0

這不是一個一致的解決方案,因爲它也將取代它逗號爲文字:'John Doe,Euro,「1,234.00」'將解析爲'John Doe; Euro;「1; 234.00」',可能不是用戶想要的。 – AutomatedChaos 2012-02-13 08:02:16

+0

很顯然,如果這是數據類型,它不會有幫助。當我發佈此信息時,沒有其他回覆,所以我只是簡單地提供一些回覆。 – 2012-02-13 12:01:32

+1

當然。如果用戶只有有限的一組數據而沒有逗號,那麼它會工作正常。但是,將數據轉換爲正確的CSV文件非常安靜。即使微軟也沒有跟上[rfc4180](http://tools.ietf.org/html/rfc4180)的格式定義。 – AutomatedChaos 2012-02-13 16:05:25

1

在分隔文本文件中使用逗號會在區域設置中找到它的根。雖然逗號在美國是標準的,但其他國家(例如德國)則使用分號代替。您可以更改區域和語言設置中的列表分隔符值,然後從Excel的另存爲窗口中選擇CSV(逗號分隔)(.csv)。生成的文件將由系統設置中的任何值分隔。該腳本更改了默認的List Separator設置。然後它打開指定的電子表格並重新保存它。在完成之前,它將系統設置恢復到先前的值。

它接受兩個命令行參數。首先是輸入電子表格;第二個是輸出文件的輸出文件名。

strDelimiter = ";" 

strSystemDelimiter = ""   ' This will be used to store the current sytem value 
Const HKEY_CURRENT_USER = &H80000001 

' Get the current List Separator (Regional Settings) from the registry 
strKeyPath = "Control Panel\International" 
strValueName = "sList" 
strComputer = "." 
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") 
objRegistry.GetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strSystemDelimiter 

' Set it temporarily to our custom delimiter 
objRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strDelimiter 

' Open spreadsheet with Excel and save it in a text delimited format 
Const xlCSV = 6 

Set objExcel = CreateObject("Excel.Application") 
Set objWorkbook = objExcel.Workbooks.Open(WScript.Arguments.Item(0)) 
objWorkbook.SaveAs WScript.Arguments.Item(1), xlCSV 
objWorkbook.Close vbFalse   ' Prevent duplicate Save dialog 
objExcel.Quit 

' Reset the system setting to its original value 
objRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strSystemDelimiter 

經過一番測試,看來這隻能通過Excel的另存爲對話框,而不是通過命令行或自動化。爲了使Excel窗口可見並使用快捷鍵通過Excel界面打開「另存爲」對話框,我稍微更改了該腳本。這應該做的伎倆。它在Excel 2007中對Vista有幫助。我希望這對你有用。

strDelimiter = ";" 

strSystemDelimiter = ""   ' This will be used to store the current sytem value 
Const HKEY_CURRENT_USER = &H80000001 

' Get the current List Separator (Regional Settings) from the registry 
strKeyPath = "Control Panel\International" 
strValueName = "sList" 
strComputer = "." 
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") 
objRegistry.GetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strSystemDelimiter 

' Set it temporarily to our custom delimiter 
objRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strDelimiter 

' Open spreadsheet with Excel and save it in a text delimited format 
Const xlCSV = 6 

Set objExcel = CreateObject("Excel.Application") 
objExcel.Visible = vbTrue 
Set objWorkbook = objExcel.Workbooks.Open(WScript.Arguments.Item(0)) 

WScript.Sleep 500     ' Delay to make sure the Excel workbook is open 
strWorkbookName = objExcel.ActiveWorkbook.Name 
strTitlebar = strWorkbookName 
Set WshShell = CreateObject("WScript.Shell") 
WshShell.AppActivate strTitlebar ' Make the workbook active so it receives the keystrokes 
WshShell.SendKeys "%fa"   ' Keyboard shortcuts for the Save As dialog 
WScript.Sleep 500 
WshShell.SendKeys "%tc{ENTER}" ' Change the Save As type to CSV 
If WScript.Arguments.Count > 1 Then 
    WshShell.SendKeys "+{TAB}" & WScript.Arguments.Item(1) 
    WScript.Sleep 500 
End If       ' This If block changes the save name if one was provided 
WshShell.SendKeys "{ENTER}"  ' Save the file 
WScript.Sleep 500 
WshShell.SendKeys "{ENTER}"  ' Dismiss the CSV warning dialog 
Set WshShell = Nothing 

objWorkbook.Close vbFalse   ' Prevent duplicate Save dialog 
objExcel.Quit 

' Reset the system setting to its original value 
objRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strSystemDelimiter 
+0

謝謝!不過,我將控制面板中我的計算機上的分隔符更改爲分號,但當VBScript運行時,它使用逗號作爲其分隔符,這很奇怪,因爲當我另存爲Excel時,它以分號分隔。這似乎是從命令行執行完全相同的事情,而不是單擊並更改控制面板中的設置。但我會嘗試這個腳本並讓你知道結果。 – 2012-02-15 18:57:47

+0

是的,我得到了同樣的結果。它被逗號分開。我在學校電腦上,也許它不會允許我編輯註冊表項?當我在家用電腦上時,我會嘗試。我希望這是原因。我會讓你知道我在家裏的結果。 – 2012-02-15 19:04:09

+0

盧克,我想出了這個問題,並在我的回覆中發佈了一些新代碼。這應該適合你。 – Nilpo 2012-02-15 20:31:13

1

的作用另存爲被限定爲使得: .SaveAs(文件名,的FileFormat,密碼WriteResPassword,ReadOnlyRecommended,CreateBackup,AccessMode,衝突解決,AddToMru,TextCodepage,TextVisualLayout,本地)

THAS是,用分號(如果您的區域語言選項都設置正確)

ExcelObj.Workbooks(1).SaveAs csvFile,6 ,,,,,,,,,,真

+0

謝謝,但我不再需要此代碼。非常感謝你。 – 2013-01-12 19:43:41

+0

最好的答案在這裏... 只有你需要注意的是你的Excel版本真正的位置12爲Excel 2013工作 對於Excel 2010它是2個參數少於位置10 還可以檢查: http ://msdn.microsoft.com/en-us/library/office/microsoft.office.interop.excel.dialogsheet.saveas.aspx 在該上下文中... – WD11 2013-10-21 16:45:48