Joe*_*oel 97 windows csv excel
如何在Windows命令行上将XLS文件转换为CSV文件.
该计算机已安装Microsoft Office 2000.如果不能使用Microsoft Office,我愿意安装OpenOffice.
Sco*_*ttF 116
打开记事本,创建一个名为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"
Run Code Online (Sandbox Code Playgroud)
然后从命令行转到保存.vbs文件的文件夹并运行:
XlsToCsv.vbs [sourcexlsFile].xls [destinationcsvfile].csv
Run Code Online (Sandbox Code Playgroud)
这需要在您所在的计算机上安装Excel.
pla*_*ang 75
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
Run Code Online (Sandbox Code Playgroud)
我已经重命名了ExcelToCsv脚本,因为这个脚本根本不限于xls.xlsx工作正常,正如我们所料.
使用Office 2010进行测试.
小智 25
ScottF的groovy VB脚本的一个小扩展:这个批处理文件将循环遍历目录中的.xlsx文件并将它们转储到*.csv文件中:
FOR /f "delims=" %%i IN ('DIR *.xlsx /b') DO ExcelToCSV.vbs "%%i" "%%i.csv"
Run Code Online (Sandbox Code Playgroud)
注意:您可以将扩展名.xlsx更改为.xls和脚本ExcelToCSV的名称更改为XlsToCsv
YOU*_*YOU 17
使用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()
Run Code Online (Sandbox Code Playgroud)
这是一篇解释如何使用它的帖子
如何使用Windows PowerShell自动化Microsoft Excel?
我需要从不同的工作表中提取几个cvs,所以这里有一个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
Run Code Online (Sandbox Code Playgroud)
这是一个版本,可以处理从Windows拖放的多个文件.基于以上作品
Christian Lemer
plang
ScottF
Run Code Online (Sandbox Code Playgroud)
打开记事本,创建一个名为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
Run Code Online (Sandbox Code Playgroud)
为什么不写自己的?
我从你的个人资料中看到你至少有一些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})"
Run Code Online (Sandbox Code Playgroud)
默认情况下,Alasql 转换来自“Sheet1”的数据,但您可以使用参数更改它:
{headers:false, sheetid: 'Sheet2', range: 'A1:C100'}
Run Code Online (Sandbox Code Playgroud)
Alacon 支持其他类型的转换(CSV、TSV、TXT、XLSX、XLS)和 SQL 语言结构(示例参见用户手册)。
| 归档时间: |
|
| 查看次数: |
236547 次 |
| 最近记录: |