Answers:
打开记事本,创建一个名为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。
oExcel.Workbooks.Open
具有所需工作表索引的行之后添加以下行(从1开始): oBook.Worksheets(1).Activate
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测试。
ScottF的groovy VB脚本的一个小扩展:该批处理文件将循环遍历目录中的.xlsx文件,并将它们转储为* .csv文件:
FOR /f "delims=" %%i IN ('DIR *.xlsx /b') DO ExcelToCSV.vbs "%%i" "%%i.csv"
注意:您可以将扩展名.xlsx更改为.xls,并将脚本ExcelToCSV的名称更改为XlsToCsv
如何使用PowerShell?
代码应如下所示,但未经测试
$xlCSV = 6
$Excel = New-Object -Com Excel.Application
$Excel.visible = $False
$Excel.displayalerts=$False
$WorkBook = $Excel.Workbooks.Open("YOUDOC.XLS")
$Workbook.SaveAs("YOURDOC.csv",$xlCSV)
$Excel.quit()
这是一篇说明如何使用它的帖子
$Excel.Workbooks.Open
方法的困难。找不到指定的文件。我通过Get-Item
在文件上使用并将其管道输送到以ForEach-Object
开头的两行循环到循环中(无论如何最终要在我的最终实现中完成)来解决此问题$Workbook
。
CD /D C:\ && DIR YOURDOC.csv /s
。原来该文件默认情况下已保存到“我的文档”中。因此,如果要将文件保存到工作所在的同一文件夹(如果不是“我的文档”),则需要在脚本中添加更多内容。
我需要从不同的工作表中提取多个简历,因此这是Plang代码的修改版本,允许您指定工作表名称。
if WScript.Arguments.Count < 3 Then
WScript.Echo "Please specify the sheet, the source, the destination files. Usage: ExcelToCsv <sheetName> <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(1))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(2))
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)
oBook.Sheets(WScript.Arguments.Item(0)).Select
oBook.SaveAs dest_file, csv_format
oBook.Close False
oExcel.Quit
这是一个可以处理多个文件从Windows拖放的版本。基于以上工作
Christian Lemer
plang
ScottF
打开记事本,创建一个名为XlsToCsv.vbs的文件并将其粘贴到:
'* Usage: Drop .xl* files on me to export each sheet as CSV
'* Global Settings and Variables
Dim gSkip
Set args = Wscript.Arguments
For Each sFilename In args
iErr = ExportExcelFileToCSV(sFilename)
' 0 for normal success
' 404 for file not found
' 10 for file skipped (or user abort if script returns 10)
Next
WScript.Quit(0)
Function ExportExcelFileToCSV(sFilename)
'* Settings
Dim oExcel, oFSO, oExcelFile
Set oExcel = CreateObject("Excel.Application")
Set oFSO = CreateObject("Scripting.FileSystemObject")
iCSV_Format = 6
'* Set Up
sExtension = oFSO.GetExtensionName(sFilename)
if sExtension = "" then
ExportExcelFileToCSV = 404
Exit Function
end if
sTest = Mid(sExtension,1,2) '* first 2 letters of the extension, vb's missing a Like operator
if not (sTest = "xl") then
if (PromptForSkip(sFilename,oExcel)) then
ExportExcelFileToCSV = 10
Exit Function
end if
End If
sAbsoluteSource = oFSO.GetAbsolutePathName(sFilename)
sAbsoluteDestination = Replace(sAbsoluteSource,sExtension,"{sheet}.csv")
'* Do Work
Set oExcelFile = oExcel.Workbooks.Open(sAbsoluteSource)
For Each oSheet in oExcelFile.Sheets
sThisDestination = Replace(sAbsoluteDestination,"{sheet}",oSheet.Name)
oExcelFile.Sheets(oSheet.Name).Select
oExcelFile.SaveAs sThisDestination, iCSV_Format
Next
'* Take Down
oExcelFile.Close False
oExcel.Quit
ExportExcelFileToCSV = 0
Exit Function
End Function
Function PromptForSkip(sFilename,oExcel)
if not (VarType(gSkip) = vbEmpty) then
PromptForSkip = gSkip
Exit Function
end if
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sPrompt = vbCRLF & _
"A filename was received that doesn't appear to be an Excel Document." & vbCRLF & _
"Do you want to skip this and all other unrecognized files? (Will only prompt this once)" & vbCRLF & _
"" & vbCRLF & _
"Yes - Will skip all further files that don't have a .xl* extension" & vbCRLF & _
"No - Will pass the file to excel regardless of extension" & vbCRLF & _
"Cancel - Abort any further conversions and exit this script" & vbCRLF & _
"" & vbCRLF & _
"The unrecognized file was:" & vbCRLF & _
sFilename & vbCRLF & _
"" & vbCRLF & _
"The path returned by the system was:" & vbCRLF & _
oFSO.GetAbsolutePathName(sFilename) & vbCRLF
sTitle = "Unrecognized File Type Encountered"
sResponse = MsgBox (sPrompt,vbYesNoCancel,sTitle)
Select Case sResponse
Case vbYes
gSkip = True
Case vbNo
gSkip = False
Case vbCancel
oExcel.Quit
WScript.Quit(10) '* 10 Is the error code I use to indicate there was a user abort (1 because wasn't successful, + 0 because the user chose to exit)
End Select
PromptForSkip = gSkip
Exit Function
End Function
为什么不自己写?
我从您的个人资料中看到,您至少具有一些C#/。NET经验。我将创建一个Windows控制台应用程序,并使用免费的Excel阅读器读取您的Excel文件。我已经使用了CodePlex的Excel Data Reader,没有任何问题(一件好事:此阅读器不需要安装Excel)。您可以从命令行调用控制台应用程序。
如果您发现自己的帖子停留在这里,我相信您会得到帮助。
您可以使用Alacon - Alasql数据库的命令行实用程序来完成此操作。它与Node.js一起使用,因此您需要先安装Node.js,然后再安装Alasql软件包。
要将Excel文件转换为CVS(ot TSV),可以输入:
> node alacon "SELECT * INTO CSV('mydata.csv', {headers:true}) FROM XLS('mydata.xls', {headers:true})"
默认情况下,Alasql会从“ Sheet1”转换数据,但您可以使用参数进行更改:
{headers:false, sheetid: 'Sheet2', range: 'A1:C100'}
Alacon支持其他类型的转换(CSV,TSV,TXT,XLSX,XLS)和SQL语言构造(有关示例,请参见用户手册)。
Scott F的答案是我在互联网上找到的最好的答案。我确实添加了他的代码以满足我的需求。我补充说:
错误恢复上的下一个 <-要在顶部的批处理中解决缺少的xls文件。 oBook.Application.Columns(“ A:J”)。NumberFormat =“ @” <-在SaveAs行之前,以确保将我的数据保存为文本格式,以防止excel删除数据中的前导零并消除数字字符串中的逗号即(1,200至1200)。列的范围应进行调整以满足您的需要(A:J)。
我还删除了“回声”,使它不具有交互性。
然后,我将脚本添加到cmd批处理文件中,以通过任务每小时处理一次自动数据。
所有这些答案都帮助我构建了以下脚本,该脚本通过将一个或多个文件拖放到脚本中(或通过命令行)来自动将XLS *文件转换为CSV ,反之亦然。不好意思的格式道歉。
' /programming/1858195/convert-xls-to-csv-on-command-line
' https://gist.github.com/tonyerskine/77250575b166bec997f33a679a0dfbe4
' https://stackoverflow.com/a/36804963/1037948
'* Global Settings and Variables
Set args = Wscript.Arguments
For Each sFilename In args
iErr = ConvertExcelFormat(sFilename)
' 0 for normal success
' 404 for file not found
' 10 for file skipped (or user abort if script returns 10)
Next
WScript.Quit(0)
Function ConvertExcelFormat(srcFile)
if IsEmpty(srcFile) OR srcFile = "" Then
WScript.Echo "Error! Please specify at least one source path. Usage: " & WScript.ScriptName & " SourcePath.xls*|csv"
ConvertExcelFormat = -1
Exit Function
'Wscript.Quit
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
srcExt = objFSO.GetExtensionName(srcFile)
' the 6 is the constant for 'CSV' format, 51 is for 'xlsx'
' https://msdn.microsoft.com/en-us/vba/excel-vba/articles/xlfileformat-enumeration-excel
' https://www.rondebruin.nl/mac/mac020.htm
Dim outputFormat, srcDest
If LCase(Mid(srcExt, 1, 2)) = "xl" Then
outputFormat = 6
srcDest = "csv"
Else
outputFormat = 51
srcDest = "xlsx"
End If
'srcFile = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
srcFile = objFSO.GetAbsolutePathName(srcFile)
destFile = Replace(srcFile, srcExt, srcDest)
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(srcFile)
' preserve formatting? https://stackoverflow.com/a/8658845/1037948
'oBook.Application.Columns("A:J").NumberFormat = "@"
oBook.SaveAs destFile, outputFormat
oBook.Close False
oExcel.Quit
WScript.Echo "Conversion complete of '" & srcFile & "' to '" & objFSO.GetFileName(destFile) & "'"
End Function
::对于UTF-8,适用于Microsoft Office 2016及更高版本!
试试这个代码:
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 = 62
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
在名为“ xls2csv.vbs”的桌面上创建一个TXT文件,然后粘贴代码:
Dim vExcel
Dim vCSV
Set vExcel = CreateObject("Excel.Application")
Set vCSV = vExcel.Workbooks.Open(Wscript.Arguments.Item(0))
vCSV.SaveAs WScript.Arguments.Item(0) & ".csv", 6
vCSV.Close False
vExcel.Quit
将XLS文件拖到其中(例如“ test.xls”)。它将创建一个名为“ test.xls.csv”的转换后的CSV文件。然后,将其重命名为“ test.csv”。做完了