自动将Xls转换为CSV

我正在使用下面的stackoverflow上find的VBS脚本将xls转换为csv。 它工作正常。 我想用batch file在底部运行它。 我不知道如何达到我想要的。 batch file为csv文件提供与xls文件相同的名称。 由于xls文件中有两个工作表,我需要为每个xls文件生成两个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 Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(1)) oBook.SaveAs WScript.Arguments.Item(2), 6 oBook.Close False oExcel.Quit WScript.Echo "Done" 

这是batch file

 FOR /f "delims=" %%i IN ('DIR *.xlsx /b') DO ExcelToCSV.vbs "%%i" "%%i.csv" 

我需要传递2个输出.csv文件名,其中一个应该是nnnnn_1.csv,另一个应该是nnnnn_2.csv来解释xls文件中的2个工作表。

谢谢你的帮助

与上面Scott的回答类似,我可能会更改脚本以这种方式工作,因为这种更改将适用于给定工作簿中的所有工作表,并将每个工作表输出到.csv文件,而不必担心是否有1,2或10个工作表。

 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)) Dim oNewBook shIndex = 1 ' start with 1 For shIndex = 1 To oBook.Worksheets.Count oBook.Worksheets(shIndex).Copy Set oNewBook = oExcel.ActiveWorkbook oNewBook.SaveAs Replace(WScript.Arguments.Item(1),".csv","_" & shIndex & ".csv"), 6 oNewBook.Close False Next oBook.Close False oExcel.Quit WScript.Echo "Done" 

该脚本会将工作簿中的每个工作表保存为[cvs base name] - [worksheet name].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 BaseName, oExcel, oBook, xlWorksheet, i BaseName = Replace( WScript.Arguments.Item(0), ".cvs", "") Set oExcel = CreateObject("Excel.Application") Set oBook = oExcel.Workbooks.Open(WScript.Arguments.Item(0)) For Each xlWorksheet In oBook.Worksheets xlWorksheet.Copy oExcel.ActiveWorkbook.SaveAs BaseName & " - " & xlWorksheet.Name & ".csv" oExcel.ActiveWorkbook.Close False Next oBook.Close False oExcel.Quit WScript.Echo "Done" 

如果您将VBS脚本更改为此,它应该可以工作:

 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.Worksheets(1).Copy Dim oBookNew1 Set oBookNew1 = oExcel.ActiveWorkbook oBookNew1.SaveAs Replace(WScript.Arguments.Item(1),".csv","_1.csv"), 6 oBookNew1.Close False oBook.Worksheets(2).Copy Dim oBookNew1 Set oBookNew1 = oExcel.ActiveWorkbook oBookNew2.SaveAs Replace(WScript.Arguments.Item(1),".csv","_2.csv"), 6 oBookNew2.Close False oBook.Close False oExcel.Quit WScript.Echo "Done" 

我已经在Python中编写了一个替代解决scheme,用CVS格式导出工作簿的当前Excel表格。

你可以在这里find它

最好的,朱利安