使用VBScript将xls转换为csv并用分号分隔

使用VBScript将xls转换为csv并用分号分隔,csv,vbscript,xls,xlsx,Csv,Vbscript,Xls,Xlsx,我有一个VBScript代码段,它将我的xls和xlsx文件转换为csv文件。但是,我希望每个单元格用分号而不是逗号分隔。在我的计算机上,列表分隔符设置为分号而不是逗号,因此当我打开excel窗口并另存为csv时,它会以分号分隔。但是,我的VBScript生成一个用逗号分隔的csv文件。我在网上找到了代码片段,因为我对VBScript(我主要是Java程序员)不太了解。如何更改代码段,以分号而不是逗号分隔csv文件 if WScript.Arguments.Count < 2 Then W

我有一个VBScript代码段,它将我的xls和xlsx文件转换为csv文件。但是,我希望每个单元格用分号而不是逗号分隔。在我的计算机上,列表分隔符设置为分号而不是逗号,因此当我打开excel窗口并另存为csv时,它会以分号分隔。但是,我的VBScript生成一个用逗号分隔的csv文件。我在网上找到了代码片段,因为我对VBScript(我主要是Java程序员)不太了解。如何更改代码段,以分号而不是逗号分隔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")
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"
如果WScript.Arguments.Count<2,则
WScript.Echo“错误!请指定源路径和目标。用法:XlsToCsv SourcePath.xls destination.csv”
Wscript.Quit
如果结束
暗色oExcel
设置oExcel=CreateObject(“Excel.Application”)
Dim oBook
设置oBook=oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.SaveAs WScript.Arguments.Item(1),6
好的,关上
oExcel,退出
Echo“完成”

您可以使用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

在分隔文本文件中使用逗号可在区域设置中找到其根。虽然逗号在美国是标准的,但德国等其他国家则使用分号。您可以在区域和语言设置中更改列表分隔符值,然后从Excel的“另存为”窗口中选择CSV(逗号分隔)(.CSV)。结果文件将由系统设置中的任何值分隔。此脚本更改默认列表分隔符设置。然后打开指定的电子表格并重新保存。在完成之前,它会将系统设置恢复到以前的值

它接受两个命令行参数。第一个是输入电子表格;第二个是导出文件的输出文件名

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界面打开“另存为”对话框。这应该能奏效。它在Vista x64和Excel 2007上对我有效。我希望这对你有用

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

您可以保留原始脚本,只需提供一个参数来指示必须应用本地设置。这将保存我的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" 
如果WScript.Arguments.Count<2,则
WScript.Echo“错误!请指定源路径和目标。用法:XlsToCsv SourcePath.xls destination.csv”
Wscript.Quit
如果结束
暗色oExcel
设置oExcel=CreateObject(“Excel.Application”)
oExcel.DisplayAlerts=FALSE'以避免提示
Dim oBook,本地
设置oBook=oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
本地=真
调用oBook.SaveAs(WScript.Arguments.Item(1)、6、0、0、0、0、0、0、0、0、local)’这已更改
好的,关上
oExcel,退出
Echo“完成”

函数SaveAs的定义如下: .SaveAs(文件名、文件格式、密码、WriteResPassword、ReadOnlyRecommended、CreateBackup、AccessMode、ConflictResolution、AddToMru、TextCodepage、TextVisualYout、Local)

Thas是,使用分号(如果您的区域语言选项设置正确)


ExcelObj.工作簿(1).另存为csvFile,6,,,,,,,True

我将参数更改为True,并为我工作。

if WScript.Arguments.Count < 2 Then
    WScript.Echo "Erro! Especifique origem e destino. Exemplo: 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))
call oBook.SaveAs(WScript.Arguments.Item(1), 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, true) --CHANGED
oBook.Close False
oExcel.Quit
如果WScript.Arguments.Count<2,则
Echo“Erro!specifique origem e destino.examplo:XlsToCsv SourcePath.xls Destination.csv”
Wscript.Quit
如果结束
暗色oExcel
设置oExcel=CreateObject(“Excel.Application”)
暗双簧管
设置oBook=oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
调用oBook.SaveAs(WScript.Arguments.Item(1)、6、0、0、0、0、0、0、0、true)——已更改
好的,关上
oExcel,退出

这不是一个一致的解决方案,因为它还将替换文本中的逗号:
johndoe,Euro,“1234.00”
将解析为
johndoe;欧元“1;234.00”
,可能不是用户想要的。显然,如果这是数据类型,则没有帮助。当我发布这篇文章时,没有其他回复,所以我只是简单地给出一些回复。当然。如果用户只有一组有限的数据,而没有逗号,那么结果会很好。然而,将数据转换为正确的CSV文件是一件困难的事情。甚至微软也没有跟上格式定义的步伐。是的,文件中会有钱,这就是为什么我想用分号而不是逗号分隔。如果我不是在和钱打交道,我会坚持用逗号,就这样算了。但是谢谢你的帮助,我很感激。谢谢你!但是,我在控制面板中将计算机上的分隔符更改为分号,但当VBScript运行时,它使用逗号作为分隔符,这很奇怪,因为在Excel中另存为时,它以分号分隔。这似乎只是从命令行执行相同的操作,而不是单击并更改控制面板中的设置。但我会试试这个脚本,让你知道结果。是的,我得到了同样的结果。它由逗号分隔。我在学校的电脑上,也许它不允许我编辑注册表项?当我在家里的电脑上时,我会试试。我希望这就是原因。我会在家里告诉你我的结果。卢克,我解决了问题,并在回复中发布了一些新代码。这应该对你有用。这也不行。excel文件打开,但随后代码尝试使用cmd中的命令“fatc”,它表示“这不被识别为内部或批处理命令、可操作程序或批处理文件”。错误命令的常见错误消息。excel窗口也将关闭。我不知道为什么会发生这种错误。但是,这个解决方案可能对我不起作用,因为它似乎会为我试图转换的每个excel文件打开一个excel窗口,这是一个问题,因为我将使用它一次转换大约700个excel文件…这是很多excel文件