使用VBScript(由分号分隔)将xls / xlsx文件(所有工作表)转换为csv

时间:2014-06-22 14:36:11

标签: excel csv vbscript xls

我需要将包含所有工作表的excel文件(* .xls,* .xlsx)转换为每张分割的CSV文件。 输出CSV文件应由SheetName命名,并用分号而不是逗号分隔。 我已经完成了这个,通过将几个VBS脚本绑定到一个,但我仍然有一个错误。 我的下面的代码确实转换为由分号分隔的XLS文件中的所有工作表,由工作表命名 - 但是如果有超过1个工作表,则下一个工作表的内容将被第一个工作表覆盖。 那有什么不对吗?我在cmd中运行它: xls_to_csv.vbs ExcelFile.xls OutPutdir

'------------------- SET SEMI-COLON DELIMITER VIA WIN REGISTERS ---------------
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


'----------------------------- CONVERT XLS TO CSV ------------------------------
Dim strExcelFileName
Dim strCSVFileName
Dim objFSO
Set objFSO = CreateObject("scripting.filesystemobject")  
strPath = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(1))

strExcelFileName = WScript.Arguments.Item(0)  'file name to parses

rem get path where script is running
Set fso = CreateObject ("Scripting.FileSystemObject")  'use this to find current path
strScript = Wscript.ScriptFullName
strScriptPath = fso.GetAbsolutePathName(strScript & "\..")

rem If the Input file is NOT qualified with a path, default the current path
LPosition = InStrRev(strExcelFileName, "\")
if LPosition = 0 Then 'no folder path
strExcelFileName = strScriptPath & "\" & strExcelFileName
strScriptPath = strScriptPath & "\"
else                 'there is a folder path, use it for the output folder path also
strScriptPath = Mid(strExcelFileName, 1, LPosition)
End If
rem msgbox LPosition & " - " & strExcelFileName & " - " & strScriptPath  ' use this for debugging

Dim objXL
Dim objWorkBook, local
Set objXL = CreateObject("Excel.Application")
Set objWorkBook = objXL.Workbooks.Open(strExcelFileName)

objXL.DisplayAlerts = False
rem loop over worksheets
  For Each sheet In objWorkBook.Sheets 
      'only saveAS sheets that are NOT empty
if objXL.Application.WorksheetFunction.CountA(sheet.Cells) <> 0 Then
rem             sheet.Rows(1).delete  ' this will remove Row 1 or the header Row
local = true  
call objWorkBook.SaveAs(strPath & "\" & sheet.Name & ".csv", 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, local)
    End If
  Next

rem clean up 
objWorkBook.Close
objXL.quit
Set objXL = Nothing
Set objWorkBook = Nothing
Set fso = Nothing

'------------------------- RETURN REGISTRY CHANGES BACK --------------------
' Reset the system setting to its original value
objRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strSystemDelimiter

rem end script    

2 个答案:

答案 0 :(得分:3)

您正在尝试保存工作表,但您正在使用WorkBook对象。尝试使用您使用For语句

提取的WorkSheet对象
 Dim WorkSheet
 For Each WorkSheet In objWorkBook.Sheets 
  If objXL.Application.WorksheetFunction.CountA(WorkSheet.Cells) <> 0 Then
   WorkSheet.SaveAs strPath & "\" & WorkSheet.Name & ".csv", 6
  End If
 Next

这适合我。

我已将“工作表”更改为“工作表”,只是为了强调差异。当然“工作表”或任何其他对象名称都可以正常工作。

答案 1 :(得分:1)

我的建议是使用不同的语言。为了更改分隔符而编辑注册表项至少可以说是“hacky”。​​

可以通过简单的Python脚本并使用xlrd和csv包来完成。这将使其可用于多个平台。您可以使用Py2exe轻松地将Python脚本转换为.exe。