在分隔文本文件中使用逗號會在區域設置中找到它的根。雖然逗號在美國是標準的,但其他國家(例如德國)則使用分號代替。您可以更改區域和語言設置中的列表分隔符值,然後從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
這不是一個一致的解決方案,因爲它也將取代它逗號爲文字:'John Doe,Euro,「1,234.00」'將解析爲'John Doe; Euro;「1; 234.00」',可能不是用戶想要的。 – AutomatedChaos 2012-02-13 08:02:16
很顯然,如果這是數據類型,它不會有幫助。當我發佈此信息時,沒有其他回覆,所以我只是簡單地提供一些回覆。 – 2012-02-13 12:01:32
當然。如果用戶只有有限的一組數據而沒有逗號,那麼它會工作正常。但是,將數據轉換爲正確的CSV文件非常安靜。即使微軟也沒有跟上[rfc4180](http://tools.ietf.org/html/rfc4180)的格式定義。 – AutomatedChaos 2012-02-13 16:05:25