如何在windows命令行上将XLS文件转换为CSV文件。
该计算机安装了Microsoft Office 2000。如果不能使用Microsoft Office,我愿意安装OpenOffice。
发布于 2009-12-07 22:22:28
打开记事本,创建一个名为XlsToCsv.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.SaveAs WScript.Arguments.Item(1), 6
oBook.Close False
oExcel.Quit
WScript.Echo "Done"
然后从命令行转到保存.vbs文件的文件夹并运行:
XlsToCsv.vbs [sourcexlsFile].xls [destinationcsvfile].csv
不过,这需要在您所在的计算机上安装Excel。
发布于 2012-05-31 22:25:41
略有修改的ScottF答案版本,不需要绝对文件路径:
if WScript.Arguments.Count < 2 Then
WScript.Echo "Please specify the source and the destination files. Usage: ExcelToCsv <xls/xlsx source file> <csv destination file>"
Wscript.Quit
End If
csv_format = 6
Set objFSO = CreateObject("Scripting.FileSystemObject")
src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)
oBook.SaveAs dest_file, csv_format
oBook.Close False
oExcel.Quit
我已将脚本重命名为ExcelToCsv,因为此脚本根本不限于xls。正如我们所期望的那样,xlsx工作得很好。
已使用Office 2010进行测试。
发布于 2012-06-29 04:54:40
ScottF的groovy VB脚本的一个小扩展:这个批处理文件将遍历目录中的.xlsx文件,并将它们转储到*.csv文件中:
FOR /f "delims=" %%i IN ('DIR *.xlsx /b') DO ExcelToCSV.vbs "%%i" "%%i.csv"
注意:您可以将扩展名更改为.xls,将脚本ExcelToCSV的名称更改为XlsToCsv
https://stackoverflow.com/questions/1858195
复制相似问题